diff --git a/CryptoExchange.Net/Clients/BaseSocketClient.cs b/CryptoExchange.Net/Clients/BaseSocketClient.cs index 61c877e..e817282 100644 --- a/CryptoExchange.Net/Clients/BaseSocketClient.cs +++ b/CryptoExchange.Net/Clients/BaseSocketClient.cs @@ -101,7 +101,7 @@ namespace CryptoExchange.Net public string GetSubscriptionsState() { var result = new StringBuilder(); - foreach (var client in ApiClients.OfType()) + foreach (var client in ApiClients.OfType().Where(c => c.CurrentSubscriptions > 0)) { result.AppendLine(client.GetSubscriptionsState()); } diff --git a/CryptoExchange.Net/Clients/CryptoBaseClient.cs b/CryptoExchange.Net/Clients/CryptoBaseClient.cs new file mode 100644 index 0000000..d7db31e --- /dev/null +++ b/CryptoExchange.Net/Clients/CryptoBaseClient.cs @@ -0,0 +1,75 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + /// + /// Base crypto client + /// + public class CryptoBaseClient : IDisposable + { + private Dictionary _serviceCache = new Dictionary(); + + /// + /// Service provider + /// + protected readonly IServiceProvider? _serviceProvider; + + /// + /// ctor + /// + public CryptoBaseClient() { } + + /// + /// ctor + /// + /// + public CryptoBaseClient(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + _serviceCache = new Dictionary(); + } + + /// + /// Try get a client by type for the service collection + /// + /// + /// + public T TryGet(Func createFunc) + { + if (_serviceProvider == null) + { + // Create with default options + var createResult = createFunc(); + _serviceCache.Add(typeof(T), createResult!); + return createResult; + } + + var type = typeof(T); + if (_serviceCache.TryGetValue(type, out var value)) + return (T)value; + + var result = _serviceProvider.GetRequiredService(); + if (result == null) + { + // Does this mean the AddXX() hasn't been done? + } + + _serviceCache.Add(type, result!); + return result; + } + + /// + /// Dispose + /// + public void Dispose() + { + _serviceCache.Clear(); + } + } +} diff --git a/CryptoExchange.Net/Clients/CryptoExchangeClient.cs b/CryptoExchange.Net/Clients/CryptoExchangeClient.cs deleted file mode 100644 index 61b3874..0000000 --- a/CryptoExchange.Net/Clients/CryptoExchangeClient.cs +++ /dev/null @@ -1,44 +0,0 @@ -using CryptoExchange.Net.Interfaces; -using CryptoExchange.Net.Interfaces.CommonClients; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace CryptoExchange.Net.Clients -{ - public class CryptoExchangeClient : ICryptoExchangeClient, IDisposable - { - private Dictionary _serviceCache = new Dictionary(); - - private readonly IServiceProvider _serviceProvider; - - public CryptoExchangeClient(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - _serviceCache = new Dictionary(); - } - - public IEnumerable GetSpotClients() - { - return _serviceProvider.GetServices().ToList(); - } - - public T? TryGet() - { - var type = typeof(T); - if (_serviceCache.TryGetValue(type, out var value)) - return (T?)value; - - var result = _serviceProvider.GetService(); - _serviceCache.Add(type, result); - return result; - } - - public void Dispose() - { - _serviceCache.Clear(); - } - } -} diff --git a/CryptoExchange.Net/Clients/CryptoRestClient.cs b/CryptoExchange.Net/Clients/CryptoRestClient.cs new file mode 100644 index 0000000..7b51222 --- /dev/null +++ b/CryptoExchange.Net/Clients/CryptoRestClient.cs @@ -0,0 +1,34 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + /// + public class CryptoRestClient : CryptoBaseClient, ICryptoRestClient + { + /// + /// ctor + /// + /// + public CryptoRestClient(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + + /// + /// Try get a client by type for the service collection + /// + /// + public IEnumerable GetSpotClients() + { + if (_serviceProvider == null) + return new List(); + + return _serviceProvider.GetServices().ToList(); + } + } +} diff --git a/CryptoExchange.Net/Clients/CryptoSocketClient.cs b/CryptoExchange.Net/Clients/CryptoSocketClient.cs new file mode 100644 index 0000000..28af548 --- /dev/null +++ b/CryptoExchange.Net/Clients/CryptoSocketClient.cs @@ -0,0 +1,22 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + /// + public class CryptoSocketClient : CryptoBaseClient, ICryptoSocketClient + { + /// + /// ctor + /// + /// + public CryptoSocketClient(IServiceProvider serviceProvider) : base(serviceProvider) + { + } + } +} diff --git a/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs b/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs deleted file mode 100644 index 1223984..0000000 --- a/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs +++ /dev/null @@ -1,15 +0,0 @@ -using CryptoExchange.Net.Interfaces; -using CryptoExchange.Net.Interfaces.CommonClients; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptoExchange.Net.Clients -{ - public interface ICryptoExchangeClient - { - IEnumerable GetSpotClients(); - T? TryGet(); - } -} diff --git a/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs b/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs new file mode 100644 index 0000000..da0cf01 --- /dev/null +++ b/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs @@ -0,0 +1,27 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + /// + /// Client for accessing REST API's for different exchanges + /// + public interface ICryptoRestClient + { + /// + /// Get a list of all registered common ISpotClient types + /// + /// + IEnumerable GetSpotClients(); + /// + /// Try get + /// + /// + /// + T TryGet(Func createFunc); + } +} diff --git a/CryptoExchange.Net/Interfaces/ICryptoSocketClient.cs b/CryptoExchange.Net/Interfaces/ICryptoSocketClient.cs new file mode 100644 index 0000000..5fc1c6c --- /dev/null +++ b/CryptoExchange.Net/Interfaces/ICryptoSocketClient.cs @@ -0,0 +1,22 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + /// + /// Client for accessing Websocket API's for different exchanges + /// + public interface ICryptoSocketClient + { + /// + /// Try get a client by type for the service collection + /// + /// + /// + T TryGet(Func createFunc); + } +} diff --git a/CryptoExchange.Net/Sockets/Query.cs b/CryptoExchange.Net/Sockets/Query.cs index 045b2e3..56d3ea4 100644 --- a/CryptoExchange.Net/Sockets/Query.cs +++ b/CryptoExchange.Net/Sockets/Query.cs @@ -41,7 +41,7 @@ namespace CryptoExchange.Net.Sockets public object? Response { get; set; } /// - /// Action to execute when query is finished + /// Wait event for the calling message processing thread /// public AsyncResetEvent? ContinueAwaiter { get; set; }