mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-09 00:46:19 +00:00
Some adjustment to the Crypto clients
This commit is contained in:
parent
1eb0214e7a
commit
9ddd446892
@ -1,10 +1,6 @@
|
||||
using CryptoExchange.Net.Interfaces;
|
||||
using CryptoExchange.Net.Interfaces.CommonClients;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CryptoExchange.Net.Clients
|
||||
{
|
||||
@ -42,6 +38,10 @@ namespace CryptoExchange.Net.Clients
|
||||
/// <returns></returns>
|
||||
public T TryGet<T>(Func<T> createFunc)
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (_serviceCache.TryGetValue(type, out var value))
|
||||
return (T)value;
|
||||
|
||||
if (_serviceProvider == null)
|
||||
{
|
||||
// Create with default options
|
||||
@ -50,16 +50,8 @@ namespace CryptoExchange.Net.Clients
|
||||
return createResult;
|
||||
}
|
||||
|
||||
var type = typeof(T);
|
||||
if (_serviceCache.TryGetValue(type, out var value))
|
||||
return (T)value;
|
||||
|
||||
var result = _serviceProvider.GetRequiredService<T>();
|
||||
if (result == null)
|
||||
{
|
||||
// Does this mean the AddXX() hasn't been done?
|
||||
}
|
||||
|
||||
var result = _serviceProvider.GetService<T>()
|
||||
?? 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");
|
||||
_serviceCache.Add(type, result!);
|
||||
return result;
|
||||
}
|
||||
|
@ -4,13 +4,19 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CryptoExchange.Net.Clients
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class CryptoRestClient : CryptoBaseClient, ICryptoRestClient
|
||||
{
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public CryptoRestClient()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
@ -20,7 +26,7 @@ namespace CryptoExchange.Net.Clients
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try get a client by type for the service collection
|
||||
/// Get a list of the registered ISpotClient implementations
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<ISpotClient> GetSpotClients()
|
||||
@ -30,5 +36,12 @@ namespace CryptoExchange.Net.Clients
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
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
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class CryptoSocketClient : CryptoBaseClient, ICryptoSocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
public CryptoSocketClient()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
|
@ -16,6 +16,14 @@ namespace CryptoExchange.Net.Interfaces
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerable<ISpotClient> GetSpotClients();
|
||||
|
||||
/// <summary>
|
||||
/// Get an ISpotClient implementation by exchange name
|
||||
/// </summary>
|
||||
/// <param name="exchangeName"></param>
|
||||
/// <returns></returns>
|
||||
ISpotClient? SpotClient(string exchangeName);
|
||||
|
||||
/// <summary>
|
||||
/// Try get
|
||||
/// </summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user