-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
497 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...KIT.FlurlHttpClient.ByteDance.MicroApp/ExtendedSDK/Webcast/DouyinMicroAppWebcastClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Flurl.Http; | ||
|
||
namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast | ||
{ | ||
/// <summary> | ||
/// 一个抖音小程序直播小玩法 API HTTP 客户端。 | ||
/// </summary> | ||
public class DouyinMicroAppWebcastClient : CommonClientBase, ICommonClient | ||
{ | ||
/// <summary> | ||
/// 获取当前客户端使用的抖音小程序凭证。 | ||
/// </summary> | ||
public Settings.Credentials Credentials { get; } | ||
|
||
/// <summary> | ||
/// 用指定的配置项初始化 <see cref="DouyinMicroAppWebcastClient"/> 类的新实例。 | ||
/// </summary> | ||
/// <param name="options">配置项。</param> | ||
public DouyinMicroAppWebcastClient(DouyinMicroAppWebcastClientOptions options) | ||
: this(options, null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="httpClient"></param> | ||
/// <param name="disposeClient"></param> | ||
internal protected DouyinMicroAppWebcastClient(DouyinMicroAppWebcastClientOptions options, HttpClient? httpClient, bool disposeClient = true) | ||
: base(httpClient, disposeClient) | ||
{ | ||
if (options is null) throw new ArgumentNullException(nameof(options)); | ||
|
||
Credentials = new Settings.Credentials(options); | ||
|
||
FlurlClient.BaseUrl = options.Endpoint ?? DouyinMicroAppWebcastEndpoints.DEFAULT; | ||
FlurlClient.WithTimeout(options.Timeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(options.Timeout)); | ||
} | ||
|
||
/// <summary> | ||
/// 使用当前客户端生成一个新的 <see cref="IFlurlRequest"/> 对象。 | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="httpMethod"></param> | ||
/// <param name="urlSegments"></param> | ||
/// <returns></returns> | ||
public IFlurlRequest CreateFlurlRequest(DouyinMicroAppWebcastRequest request, HttpMethod httpMethod, params object[] urlSegments) | ||
{ | ||
IFlurlRequest flurlRequest = base.CreateFlurlRequest(request, httpMethod, urlSegments); | ||
|
||
return flurlRequest; | ||
} | ||
|
||
/// <summary> | ||
/// 异步发起请求。 | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="flurlRequest"></param> | ||
/// <param name="httpContent"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task<T> SendFlurlRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default) | ||
where T : DouyinMicroAppWebcastResponse, new() | ||
{ | ||
if (flurlRequest is null) throw new ArgumentNullException(nameof(flurlRequest)); | ||
|
||
using IFlurlResponse flurlResponse = await base.SendFlurlRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false); | ||
return await WrapFlurlResponseAsJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// 异步发起请求。 | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="flurlRequest"></param> | ||
/// <param name="data"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task<T> SendFlurlRequestAsJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default) | ||
where T : DouyinMicroAppWebcastResponse, new() | ||
{ | ||
if (flurlRequest is null) throw new ArgumentNullException(nameof(flurlRequest)); | ||
|
||
bool isSimpleRequest = data is null || | ||
flurlRequest.Verb == HttpMethod.Get || | ||
flurlRequest.Verb == HttpMethod.Head || | ||
flurlRequest.Verb == HttpMethod.Options; | ||
using IFlurlResponse flurlResponse = isSimpleRequest ? | ||
await base.SendFlurlRequestAsync(flurlRequest, null, cancellationToken) : | ||
await base.SendFlurlRequestAsJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false); | ||
return await WrapFlurlResponseAsJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...rlHttpClient.ByteDance.MicroApp/ExtendedSDK/Webcast/DouyinMicroAppWebcastClientBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
|
||
namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast | ||
{ | ||
/// <summary> | ||
/// 用于构造 <see cref="DouyinMicroAppWebcastClient"/> 实例的构造器。 | ||
/// </summary> | ||
public partial class DouyinMicroAppWebcastClientBuilder : ICommonClientBuilder<DouyinMicroAppWebcastClient> | ||
{ | ||
private readonly DouyinMicroAppWebcastClientOptions _options; | ||
private readonly IList<Action<CommonClientSettings>> _configures; | ||
private readonly IList<HttpInterceptor> _interceptors; | ||
private HttpClient? _httpClient; | ||
private bool? _disposeClient; | ||
|
||
private DouyinMicroAppWebcastClientBuilder(DouyinMicroAppWebcastClientOptions options) | ||
{ | ||
_options = options; | ||
_configures = new List<Action<CommonClientSettings>>(); | ||
_interceptors = new List<HttpInterceptor>(); | ||
} | ||
|
||
ICommonClientBuilder<DouyinMicroAppWebcastClient> ICommonClientBuilder<DouyinMicroAppWebcastClient>.ConfigureSettings(Action<CommonClientSettings> configure) | ||
{ | ||
return ConfigureSettings(configure); | ||
} | ||
|
||
ICommonClientBuilder<DouyinMicroAppWebcastClient> ICommonClientBuilder<DouyinMicroAppWebcastClient>.UseInterceptor(HttpInterceptor interceptor) | ||
{ | ||
return UseInterceptor(interceptor); | ||
} | ||
|
||
ICommonClientBuilder<DouyinMicroAppWebcastClient> ICommonClientBuilder<DouyinMicroAppWebcastClient>.UseHttpClient(HttpClient httpClient, bool disposeClient) | ||
{ | ||
return UseHttpClient(httpClient, disposeClient); | ||
} | ||
|
||
public DouyinMicroAppWebcastClientBuilder ConfigureSettings(Action<CommonClientSettings> configure) | ||
{ | ||
if (configure is null) throw new ArgumentNullException(nameof(configure)); | ||
|
||
_configures.Add(configure); | ||
return this; | ||
} | ||
|
||
public DouyinMicroAppWebcastClientBuilder UseInterceptor(HttpInterceptor interceptor) | ||
{ | ||
if (interceptor is null) throw new ArgumentNullException(nameof(interceptor)); | ||
|
||
_interceptors.Add(interceptor); | ||
return this; | ||
} | ||
|
||
public DouyinMicroAppWebcastClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true) | ||
{ | ||
if (httpClient is null) throw new ArgumentNullException(nameof(httpClient)); | ||
|
||
_httpClient = httpClient; | ||
_disposeClient = disposeClient; | ||
return this; | ||
} | ||
|
||
public DouyinMicroAppWebcastClient Build() | ||
{ | ||
DouyinMicroAppWebcastClient client = _disposeClient.HasValue | ||
? new DouyinMicroAppWebcastClient(_options, _httpClient, _disposeClient.Value) | ||
: new DouyinMicroAppWebcastClient(_options, _httpClient); | ||
|
||
foreach (Action<CommonClientSettings> configure in _configures) | ||
{ | ||
client.Configure(configure); | ||
} | ||
|
||
foreach (HttpInterceptor interceptor in _interceptors) | ||
{ | ||
client.Interceptors.Add(interceptor); | ||
} | ||
|
||
return client; | ||
} | ||
} | ||
|
||
partial class DouyinMicroAppWebcastClientBuilder | ||
{ | ||
public static DouyinMicroAppWebcastClientBuilder Create(DouyinMicroAppWebcastClientOptions options) | ||
{ | ||
if (options is null) throw new ArgumentNullException(nameof(options)); | ||
|
||
return new DouyinMicroAppWebcastClientBuilder(options); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...rlHttpClient.ByteDance.MicroApp/ExtendedSDK/Webcast/DouyinMicroAppWebcastClientOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast | ||
{ | ||
/// <summary> | ||
/// 一个用于构造 <see cref="DouyinMicroAppWebcastClient"/> 时使用的配置项。 | ||
/// </summary> | ||
public class DouyinMicroAppWebcastClientOptions | ||
{ | ||
/// <summary> | ||
/// 获取或设置请求超时时间(单位:毫秒)。 | ||
/// <para>默认值:30000</para> | ||
/// </summary> | ||
public int Timeout { get; set; } = 30 * 1000; | ||
|
||
/// <summary> | ||
/// 获取或设置抖音小程序直播小玩法 API 入口点。 | ||
/// <para>默认值:<see cref="DouyinMicroAppWebcastEndpoints.DEFAULT"/></para> | ||
/// </summary> | ||
public string Endpoint { get; set; } = DouyinMicroAppWebcastEndpoints.DEFAULT; | ||
|
||
/// <summary> | ||
/// 获取或设置抖音小程序 AppId。 | ||
/// </summary> | ||
public string AppId { get; set; } = default!; | ||
|
||
/// <summary> | ||
/// 获取或设置抖音小程序 AppSecret。 | ||
/// </summary> | ||
public string AppSecret { get; set; } = default!; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
....FlurlHttpClient.ByteDance.MicroApp/ExtendedSDK/Webcast/DouyinMicroAppWebcastEndpoints.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast | ||
{ | ||
/// <summary> | ||
/// 抖音小程序直播小玩法 API 接口域名。 | ||
/// </summary> | ||
public static class DouyinMicroAppWebcastEndpoints | ||
{ | ||
/// <summary> | ||
/// 主域名(默认)。 | ||
/// </summary> | ||
public const string DEFAULT = "https://webcast.bytedance.com/api"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...IT.FlurlHttpClient.ByteDance.MicroApp/ExtendedSDK/Webcast/DouyinMicroAppWebcastRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast | ||
{ | ||
/// <summary> | ||
/// 抖音小程序直播小玩法 API 请求的基类。 | ||
/// </summary> | ||
public abstract class DouyinMicroAppWebcastRequest : CommonRequestBase, ICommonRequest | ||
{ | ||
/// <summary> | ||
/// 获取或设置抖音小程序的 AccessToken。 | ||
/// </summary> | ||
[Newtonsoft.Json.JsonIgnore] | ||
[System.Text.Json.Serialization.JsonIgnore] | ||
public virtual string? AccessToken { get; set; } | ||
} | ||
} |
Oops, something went wrong.