Skip to content

Commit

Permalink
feat(microapp): 构造器模式
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed Mar 5, 2024
1 parent 16c5f69 commit dadc442
Show file tree
Hide file tree
Showing 4 changed files with 376 additions and 0 deletions.
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
{
/// <summary>
/// 用于构造 <see cref="DouyinMicroAppClient"/> 实例的构造器。
/// </summary>
public partial class DouyinMicroAppClientBuilder : ICommonClientBuilder<DouyinMicroAppClient>
{
private readonly DouyinMicroAppClientOptions _options;
private readonly IList<Action<CommonClientSettings>> _configures;
private readonly IList<HttpInterceptor> _interceptors;
private HttpClient? _httpClient;
private bool? _disposeClient;

private DouyinMicroAppClientBuilder(DouyinMicroAppClientOptions options)
{
_options = options;
_configures = new List<Action<CommonClientSettings>>();
_interceptors = new List<HttpInterceptor>();
}

ICommonClientBuilder<DouyinMicroAppClient> ICommonClientBuilder<DouyinMicroAppClient>.ConfigureSettings(Action<CommonClientSettings> configure)
{
return ConfigureSettings(configure);
}

ICommonClientBuilder<DouyinMicroAppClient> ICommonClientBuilder<DouyinMicroAppClient>.UseInterceptor(HttpInterceptor interceptor)
{
return UseInterceptor(interceptor);
}

ICommonClientBuilder<DouyinMicroAppClient> ICommonClientBuilder<DouyinMicroAppClient>.UseHttpClient(HttpClient httpClient, bool disposeClient)
{
return UseHttpClient(httpClient, disposeClient);
}

public DouyinMicroAppClientBuilder ConfigureSettings(Action<CommonClientSettings> configure)
{
if (configure is null) throw new ArgumentNullException(nameof(configure));

_configures.Add(configure);
return this;
}

public DouyinMicroAppClientBuilder UseInterceptor(HttpInterceptor interceptor)
{
if (interceptor is null) throw new ArgumentNullException(nameof(interceptor));

_interceptors.Add(interceptor);
return this;
}

public DouyinMicroAppClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true)
{
if (httpClient is null) throw new ArgumentNullException(nameof(httpClient));

_httpClient = httpClient;
_disposeClient = disposeClient;
return this;
}

public DouyinMicroAppClient Build()
{
DouyinMicroAppClient client = _disposeClient.HasValue
? new DouyinMicroAppClient(_options, _httpClient, _disposeClient.Value)
: new DouyinMicroAppClient(_options, _httpClient);

foreach (Action<CommonClientSettings> configure in _configures)
{
client.Configure(configure);
}

foreach (HttpInterceptor interceptor in _interceptors)
{
client.Interceptors.Add(interceptor);
}

return client;
}
}

