using System; using System.Threading.Tasks; using BililiveRecorder.Core.Api.Model; using Polly; using Polly.Registry; namespace BililiveRecorder.Core.Api { internal class PolicyWrappedApiClient : IApiClient, IDanmakuServerApiClient, IDisposable where T : class, IApiClient, IDanmakuServerApiClient, IDisposable { private readonly T client; private readonly IReadOnlyPolicyRegistry policies; public PolicyWrappedApiClient(T client, IReadOnlyPolicyRegistry policies) { this.client = client ?? throw new ArgumentNullException(nameof(client)); this.policies = policies ?? throw new ArgumentNullException(nameof(policies)); } public async Task> GetDanmakuServerAsync(int roomid) => await this.policies .Get(PolicyNames.PolicyDanmakuApiRequestAsync) .ExecuteAsync(_ => this.client.GetDanmakuServerAsync(roomid), new Context(PolicyNames.CacheKeyDanmaku + ":" + roomid)) .ConfigureAwait(false); public async Task> GetRoomInfoAsync(int roomid) => await this.policies .Get(PolicyNames.PolicyRoomInfoApiRequestAsync) .ExecuteAsync(_ => this.client.GetRoomInfoAsync(roomid), new Context(PolicyNames.CacheKeyRoomInfo + ":" + roomid)) .ConfigureAwait(false); public async Task> GetStreamUrlAsync(int roomid, int qn) => await this.policies .Get(PolicyNames.PolicyStreamApiRequestAsync) .ExecuteAsync(_ => this.client.GetStreamUrlAsync(roomid, qn), new Context(PolicyNames.CacheKeyStream + ":" + roomid + ":" + qn)) .ConfigureAwait(false); public void Dispose() => this.client.Dispose(); } }