using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Objects.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
namespace CryptoExchange.Net.Clients
{
///
/// The base for all clients, websocket client and rest client
///
public abstract class BaseClient : IDisposable
{
///
/// The name of the API the client is for
///
public string Exchange { get; }
///
/// Api clients in this client
///
internal List ApiClients { get; } = new List();
///
/// The log object
///
protected internal ILogger _logger;
///
/// Provided client options
///
public ExchangeOptions ClientOptions { get; private set; }
///
/// ctor
///
/// Logger
/// The name of the exchange this client is for
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
protected BaseClient(ILoggerFactory? logger, string exchange)
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
_logger = logger?.CreateLogger(exchange) ?? NullLoggerFactory.Instance.CreateLogger(exchange);
Exchange = exchange;
}
///
/// Initialize the client with the specified options
///
///
///
protected virtual void Initialize(ExchangeOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
ClientOptions = options;
_logger.Log(LogLevel.Trace, $"Client configuration: {options}, CryptoExchange.Net: v{typeof(BaseClient).Assembly.GetName().Version}, {Exchange}.Net: v{GetType().Assembly.GetName().Version}");
}
///
/// Set the API credentials for this client. All Api clients in this client will use the new credentials, regardless of earlier set options.
///
/// The credentials to set
protected virtual void SetApiCredentials(T credentials) where T : ApiCredentials
{
foreach (var apiClient in ApiClients)
apiClient.SetApiCredentials(credentials);
}
///
/// Register an API client
///
/// The client
protected T AddApiClient(T apiClient) where T : BaseApiClient
{
if (ClientOptions == null)
throw new InvalidOperationException("Client should have called Initialize before adding API clients");
_logger.Log(LogLevel.Trace, $" {apiClient.GetType().Name}, base address: {apiClient.BaseAddress}");
ApiClients.Add(apiClient);
return apiClient;
}
///
/// Dispose
///
public virtual void Dispose()
{
_logger.Log(LogLevel.Debug, "Disposing client");
foreach (var client in ApiClients)
client.Dispose();
}
}
}