diff --git a/CryptoExchange.Net/Clients/UserClientProvider.cs b/CryptoExchange.Net/Clients/UserClientProvider.cs index c6cb2232..d71f56a0 100644 --- a/CryptoExchange.Net/Clients/UserClientProvider.cs +++ b/CryptoExchange.Net/Clients/UserClientProvider.cs @@ -14,21 +14,19 @@ using System.Text; namespace CryptoExchange.Net.Clients { /// - public abstract class UserClientProvider + public abstract class UserClientProvider where TRestClient : IRestClient - where TSocketClient : ISocketClient where TRestOptions : RestExchangeOptions, new() - where TSocketOptions : SocketExchangeOptions, new() where TCredentials : ApiCredentials where TEnvironment : TradeEnvironment { private ConcurrentDictionary _restClients = new ConcurrentDictionary(); - private ConcurrentDictionary _socketClients = new ConcurrentDictionary(); - private readonly IOptions _restOptions; - private readonly IOptions _socketOptions; private readonly HttpClient _httpClient; - private readonly ILoggerFactory? _loggerFactory; + /// + /// Logger factory + /// + protected readonly ILoggerFactory? _loggerFactory; /// public abstract string ExchangeName { get; } @@ -39,17 +37,14 @@ namespace CryptoExchange.Net.Clients public UserClientProvider( HttpClient? httpClient, ILoggerFactory? loggerFactory, - IOptions restOptions, - IOptions socketOptions) + IOptions restOptions) { _httpClient = httpClient ?? new HttpClient(); _httpClient.Timeout = restOptions.Value.RequestTimeout; _loggerFactory = loggerFactory; _restOptions = restOptions; - _socketOptions = socketOptions; } - private IOptions SetRestEnvironment(IOptions options, TEnvironment? environment) { if (environment == null) @@ -61,22 +56,10 @@ namespace CryptoExchange.Net.Clients return Options.Create(newRestClientOptions); } - private IOptions SetSocketEnvironment(IOptions options, TEnvironment? environment) - { - if (environment == null) - return options; - - var newSocketClientOptions = new TSocketOptions(); - options.Value.Set(newSocketClientOptions); - newSocketClientOptions.Environment = environment; - return Options.Create(newSocketClientOptions); - } - /// - 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); - CreateSocketClient(userIdentifier, credentials, environment); } /// @@ -88,15 +71,6 @@ namespace CryptoExchange.Net.Clients return client; } - /// - 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) { var clientRestOptions = SetRestEnvironment(_restOptions, environment); @@ -109,6 +83,94 @@ namespace CryptoExchange.Net.Clients return client; } + /// + /// Constructs a new instance of the rest client + /// + protected abstract TRestClient ConstructRestClient( + HttpClient client, + ILoggerFactory? loggerFactory, + IOptions options); + + /// + public virtual void ClearUserClients(string userIdentifier) + { + _restClients.TryRemove(userIdentifier, out var restClient); + restClient?.Dispose(); + } + + /// + public virtual void Clear() + { + foreach (var client in _restClients.Values) + client.Dispose(); + _restClients.Clear(); + } + + /// + /// Applies the provided options delegate to a new instance of the specified type. + /// + protected static T ApplyOptionsDelegate(Action? del) where T : new() + { + var opts = new T(); + del?.Invoke(opts); + return opts; + } + } + + /// + public abstract class UserClientProvider + : UserClientProvider + where TRestClient : IRestClient + where TSocketClient : ISocketClient + where TRestOptions : RestExchangeOptions, new() + where TSocketOptions : SocketExchangeOptions, new() + where TCredentials : ApiCredentials + where TEnvironment : TradeEnvironment + { + private ConcurrentDictionary _socketClients = new ConcurrentDictionary(); + private readonly IOptions _socketOptions; + + + /// + /// ctor + /// + public UserClientProvider( + HttpClient? httpClient, + ILoggerFactory? loggerFactory, + IOptions restOptions, + IOptions socketOptions) + : base(httpClient, loggerFactory, restOptions) + { + _socketOptions = socketOptions; + } + + private IOptions SetSocketEnvironment(IOptions options, TEnvironment? environment) + { + if (environment == null) + return options; + + var newSocketClientOptions = new TSocketOptions(); + options.Value.Set(newSocketClientOptions); + newSocketClientOptions.Environment = environment; + return Options.Create(newSocketClientOptions); + } + + /// + public override void InitializeUserClient(string userIdentifier, TCredentials credentials, TEnvironment? environment = null) + { + base.InitializeUserClient(userIdentifier, credentials, environment); + CreateSocketClient(userIdentifier, credentials, environment); + } + + /// + 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) { var clientSocketOptions = SetSocketEnvironment(_socketOptions, environment); @@ -121,15 +183,6 @@ namespace CryptoExchange.Net.Clients return client; } - /// - /// Constructs a new instance of the rest client - /// - protected abstract TRestClient ConstructRestClient( - HttpClient client, - ILoggerFactory? loggerFactory, - IOptions options); - - /// /// Constructs a new instance of the socket client /// @@ -137,36 +190,23 @@ namespace CryptoExchange.Net.Clients ILoggerFactory? loggerFactory, IOptions options); - /// - 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); - restClient?.Dispose(); socketClient?.Dispose(); } /// - public void Clear() + public override void Clear() { - foreach (var client in _restClients.Values) - client.Dispose(); - _restClients.Clear(); + base.Clear(); foreach (var client in _socketClients.Values) client.Dispose(); _socketClients.Clear(); } - - /// - /// Applies the provided options delegate to a new instance of the specified type. - /// - protected static T ApplyOptionsDelegate(Action? del) where T : new() - { - var opts = new T(); - del?.Invoke(opts); - return opts; - } } }