1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-09 08:56:13 +00:00

Some adjustment to the Crypto clients

This commit is contained in:
JKorf 2024-02-06 19:28:31 +01:00
parent 1eb0214e7a
commit 9ddd446892
4 changed files with 37 additions and 22 deletions

View File

@ -1,10 +1,6 @@
using CryptoExchange.Net.Interfaces; using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Interfaces.CommonClients;
using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CryptoExchange.Net.Clients namespace CryptoExchange.Net.Clients
{ {
@ -42,6 +38,10 @@ namespace CryptoExchange.Net.Clients
/// <returns></returns> /// <returns></returns>
public T TryGet<T>(Func<T> createFunc) public T TryGet<T>(Func<T> createFunc)
{ {
var type = typeof(T);
if (_serviceCache.TryGetValue(type, out var value))
return (T)value;
if (_serviceProvider == null) if (_serviceProvider == null)
{ {
// Create with default options // Create with default options
@ -50,16 +50,8 @@ namespace CryptoExchange.Net.Clients
return createResult; return createResult;
} }
var type = typeof(T); var result = _serviceProvider.GetService<T>()
if (_serviceCache.TryGetValue(type, out var value)) ?? throw new InvalidOperationException($"No service was found for {typeof(T).Name}, make sure the exchange is registered in dependency injection with the `services.Add[Exchange]()` method");
return (T)value;
var result = _serviceProvider.GetRequiredService<T>();
if (result == null)
{
// Does this mean the AddXX() hasn't been done?
}
_serviceCache.Add(type, result!); _serviceCache.Add(type, result!);
return result; return result;
} }

View File

@ -4,13 +4,19 @@ using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace CryptoExchange.Net.Clients namespace CryptoExchange.Net.Clients
{ {
/// <inheritdoc /> /// <inheritdoc />
public class CryptoRestClient : CryptoBaseClient, ICryptoRestClient public class CryptoRestClient : CryptoBaseClient, ICryptoRestClient
{ {
/// <summary>
/// ctor
/// </summary>
public CryptoRestClient()
{
}
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@ -20,7 +26,7 @@ namespace CryptoExchange.Net.Clients
} }
/// <summary> /// <summary>
/// Try get a client by type for the service collection /// Get a list of the registered ISpotClient implementations
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public IEnumerable<ISpotClient> GetSpotClients() public IEnumerable<ISpotClient> GetSpotClients()
@ -30,5 +36,12 @@ namespace CryptoExchange.Net.Clients
return _serviceProvider.GetServices<ISpotClient>().ToList(); return _serviceProvider.GetServices<ISpotClient>().ToList();
} }
/// <summary>
/// Get an ISpotClient implementation by exchange name
/// </summary>
/// <param name="exchangeName"></param>
/// <returns></returns>
public ISpotClient? SpotClient(string exchangeName) => _serviceProvider.GetServices<ISpotClient>()?.SingleOrDefault(s => s.ExchangeName.Equals(exchangeName, StringComparison.InvariantCultureIgnoreCase));
} }
} }

View File

@ -1,16 +1,18 @@
using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Interfaces.CommonClients;
using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CryptoExchange.Net.Clients namespace CryptoExchange.Net.Clients
{ {
/// <inheritdoc /> /// <inheritdoc />
public class CryptoSocketClient : CryptoBaseClient, ICryptoSocketClient public class CryptoSocketClient : CryptoBaseClient, ICryptoSocketClient
{ {
/// <summary>
/// ctor
/// </summary>
public CryptoSocketClient()
{
}
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>

View File

@ -16,6 +16,14 @@ namespace CryptoExchange.Net.Interfaces
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
IEnumerable<ISpotClient> GetSpotClients(); IEnumerable<ISpotClient> GetSpotClients();
/// <summary>
/// Get an ISpotClient implementation by exchange name
/// </summary>
/// <param name="exchangeName"></param>
/// <returns></returns>
ISpotClient? SpotClient(string exchangeName);
/// <summary> /// <summary>
/// Try get /// Try get
/// </summary> /// </summary>