|
| 1 | +using HTX.Net.Interfaces.Clients; |
| 2 | +using HTX.Net.Objects.Options; |
| 3 | +using CryptoExchange.Net.Authentication; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using System; |
| 8 | +using System.Collections.Concurrent; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Collections.Generic; |
| 11 | + |
| 12 | +namespace HTX.Net.Clients |
| 13 | +{ |
| 14 | + /// <inheritdoc /> |
| 15 | + public class HTXUserClientProvider : IHTXUserClientProvider |
| 16 | + { |
| 17 | + private static ConcurrentDictionary<string, IHTXRestClient> _restClients = new ConcurrentDictionary<string, IHTXRestClient>(); |
| 18 | + private static ConcurrentDictionary<string, IHTXSocketClient> _socketClients = new ConcurrentDictionary<string, IHTXSocketClient>(); |
| 19 | + |
| 20 | + private readonly IOptions<HTXRestOptions> _restOptions; |
| 21 | + private readonly IOptions<HTXSocketOptions> _socketOptions; |
| 22 | + private readonly HttpClient _httpClient; |
| 23 | + private readonly ILoggerFactory? _loggerFactory; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// ctor |
| 27 | + /// </summary> |
| 28 | + /// <param name="optionsDelegate">Options to use for created clients</param> |
| 29 | + public HTXUserClientProvider(Action<HTXOptions>? optionsDelegate = null) |
| 30 | + : this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate).Rest), Options.Create(ApplyOptionsDelegate(optionsDelegate).Socket)) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// ctor |
| 36 | + /// </summary> |
| 37 | + public HTXUserClientProvider( |
| 38 | + HttpClient? httpClient, |
| 39 | + ILoggerFactory? loggerFactory, |
| 40 | + IOptions<HTXRestOptions> restOptions, |
| 41 | + IOptions<HTXSocketOptions> socketOptions) |
| 42 | + { |
| 43 | + _httpClient = httpClient ?? new HttpClient(); |
| 44 | + _loggerFactory = loggerFactory; |
| 45 | + _restOptions = restOptions; |
| 46 | + _socketOptions = socketOptions; |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc /> |
| 50 | + public void InitializeUserClient(string userIdentifier, ApiCredentials credentials, HTXEnvironment? environment = null) |
| 51 | + { |
| 52 | + CreateRestClient(userIdentifier, credentials, environment); |
| 53 | + CreateSocketClient(userIdentifier, credentials, environment); |
| 54 | + } |
| 55 | + |
| 56 | + /// <inheritdoc /> |
| 57 | + public IHTXRestClient GetRestClient(string userIdentifier, ApiCredentials? credentials = null, HTXEnvironment? environment = null) |
| 58 | + { |
| 59 | + if (!_restClients.TryGetValue(userIdentifier, out var client)) |
| 60 | + client = CreateRestClient(userIdentifier, credentials, environment); |
| 61 | + |
| 62 | + return client; |
| 63 | + } |
| 64 | + |
| 65 | + /// <inheritdoc /> |
| 66 | + public IHTXSocketClient GetSocketClient(string userIdentifier, ApiCredentials? credentials = null, HTXEnvironment? environment = null) |
| 67 | + { |
| 68 | + if (!_socketClients.TryGetValue(userIdentifier, out var client)) |
| 69 | + client = CreateSocketClient(userIdentifier, credentials, environment); |
| 70 | + |
| 71 | + return client; |
| 72 | + } |
| 73 | + |
| 74 | + private IHTXRestClient CreateRestClient(string userIdentifier, ApiCredentials? credentials, HTXEnvironment? environment) |
| 75 | + { |
| 76 | + var clientRestOptions = SetRestEnvironment(environment); |
| 77 | + var client = new HTXRestClient(_httpClient, _loggerFactory, clientRestOptions); |
| 78 | + if (credentials != null) |
| 79 | + { |
| 80 | + client.SetApiCredentials(credentials); |
| 81 | + _restClients.TryAdd(userIdentifier, client); |
| 82 | + } |
| 83 | + return client; |
| 84 | + } |
| 85 | + |
| 86 | + private IHTXSocketClient CreateSocketClient(string userIdentifier, ApiCredentials? credentials, HTXEnvironment? environment) |
| 87 | + { |
| 88 | + var clientSocketOptions = SetSocketEnvironment(environment); |
| 89 | + var client = new HTXSocketClient(clientSocketOptions!, _loggerFactory); |
| 90 | + if (credentials != null) |
| 91 | + { |
| 92 | + client.SetApiCredentials(credentials); |
| 93 | + _socketClients.TryAdd(userIdentifier, client); |
| 94 | + } |
| 95 | + return client; |
| 96 | + } |
| 97 | + |
| 98 | + private IOptions<HTXRestOptions> SetRestEnvironment(HTXEnvironment? environment) |
| 99 | + { |
| 100 | + if (environment == null) |
| 101 | + return _restOptions; |
| 102 | + |
| 103 | + var newRestClientOptions = new HTXRestOptions(); |
| 104 | + var restOptions = _restOptions.Value.Set(newRestClientOptions); |
| 105 | + newRestClientOptions.Environment = environment; |
| 106 | + return Options.Create(newRestClientOptions); |
| 107 | + } |
| 108 | + |
| 109 | + private IOptions<HTXSocketOptions> SetSocketEnvironment(HTXEnvironment? environment) |
| 110 | + { |
| 111 | + if (environment == null) |
| 112 | + return _socketOptions; |
| 113 | + |
| 114 | + var newSocketClientOptions = new HTXSocketOptions(); |
| 115 | + var restOptions = _socketOptions.Value.Set(newSocketClientOptions); |
| 116 | + newSocketClientOptions.Environment = environment; |
| 117 | + return Options.Create(newSocketClientOptions); |
| 118 | + } |
| 119 | + |
| 120 | + private static T ApplyOptionsDelegate<T>(Action<T>? del) where T : new() |
| 121 | + { |
| 122 | + var opts = new T(); |
| 123 | + del?.Invoke(opts); |
| 124 | + return opts; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments