mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-10 01:16:24 +00:00
73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using System;
|
|
using CryptoExchange.Net.Authentication;
|
|
using CryptoExchange.Net.Objects;
|
|
|
|
namespace CryptoExchange.Net
|
|
{
|
|
/// <summary>
|
|
/// Base API for all API clients
|
|
/// </summary>
|
|
public abstract class BaseApiClient: IDisposable
|
|
{
|
|
private ApiCredentials? _apiCredentials;
|
|
private AuthenticationProvider? _authenticationProvider;
|
|
private bool _created;
|
|
|
|
/// <summary>
|
|
/// The authentication provider for this API client. (null if no credentials are set)
|
|
/// </summary>
|
|
public AuthenticationProvider? AuthenticationProvider
|
|
{
|
|
get
|
|
{
|
|
if (!_created && _apiCredentials != null)
|
|
{
|
|
_authenticationProvider = CreateAuthenticationProvider(_apiCredentials);
|
|
_created = true;
|
|
}
|
|
|
|
return _authenticationProvider;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The base address for this API client
|
|
/// </summary>
|
|
internal protected string BaseAddress { get; }
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="options">Client options</param>
|
|
/// <param name="apiOptions">Api client options</param>
|
|
protected BaseApiClient(BaseClientOptions options, ApiClientOptions apiOptions)
|
|
{
|
|
_apiCredentials = apiOptions.ApiCredentials?.Copy() ?? options.ApiCredentials?.Copy();
|
|
BaseAddress = apiOptions.BaseAddress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an AuthenticationProvider implementation instance based on the provided credentials
|
|
/// </summary>
|
|
/// <param name="credentials"></param>
|
|
/// <returns></returns>
|
|
protected abstract AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials);
|
|
|
|
/// <inheritdoc />
|
|
public void SetApiCredentials(ApiCredentials credentials)
|
|
{
|
|
_apiCredentials = credentials;
|
|
_created = false;
|
|
_authenticationProvider = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
AuthenticationProvider?.Credentials?.Dispose();
|
|
}
|
|
}
|
|
}
|