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

Added ApiClients list for managing api credentials, requests made and dispose

This commit is contained in:
Jkorf 2021-12-17 14:17:30 +01:00
parent 04b43257a5
commit c62fbda3d7
4 changed files with 42 additions and 68 deletions

View File

@ -1,20 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Requests;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net
{
@ -23,7 +9,7 @@ namespace CryptoExchange.Net
/// </summary>
public abstract class BaseApiClient: IDisposable
{
private readonly ApiCredentials? _apiCredentials;
private ApiCredentials? _apiCredentials;
private AuthenticationProvider? _authenticationProvider;
private bool _created;
@ -67,6 +53,14 @@ namespace CryptoExchange.Net
/// <returns></returns>
protected abstract AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials);
/// <inheritdoc />
public void SetApiCredentials(ApiCredentials credentials)
{
_apiCredentials = credentials;
_created = false;
_authenticationProvider = null;
}
/// <summary>
/// Dispose
/// </summary>

View File

@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
@ -22,6 +23,10 @@ namespace CryptoExchange.Net
/// </summary>
internal string ExchangeName { get; }
/// <summary>
/// Api clients in this client
/// </summary>
internal List<BaseApiClient> ApiClients { get; } = new List<BaseApiClient>();
/// <summary>
/// The log object
/// </summary>
protected internal Log log;
@ -66,6 +71,16 @@ namespace CryptoExchange.Net
log.Write(LogLevel.Debug, $"Client configuration: {options}, CryptoExchange.Net: v{typeof(BaseClient).Assembly.GetName().Version}, {ExchangeName}.Net: v{GetType().Assembly.GetName().Version}");
}
/// <summary>
/// Register an API client
/// </summary>
/// <param name="apiClient">The client</param>
protected T AddApiClient<T>(T apiClient) where T: BaseApiClient
{
ApiClients.Add(apiClient);
return apiClient;
}
/// <summary>
/// Tries to parse the json data and return a JToken, validating the input not being empty and being valid json
/// </summary>
@ -263,6 +278,8 @@ namespace CryptoExchange.Net
public virtual void Dispose()
{
log.Write(LogLevel.Debug, "Disposing exchange client");
foreach (var client in ApiClients)
client.Dispose();
}
}
}

View File

@ -7,7 +7,7 @@ using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Requests;
@ -59,7 +59,7 @@ namespace CryptoExchange.Net
protected string requestBodyEmptyContent = "{}";
/// <inheritdoc />
public int TotalRequestsMade { get; private set; }
public int TotalRequestsMade => ApiClients.OfType<RestApiClient>().Sum(s => s.TotalRequestsMade);
/// <summary>
/// Request headers to be sent with each request
@ -85,6 +85,12 @@ namespace CryptoExchange.Net
RequestFactory.Configure(exchangeOptions.RequestTimeout, exchangeOptions.Proxy, exchangeOptions.HttpClient);
}
/// <inheritdoc />
public void SetApiCredentials(ApiCredentials credentials)
{
foreach (var apiClient in ApiClients)
apiClient.SetApiCredentials(credentials);
}
/// <summary>
/// Execute a request to the uri and deserialize the response into the provided type parameter
@ -154,8 +160,6 @@ namespace CryptoExchange.Net
}
apiClient.TotalRequestsMade++;
TotalRequestsMade++;
log.Write(LogLevel.Debug, $"[{requestId}] Sending {method}{(signed ? " signed" : "")} request to {request.Uri}{paramString ?? " "}{(ClientOptions.Proxy == null ? "" : $" via proxy {ClientOptions.Proxy.Host}")}");
return await GetResponseAsync<T>(request, deserializer, cancellationToken).ConfigureAwait(false);
}
@ -352,48 +356,6 @@ namespace CryptoExchange.Net
}
return request;
//var uriString = uri.ToString();
//if (apiClient.AuthenticationProvider != null)
// parameters = apiClient.AuthenticationProvider.AddAuthenticationToParameters(uriString, method, parameters, signed, parameterPosition, arraySerialization);
//if (parameterPosition == HttpMethodParameterPosition.InUri && parameters?.Any() == true)
// uriString += "?" + parameters.CreateParamString(true, arraySerialization);
//var contentType = requestBodyFormat == RequestBodyFormat.Json ? Constants.JsonContentHeader : Constants.FormContentHeader;
//var request = RequestFactory.Create(method, uriString, requestId);
//request.Accept = Constants.JsonContentHeader;
//var headers = new Dictionary<string, string>();
//if (apiClient.AuthenticationProvider != null)
// headers = apiClient.AuthenticationProvider.AddAuthenticationToHeaders(uriString, method, parameters!, signed, parameterPosition, arraySerialization);
//foreach (var header in headers)
// request.AddHeader(header.Key, header.Value);
//if (additionalHeaders != null)
//{
// foreach (var header in additionalHeaders)
// request.AddHeader(header.Key, header.Value);
//}
//if(StandardRequestHeaders != null)
//{
// foreach (var header in StandardRequestHeaders)
// // Only add it if it isn't overwritten
// if(additionalHeaders?.ContainsKey(header.Key) != true)
// request.AddHeader(header.Key, header.Value);
//}
//if (parameterPosition == HttpMethodParameterPosition.InBody)
//{
// if (parameters?.Any() == true)
// WriteParamBody(request, parameters, contentType);
// else
// request.SetContent(requestBodyEmptyContent, contentType);
//}
//return request;
}
/// <summary>
@ -427,11 +389,5 @@ namespace CryptoExchange.Net
{
return new ServerError(error.ToString());
}
/// <inheritdoc />
public override void Dispose()
{
base.Dispose();
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Objects;
namespace CryptoExchange.Net.Interfaces
@ -22,5 +23,11 @@ namespace CryptoExchange.Net.Interfaces
/// The options provided for this client
/// </summary>
BaseRestClientOptions ClientOptions { get; }
/// <summary>
/// Set the API credentials for this client. All Api clients in this client will use the new credentials, regardless of earlier set options.
/// </summary>
/// <param name="credentials">The credentials to set</param>
void SetApiCredentials(ApiCredentials credentials);
}
}