mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-10-04 10:21:13 +00:00
Split UserClientProvider into base class with RestClient and derived class also containing SocketClient
This commit is contained in:
@@ -14,21 +14,19 @@ using System.Text;
|
|||||||
namespace CryptoExchange.Net.Clients
|
namespace CryptoExchange.Net.Clients
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public abstract class UserClientProvider<TRestClient, TSocketClient, TRestOptions, TSocketOptions, TCredentials, TEnvironment>
|
public abstract class UserClientProvider<TRestClient, TRestOptions, TCredentials, TEnvironment>
|
||||||
where TRestClient : IRestClient<TCredentials>
|
where TRestClient : IRestClient<TCredentials>
|
||||||
where TSocketClient : ISocketClient<TCredentials>
|
|
||||||
where TRestOptions : RestExchangeOptions<TEnvironment, TCredentials>, new()
|
where TRestOptions : RestExchangeOptions<TEnvironment, TCredentials>, new()
|
||||||
where TSocketOptions : SocketExchangeOptions<TEnvironment, TCredentials>, new()
|
|
||||||
where TCredentials : ApiCredentials
|
where TCredentials : ApiCredentials
|
||||||
where TEnvironment : TradeEnvironment
|
where TEnvironment : TradeEnvironment
|
||||||
{
|
{
|
||||||
private ConcurrentDictionary<string, TRestClient> _restClients = new ConcurrentDictionary<string, TRestClient>();
|
private ConcurrentDictionary<string, TRestClient> _restClients = new ConcurrentDictionary<string, TRestClient>();
|
||||||
private ConcurrentDictionary<string, TSocketClient> _socketClients = new ConcurrentDictionary<string, TSocketClient>();
|
|
||||||
|
|
||||||
private readonly IOptions<TRestOptions> _restOptions;
|
private readonly IOptions<TRestOptions> _restOptions;
|
||||||
private readonly IOptions<TSocketOptions> _socketOptions;
|
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
private readonly ILoggerFactory? _loggerFactory;
|
/// <summary>
|
||||||
|
/// Logger factory
|
||||||
|
/// </summary>
|
||||||
|
protected readonly ILoggerFactory? _loggerFactory;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public abstract string ExchangeName { get; }
|
public abstract string ExchangeName { get; }
|
||||||
@@ -39,17 +37,14 @@ namespace CryptoExchange.Net.Clients
|
|||||||
public UserClientProvider(
|
public UserClientProvider(
|
||||||
HttpClient? httpClient,
|
HttpClient? httpClient,
|
||||||
ILoggerFactory? loggerFactory,
|
ILoggerFactory? loggerFactory,
|
||||||
IOptions<TRestOptions> restOptions,
|
IOptions<TRestOptions> restOptions)
|
||||||
IOptions<TSocketOptions> socketOptions)
|
|
||||||
{
|
{
|
||||||
_httpClient = httpClient ?? new HttpClient();
|
_httpClient = httpClient ?? new HttpClient();
|
||||||
_httpClient.Timeout = restOptions.Value.RequestTimeout;
|
_httpClient.Timeout = restOptions.Value.RequestTimeout;
|
||||||
_loggerFactory = loggerFactory;
|
_loggerFactory = loggerFactory;
|
||||||
_restOptions = restOptions;
|
_restOptions = restOptions;
|
||||||
_socketOptions = socketOptions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private IOptions<TRestOptions> SetRestEnvironment(IOptions<TRestOptions> options, TEnvironment? environment)
|
private IOptions<TRestOptions> SetRestEnvironment(IOptions<TRestOptions> options, TEnvironment? environment)
|
||||||
{
|
{
|
||||||
if (environment == null)
|
if (environment == null)
|
||||||
@@ -61,22 +56,10 @@ namespace CryptoExchange.Net.Clients
|
|||||||
return Options.Create(newRestClientOptions);
|
return Options.Create(newRestClientOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IOptions<TSocketOptions> SetSocketEnvironment(IOptions<TSocketOptions> options, TEnvironment? environment)
|
|
||||||
{
|
|
||||||
if (environment == null)
|
|
||||||
return options;
|
|
||||||
|
|
||||||
var newSocketClientOptions = new TSocketOptions();
|
|
||||||
options.Value.Set(newSocketClientOptions);
|
|
||||||
newSocketClientOptions.Environment = environment;
|
|
||||||
return Options.Create(newSocketClientOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void InitializeUserClient(string userIdentifier, TCredentials credentials, TEnvironment? environment = null)
|
public virtual void InitializeUserClient(string userIdentifier, TCredentials credentials, TEnvironment? environment = null)
|
||||||
{
|
{
|
||||||
CreateRestClient(userIdentifier, credentials, environment);
|
CreateRestClient(userIdentifier, credentials, environment);
|
||||||
CreateSocketClient(userIdentifier, credentials, environment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -88,15 +71,6 @@ namespace CryptoExchange.Net.Clients
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public TSocketClient GetSocketClient(string userIdentifier, TCredentials? credentials = null, TEnvironment? environment = null)
|
|
||||||
{
|
|
||||||
if (!_socketClients.TryGetValue(userIdentifier, out var client) || client.Disposed)
|
|
||||||
client = CreateSocketClient(userIdentifier, credentials, environment);
|
|
||||||
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
private TRestClient CreateRestClient(string userIdentifier, TCredentials? credentials, TEnvironment? environment)
|
private TRestClient CreateRestClient(string userIdentifier, TCredentials? credentials, TEnvironment? environment)
|
||||||
{
|
{
|
||||||
var clientRestOptions = SetRestEnvironment(_restOptions, environment);
|
var clientRestOptions = SetRestEnvironment(_restOptions, environment);
|
||||||
@@ -109,6 +83,94 @@ namespace CryptoExchange.Net.Clients
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a new instance of the rest client
|
||||||
|
/// </summary>
|
||||||
|
protected abstract TRestClient ConstructRestClient(
|
||||||
|
HttpClient client,
|
||||||
|
ILoggerFactory? loggerFactory,
|
||||||
|
IOptions<TRestOptions> options);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public virtual void ClearUserClients(string userIdentifier)
|
||||||
|
{
|
||||||
|
_restClients.TryRemove(userIdentifier, out var restClient);
|
||||||
|
restClient?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public virtual void Clear()
|
||||||
|
{
|
||||||
|
foreach (var client in _restClients.Values)
|
||||||
|
client.Dispose();
|
||||||
|
_restClients.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the provided options delegate to a new instance of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
protected static T ApplyOptionsDelegate<T>(Action<T>? del) where T : new()
|
||||||
|
{
|
||||||
|
var opts = new T();
|
||||||
|
del?.Invoke(opts);
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public abstract class UserClientProvider<TRestClient, TSocketClient, TRestOptions, TSocketOptions, TCredentials, TEnvironment>
|
||||||
|
: UserClientProvider<TRestClient, TRestOptions, TCredentials, TEnvironment>
|
||||||
|
where TRestClient : IRestClient<TCredentials>
|
||||||
|
where TSocketClient : ISocketClient<TCredentials>
|
||||||
|
where TRestOptions : RestExchangeOptions<TEnvironment, TCredentials>, new()
|
||||||
|
where TSocketOptions : SocketExchangeOptions<TEnvironment, TCredentials>, new()
|
||||||
|
where TCredentials : ApiCredentials
|
||||||
|
where TEnvironment : TradeEnvironment
|
||||||
|
{
|
||||||
|
private ConcurrentDictionary<string, TSocketClient> _socketClients = new ConcurrentDictionary<string, TSocketClient>();
|
||||||
|
private readonly IOptions<TSocketOptions> _socketOptions;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ctor
|
||||||
|
/// </summary>
|
||||||
|
public UserClientProvider(
|
||||||
|
HttpClient? httpClient,
|
||||||
|
ILoggerFactory? loggerFactory,
|
||||||
|
IOptions<TRestOptions> restOptions,
|
||||||
|
IOptions<TSocketOptions> socketOptions)
|
||||||
|
: base(httpClient, loggerFactory, restOptions)
|
||||||
|
{
|
||||||
|
_socketOptions = socketOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IOptions<TSocketOptions> SetSocketEnvironment(IOptions<TSocketOptions> options, TEnvironment? environment)
|
||||||
|
{
|
||||||
|
if (environment == null)
|
||||||
|
return options;
|
||||||
|
|
||||||
|
var newSocketClientOptions = new TSocketOptions();
|
||||||
|
options.Value.Set(newSocketClientOptions);
|
||||||
|
newSocketClientOptions.Environment = environment;
|
||||||
|
return Options.Create(newSocketClientOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void InitializeUserClient(string userIdentifier, TCredentials credentials, TEnvironment? environment = null)
|
||||||
|
{
|
||||||
|
base.InitializeUserClient(userIdentifier, credentials, environment);
|
||||||
|
CreateSocketClient(userIdentifier, credentials, environment);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public TSocketClient GetSocketClient(string userIdentifier, TCredentials? credentials = null, TEnvironment? environment = null)
|
||||||
|
{
|
||||||
|
if (!_socketClients.TryGetValue(userIdentifier, out var client) || client.Disposed)
|
||||||
|
client = CreateSocketClient(userIdentifier, credentials, environment);
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
private TSocketClient CreateSocketClient(string userIdentifier, TCredentials? credentials, TEnvironment? environment)
|
private TSocketClient CreateSocketClient(string userIdentifier, TCredentials? credentials, TEnvironment? environment)
|
||||||
{
|
{
|
||||||
var clientSocketOptions = SetSocketEnvironment(_socketOptions, environment);
|
var clientSocketOptions = SetSocketEnvironment(_socketOptions, environment);
|
||||||
@@ -121,15 +183,6 @@ namespace CryptoExchange.Net.Clients
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructs a new instance of the rest client
|
|
||||||
/// </summary>
|
|
||||||
protected abstract TRestClient ConstructRestClient(
|
|
||||||
HttpClient client,
|
|
||||||
ILoggerFactory? loggerFactory,
|
|
||||||
IOptions<TRestOptions> options);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructs a new instance of the socket client
|
/// Constructs a new instance of the socket client
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -137,36 +190,23 @@ namespace CryptoExchange.Net.Clients
|
|||||||
ILoggerFactory? loggerFactory,
|
ILoggerFactory? loggerFactory,
|
||||||
IOptions<TSocketOptions> options);
|
IOptions<TSocketOptions> options);
|
||||||
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void ClearUserClients(string userIdentifier)
|
public override void ClearUserClients(string userIdentifier)
|
||||||
{
|
{
|
||||||
_restClients.TryRemove(userIdentifier, out var restClient);
|
base.ClearUserClients(userIdentifier);
|
||||||
|
|
||||||
_socketClients.TryRemove(userIdentifier, out var socketClient);
|
_socketClients.TryRemove(userIdentifier, out var socketClient);
|
||||||
restClient?.Dispose();
|
|
||||||
socketClient?.Dispose();
|
socketClient?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Clear()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
foreach (var client in _restClients.Values)
|
base.Clear();
|
||||||
client.Dispose();
|
|
||||||
_restClients.Clear();
|
|
||||||
|
|
||||||
foreach (var client in _socketClients.Values)
|
foreach (var client in _socketClients.Values)
|
||||||
client.Dispose();
|
client.Dispose();
|
||||||
_socketClients.Clear();
|
_socketClients.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Applies the provided options delegate to a new instance of the specified type.
|
|
||||||
/// </summary>
|
|
||||||
protected static T ApplyOptionsDelegate<T>(Action<T>? del) where T : new()
|
|
||||||
{
|
|
||||||
var opts = new T();
|
|
||||||
del?.Invoke(opts);
|
|
||||||
return opts;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user