mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-04-05 17:37:54 +08:00
feat(wxapi): 构造器模式
This commit is contained in:
parent
4c007a0bfb
commit
6aca48428d
@ -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<Options.WechatOptions> 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;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.Api
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于构造 <see cref="WechatApiClient"/> 实例的构造器。
|
||||
/// </summary>
|
||||
public partial class WechatApiClientBuilder : ICommonClientBuilder<WechatApiClient>
|
||||
{
|
||||
private readonly WechatApiClientOptions _options;
|
||||
private readonly IList<Action<CommonClientSettings>> _configures;
|
||||
private readonly IList<HttpInterceptor> _interceptors;
|
||||
private HttpClient? _httpClient;
|
||||
private bool? _disposeClient;
|
||||
|
||||
private WechatApiClientBuilder(WechatApiClientOptions options)
|
||||
{
|
||||
_options = options;
|
||||
_configures = new List<Action<CommonClientSettings>>();
|
||||
_interceptors = new List<HttpInterceptor>();
|
||||
}
|
||||
|
||||
ICommonClientBuilder<WechatApiClient> ICommonClientBuilder<WechatApiClient>.ConfigureSettings(Action<CommonClientSettings> configure)
|
||||
{
|
||||
return ConfigureSettings(configure);
|
||||
}
|
||||
|
||||
ICommonClientBuilder<WechatApiClient> ICommonClientBuilder<WechatApiClient>.UseInterceptor(HttpInterceptor interceptor)
|
||||
{
|
||||
return UseInterceptor(interceptor);
|
||||
}
|
||||
|
||||
ICommonClientBuilder<WechatApiClient> ICommonClientBuilder<WechatApiClient>.UseHttpClient(HttpClient httpClient, bool disposeClient)
|
||||
{
|
||||
return UseHttpClient(httpClient, disposeClient);
|
||||
}
|
||||
|
||||
public WechatApiClientBuilder ConfigureSettings(Action<CommonClientSettings> 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<CommonClientSettings> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user