From 2428ab36cac252ebf532d0c8824b12af138ddc4a Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 29 Feb 2024 21:58:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(tiktok):=20=E6=9E=84=E9=80=A0=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TikTokV2ClientBuilder.cs | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/SKIT.FlurlHttpClient.ByteDance.TikTokGlobal/TikTokV2ClientBuilder.cs 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); + } + } +}