2021-02-23 18:03:37 +08:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BililiveRecorder.Core.Api.Model;
|
|
|
|
using Polly;
|
|
|
|
using Polly.Registry;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.Core.Api
|
|
|
|
{
|
2022-05-16 23:28:31 +08:00
|
|
|
internal class PolicyWrappedApiClient<T> : IApiClient, IDanmakuServerApiClient, IDisposable where T : class, IApiClient, IDanmakuServerApiClient, IDisposable
|
2021-02-23 18:03:37 +08:00
|
|
|
{
|
|
|
|
private readonly T client;
|
|
|
|
private readonly IReadOnlyPolicyRegistry<string> policies;
|
|
|
|
|
|
|
|
public PolicyWrappedApiClient(T client, IReadOnlyPolicyRegistry<string> policies)
|
|
|
|
{
|
|
|
|
this.client = client ?? throw new ArgumentNullException(nameof(client));
|
|
|
|
this.policies = policies ?? throw new ArgumentNullException(nameof(policies));
|
|
|
|
}
|
|
|
|
|
2023-07-04 08:34:11 +08:00
|
|
|
public long GetUid() => this.client.GetUid();
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
public async Task<BilibiliApiResponse<DanmuInfo>> GetDanmakuServerAsync(int roomid) => await this.policies
|
|
|
|
.Get<IAsyncPolicy>(PolicyNames.PolicyDanmakuApiRequestAsync)
|
|
|
|
.ExecuteAsync(_ => this.client.GetDanmakuServerAsync(roomid), new Context(PolicyNames.CacheKeyDanmaku + ":" + roomid))
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
public async Task<BilibiliApiResponse<RoomInfo>> GetRoomInfoAsync(int roomid) => await this.policies
|
|
|
|
.Get<IAsyncPolicy>(PolicyNames.PolicyRoomInfoApiRequestAsync)
|
|
|
|
.ExecuteAsync(_ => this.client.GetRoomInfoAsync(roomid), new Context(PolicyNames.CacheKeyRoomInfo + ":" + roomid))
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
2021-07-09 19:48:39 +08:00
|
|
|
public async Task<BilibiliApiResponse<RoomPlayInfo>> GetStreamUrlAsync(int roomid, int qn) => await this.policies
|
2021-02-23 18:03:37 +08:00
|
|
|
.Get<IAsyncPolicy>(PolicyNames.PolicyStreamApiRequestAsync)
|
2021-07-09 19:48:39 +08:00
|
|
|
.ExecuteAsync(_ => this.client.GetStreamUrlAsync(roomid, qn), new Context(PolicyNames.CacheKeyStream + ":" + roomid + ":" + qn))
|
2021-02-23 18:03:37 +08:00
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
public void Dispose() => this.client.Dispose();
|
|
|
|
}
|
|
|
|
}
|