partial class DouyinMicroAppClientBuilder
{
public static DouyinMicroAppClientBuilder Create(DouyinMicroAppClientOptions options)
{
if (options is null) throw new ArgumentNullException(nameof(options));

return new DouyinMicroAppClientBuilder(options);
}
}
}
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.OpenApi
{
/// <summary>
/// 用于构造 <see cref="DouyinMicroAppOpenApiClient"/> 实例的构造器。
/// </summary>
public partial class DouyinMicroAppOpenApiClientBuilder : ICommonClientBuilder<DouyinMicroAppOpenApiClient>
{
private readonly DouyinMicroAppOpenApiClientOptions _options;
private readonly IList<Action<CommonClientSettings>> _configures;
private readonly IList<HttpInterceptor> _interceptors;
private HttpClient? _httpClient;
private bool? _disposeClient;

private DouyinMicroAppOpenApiClientBuilder(DouyinMicroAppOpenApiClientOptions options)
{
_options = options;
_configures = new List<Action<CommonClientSettings>>();
_interceptors = new List<HttpInterceptor>();
}

ICommonClientBuilder<DouyinMicroAppOpenApiClient> ICommonClientBuilder<DouyinMicroAppOpenApiClient>.ConfigureSettings(Action<CommonClientSettings> configure)
{
return ConfigureSettings(configure);
}

ICommonClientBuilder<DouyinMicroAppOpenApiClient> ICommonClientBuilder<DouyinMicroAppOpenApiClient>.UseInterceptor(HttpInterceptor interceptor)
{
return UseInterceptor(interceptor);
}

ICommonClientBuilder<DouyinMicroAppOpenApiClient> ICommonClientBuilder<DouyinMicroAppOpenApiClient>.UseHttpClient(HttpClient httpClient, bool disposeClient)
{
return UseHttpClient(httpClient, disposeClient);
}

public DouyinMicroAppOpenApiClientBuilder ConfigureSettings(Action<CommonClientSettings> configure)
{
if (configure is null) throw new ArgumentNullException(nameof(configure));

_configures.Add(configure);
return this;
}

public DouyinMicroAppOpenApiClientBuilder UseInterceptor(HttpInterceptor interceptor)
{
if (interceptor is null) throw new ArgumentNullException(nameof(interceptor));

_interceptors.Add(interceptor);
return this;
}

public DouyinMicroAppOpenApiClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true)
{
if (httpClient is null) throw new ArgumentNullException(nameof(httpClient));

_httpClient = httpClient;
_disposeClient = disposeClient;
return this;
}

public DouyinMicroAppOpenApiClient Build()
{
DouyinMicroAppOpenApiClient client = _disposeClient.HasValue
? new DouyinMicroAppOpenApiClient(_options, _httpClient, _disposeClient.Value)
: new DouyinMicroAppOpenApiClient(_options, _httpClient);

foreach (Action<CommonClientSettings> configure in _configures)
{
client.Configure(configure);
}

foreach (HttpInterceptor interceptor in _interceptors)
{
client.Interceptors.Add(interceptor);
}

return client;
}
}

partial class DouyinMicroAppOpenApiClientBuilder
{
public static DouyinMicroAppOpenApiClientBuilder Create(DouyinMicroAppOpenApiClientOptions options)
{
if (options is null) throw new ArgumentNullException(nameof(options));

return new DouyinMicroAppOpenApiClientBuilder(options);
}
}
}
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.ProductApi
{
/// <summary>
/// 用于构造 <see cref="DouyinMicroAppProductApiClient"/> 实例的构造器。
/// </summary>
public partial class DouyinMicroAppProductApiClientBuilder : ICommonClientBuilder<DouyinMicroAppProductApiClient>
{
private readonly DouyinMicroAppProductApiClientOptions _options;
private readonly IList<Action<CommonClientSettings>> _configures;
private readonly IList<HttpInterceptor> _interceptors;
private HttpClient? _httpClient;
private bool? _disposeClient;

private DouyinMicroAppProductApiClientBuilder(DouyinMicroAppProductApiClientOptions options)
{
_options = options;
_configures = new List<Action<CommonClientSettings>>();
_interceptors = new List<HttpInterceptor>();
}

ICommonClientBuilder<DouyinMicroAppProductApiClient> ICommonClientBuilder<DouyinMicroAppProductApiClient>.ConfigureSettings(Action<CommonClientSettings> configure)
{
return ConfigureSettings(configure);
}

ICommonClientBuilder<DouyinMicroAppProductApiClient> ICommonClientBuilder<DouyinMicroAppProductApiClient>.UseInterceptor(HttpInterceptor interceptor)
{
return UseInterceptor(interceptor);
}

ICommonClientBuilder<DouyinMicroAppProductApiClient> ICommonClientBuilder<DouyinMicroAppProductApiClient>.UseHttpClient(HttpClient httpClient, bool disposeClient)
{
return UseHttpClient(httpClient, disposeClient);
}

public DouyinMicroAppProductApiClientBuilder ConfigureSettings(Action<CommonClientSettings> configure)
{
if (configure is null) throw new ArgumentNullException(nameof(configure));

_configures.Add(configure);
return this;
}

public DouyinMicroAppProductApiClientBuilder UseInterceptor(HttpInterceptor interceptor)
{
if (interceptor is null) throw new ArgumentNullException(nameof(interceptor));

_interceptors.Add(interceptor);
return this;
}

public DouyinMicroAppProductApiClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true)
{
if (httpClient is null) throw new ArgumentNullException(nameof(httpClient));

_httpClient = httpClient;
_disposeClient = disposeClient;
return this;
}

public DouyinMicroAppProductApiClient Build()
{
DouyinMicroAppProductApiClient client = _disposeClient.HasValue
? new DouyinMicroAppProductApiClient(_options, _httpClient, _disposeClient.Value)
: new DouyinMicroAppProductApiClient(_options, _httpClient);

foreach (Action<CommonClientSettings> configure in _configures)
{
client.Configure(configure);
}

foreach (HttpInterceptor interceptor in _interceptors)
{
client.Interceptors.Add(interceptor);
}

return client;
}
}

