diff --git a/src/SKIT.FlurlHttpClient.ByteDance.TikTokGlobal/TikTokV2ClientBuilder.cs b/src/SKIT.FlurlHttpClient.ByteDance.TikTokGlobal/TikTokV2ClientBuilder.cs new file mode 100644 index 00000000..e39310dc --- /dev/null +++ b/src/SKIT.FlurlHttpClient.ByteDance.TikTokGlobal/TikTokV2ClientBuilder.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; + +namespace SKIT.FlurlHttpClient.ByteDance.TikTokGlobal +{ + /// + /// 用于构造 实例的构造器。 + /// + public partial class TikTokV2ClientBuilder : ICommonClientBuilder + { + private readonly TikTokV2ClientOptions _options; + private readonly IList> _configures; + private readonly IList _interceptors; + private HttpClient? _httpClient; + private bool? _disposeClient; + + private TikTokV2ClientBuilder(TikTokV2ClientOptions options) + { + _options = options; + _configures = new List>(); + _interceptors = new List(); + } + + ICommonClientBuilder ICommonClientBuilder.ConfigureSettings(Action configure) + { + return ConfigureSettings(configure); + } + + ICommonClientBuilder ICommonClientBuilder.UseInterceptor(HttpInterceptor interceptor) + { + return UseInterceptor(interceptor); + } + + ICommonClientBuilder ICommonClientBuilder.UseHttpClient(HttpClient httpClient, bool disposeClient) + { + return UseHttpClient(httpClient, disposeClient); + } + + public TikTokV2ClientBuilder ConfigureSettings(Action configure) + { + if (configure is null) throw new ArgumentNullException(nameof(configure)); + + _configures.Add(configure); + return this; + } + + public TikTokV2ClientBuilder UseInterceptor(HttpInterceptor interceptor) + { + if (interceptor is null) throw new ArgumentNullException(nameof(interceptor)); + + _interceptors.Add(interceptor); + return this; + } + + public TikTokV2ClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true) + { + if (httpClient is null) throw new ArgumentNullException(nameof(httpClient)); + + _httpClient = httpClient; + _disposeClient = disposeClient; + return this; + } + + public TikTokV2Client Build() + { + TikTokV2Client client = _disposeClient.HasValue + ? new TikTokV2Client(_options, _httpClient, _disposeClient.Value) + : new TikTokV2Client(_options, _httpClient); + + foreach (Action configure in _configures) + { + client.Configure(configure); + } + + foreach (HttpInterceptor interceptor in _interceptors) + { + client.Interceptors.Add(interceptor); + } + + return client; + } + } + + partial class TikTokV2ClientBuilder + { + public static TikTokV2ClientBuilder Create(TikTokV2ClientOptions options) + { + if (options is null) throw new ArgumentNullException(nameof(options)); + + return new TikTokV2ClientBuilder(options); + } + } +}