From 6aca48428d957d3a98a0a2085eaee50181095b3f Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 6 Feb 2024 14:25:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(wxapi):=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 --- .../Implements/WechatApiClientFactory.cs | 8 +- .../WechatApiClientBuilder.cs | 94 +++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClientBuilder.cs diff --git a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6/Services/HttpClients/Implements/WechatApiClientFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6/Services/HttpClients/Implements/WechatApiClientFactory.cs index 4faecb67..46ff5f85 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6/Services/HttpClients/Implements/WechatApiClientFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6/Services/HttpClients/Implements/WechatApiClientFactory.cs @@ -1,16 +1,20 @@ using System; using System.Linq; +using System.Net.Http; using Microsoft.Extensions.Options; namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements { internal partial class WechatApiClientFactory : IWechatApiClientFactory { + private readonly IHttpClientFactory _httpClientFactory; private readonly Options.WechatOptions _wechatOptions; public WechatApiClientFactory( + IHttpClientFactory httpClientFactory, IOptions wechatOptions) { + _httpClientFactory = httpClientFactory; _wechatOptions = wechatOptions.Value; } @@ -31,7 +35,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements PushEncodingAESKey = _wechatOptions.CallbackEncodingAESKey, PushToken = _wechatOptions.CallbackToken }; - var wechatApiClient = new WechatApiClient(wechatApiClientOptions); + var wechatApiClient = WechatApiClientBuilder.Create(wechatApiClientOptions) + .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) + .Build(); return wechatApiClient; } } diff --git a/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClientBuilder.cs b/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClientBuilder.cs new file mode 100644 index 00000000..a7f14723 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.Api/WechatApiClientBuilder.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; + +namespace SKIT.FlurlHttpClient.Wechat.Api +{ + /// + /// 用于构造 实例的构造器。 + /// + public partial class WechatApiClientBuilder : ICommonClientBuilder + { + private readonly WechatApiClientOptions _options; + private readonly IList> _configures; + private readonly IList _interceptors; + private HttpClient? _httpClient; + private bool? _disposeClient; + + private WechatApiClientBuilder(WechatApiClientOptions 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 WechatApiClientBuilder ConfigureSettings(Action configure) + { + if (configure is null) throw new ArgumentNullException(nameof(configure)); + + _configures.Add(configure); + return this; + } + + public WechatApiClientBuilder UseInterceptor(HttpInterceptor interceptor) + { + if (interceptor is null) throw new ArgumentNullException(nameof(interceptor)); + + _interceptors.Add(interceptor); + return this; + } + + public WechatApiClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true) + { + if (httpClient is null) throw new ArgumentNullException(nameof(httpClient)); + + _httpClient = httpClient; + _disposeClient = disposeClient; + return this; + } + + public WechatApiClient Build() + { + WechatApiClient client = _disposeClient.HasValue + ? new WechatApiClient(_options, _httpClient, _disposeClient.Value) + : new WechatApiClient(_options, _httpClient); + + foreach (Action configure in _configures) + { + client.Configure(configure); + } + + foreach (HttpInterceptor interceptor in _interceptors) + { + client.Interceptors.Add(interceptor); + } + + return client; + } + } + + partial class WechatApiClientBuilder + { + public static WechatApiClientBuilder Create(WechatApiClientOptions options) + { + if (options is null) throw new ArgumentNullException(nameof(options)); + + return new WechatApiClientBuilder(options); + } + } +}