partial class DouyinMicroAppProductApiClientBuilder
{
public static DouyinMicroAppProductApiClientBuilder Create(DouyinMicroAppProductApiClientOptions options)
{
if (options is null) throw new ArgumentNullException(nameof(options));

return new DouyinMicroAppProductApiClientBuilder(options);
}
}
}
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.RoleApi
{
/// <summary>
/// 用于构造 <see cref="DouyinMicroAppRoleApiClient"/> 实例的构造器。
/// </summary>
public partial class DouyinMicroAppRoleApiClientBuilder : ICommonClientBuilder<DouyinMicroAppRoleApiClient>
{
private readonly DouyinMicroAppRoleApiClientOptions _options;
private readonly IList<Action<CommonClientSettings>> _configures;
private readonly IList<HttpInterceptor> _interceptors;
private HttpClient? _httpClient;
private bool? _disposeClient;

private DouyinMicroAppRoleApiClientBuilder(DouyinMicroAppRoleApiClientOptions options)
{
_options = options;
_configures = new List<Action<CommonClientSettings>>();
_interceptors = new List<HttpInterceptor>();
}

ICommonClientBuilder<DouyinMicroAppRoleApiClient> ICommonClientBuilder<DouyinMicroAppRoleApiClient>.ConfigureSettings(Action<CommonClientSettings> configure)
{
return ConfigureSettings(configure);
}

ICommonClientBuilder<DouyinMicroAppRoleApiClient> ICommonClientBuilder<DouyinMicroAppRoleApiClient>.UseInterceptor(HttpInterceptor interceptor)
{
return UseInterceptor(interceptor);
}

ICommonClientBuilder<DouyinMicroAppRoleApiClient> ICommonClientBuilder<DouyinMicroAppRoleApiClient>.UseHttpClient(HttpClient httpClient, bool disposeClient)
{
return UseHttpClient(httpClient, disposeClient);
}

public DouyinMicroAppRoleApiClientBuilder ConfigureSettings(Action<CommonClientSettings> configure)
{
if (configure is null) throw new ArgumentNullException(nameof(configure));

_configures.Add(configure);
return this;
}

public DouyinMicroAppRoleApiClientBuilder UseInterceptor(HttpInterceptor interceptor)
{
if (interceptor is null) throw new ArgumentNullException(nameof(interceptor));

_interceptors.Add(interceptor);
return this;
}

public DouyinMicroAppRoleApiClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true)
{
if (httpClient is null) throw new ArgumentNullException(nameof(httpClient));

_httpClient = httpClient;
_disposeClient = disposeClient;
return this;
}

public DouyinMicroAppRoleApiClient Build()
{
DouyinMicroAppRoleApiClient client = _disposeClient.HasValue
? new DouyinMicroAppRoleApiClient(_options, _httpClient, _disposeClient.Value)
: new DouyinMicroAppRoleApiClient(_options, _httpClient);

foreach (Action<CommonClientSettings> configure in _configures)
{
client.Configure(configure);
}

foreach (HttpInterceptor interceptor in _interceptors)
{
client.Interceptors.Add(interceptor);
}

return client;
}
}

partial class DouyinMicroAppRoleApiClientBuilder
{
public static DouyinMicroAppRoleApiClientBuilder Create(DouyinMicroAppRoleApiClientOptions options)
{
if (options is null) throw new ArgumentNullException(nameof(options));

return new DouyinMicroAppRoleApiClientBuilder(options);
}
}
}

0 comments on commit dadc442

Please sign in to comment.