mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-08 08:26:20 +00:00
Merge branch 'master' into pr/4
This commit is contained in:
commit
c3ce0de12a
@ -588,6 +588,11 @@
|
|||||||
Uri
|
Uri
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:CryptoExchange.Net.Interfaces.IRequest.RequestId">
|
||||||
|
<summary>
|
||||||
|
internal request id for tracing
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Interfaces.IRequest.SetContent(System.Byte[])">
|
<member name="M:CryptoExchange.Net.Interfaces.IRequest.SetContent(System.Byte[])">
|
||||||
<summary>
|
<summary>
|
||||||
Set byte content
|
Set byte content
|
||||||
@ -628,12 +633,13 @@
|
|||||||
<param name="uri"></param>
|
<param name="uri"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Interfaces.IRequestFactory.Configure(System.TimeSpan,CryptoExchange.Net.Objects.ApiProxy)">
|
<member name="M:CryptoExchange.Net.Interfaces.IRequestFactory.Configure(System.TimeSpan,CryptoExchange.Net.Objects.ApiProxy,System.Boolean)">
|
||||||
<summary>
|
<summary>
|
||||||
Configure the requests created by this factory
|
Configure the requests created by this factory
|
||||||
</summary>
|
</summary>
|
||||||
<param name="requestTimeout">Request timeout to use</param>
|
<param name="requestTimeout">Request timeout to use</param>
|
||||||
<param name="proxy">Proxy settings to use</param>
|
<param name="proxy">Proxy settings to use</param>
|
||||||
|
<param name="isTracingEnabled">Should generate unique id for requests</param>
|
||||||
</member>
|
</member>
|
||||||
<member name="T:CryptoExchange.Net.Interfaces.IResponse">
|
<member name="T:CryptoExchange.Net.Interfaces.IResponse">
|
||||||
<summary>
|
<summary>
|
||||||
@ -2024,12 +2030,13 @@
|
|||||||
Request object
|
Request object
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Requests.Request.#ctor(System.Net.Http.HttpRequestMessage,System.Net.Http.HttpClient)">
|
<member name="M:CryptoExchange.Net.Requests.Request.#ctor(System.Net.Http.HttpRequestMessage,System.Net.Http.HttpClient,System.Boolean)">
|
||||||
<summary>
|
<summary>
|
||||||
Create request object for web request
|
Create request object for web request
|
||||||
</summary>
|
</summary>
|
||||||
<param name="request"></param>
|
<param name="request"></param>
|
||||||
<param name="client"></param>
|
<param name="client"></param>
|
||||||
|
<param name="isTracingEnabled">if true, should assign unique id for request</param>
|
||||||
</member>
|
</member>
|
||||||
<member name="P:CryptoExchange.Net.Requests.Request.Content">
|
<member name="P:CryptoExchange.Net.Requests.Request.Content">
|
||||||
<inheritdoc />
|
<inheritdoc />
|
||||||
@ -2043,6 +2050,9 @@
|
|||||||
<member name="P:CryptoExchange.Net.Requests.Request.Uri">
|
<member name="P:CryptoExchange.Net.Requests.Request.Uri">
|
||||||
<inheritdoc />
|
<inheritdoc />
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:CryptoExchange.Net.Requests.Request.RequestId">
|
||||||
|
<inheritdoc />
|
||||||
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Requests.Request.SetContent(System.String,System.String)">
|
<member name="M:CryptoExchange.Net.Requests.Request.SetContent(System.String,System.String)">
|
||||||
<inheritdoc />
|
<inheritdoc />
|
||||||
</member>
|
</member>
|
||||||
@ -2060,7 +2070,7 @@
|
|||||||
WebRequest factory
|
WebRequest factory
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Requests.RequestFactory.Configure(System.TimeSpan,CryptoExchange.Net.Objects.ApiProxy)">
|
<member name="M:CryptoExchange.Net.Requests.RequestFactory.Configure(System.TimeSpan,CryptoExchange.Net.Objects.ApiProxy,System.Boolean)">
|
||||||
<inheritdoc />
|
<inheritdoc />
|
||||||
</member>
|
</member>
|
||||||
<member name="M:CryptoExchange.Net.Requests.RequestFactory.Create(System.Net.Http.HttpMethod,System.String)">
|
<member name="M:CryptoExchange.Net.Requests.RequestFactory.Create(System.Net.Http.HttpMethod,System.String)">
|
||||||
|
@ -27,6 +27,10 @@ namespace CryptoExchange.Net.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
Uri Uri { get; }
|
Uri Uri { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// internal request id for tracing
|
||||||
|
/// </summary>
|
||||||
|
string? RequestId { get; }
|
||||||
|
/// <summary>
|
||||||
/// Set byte content
|
/// Set byte content
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
|
@ -22,6 +22,7 @@ namespace CryptoExchange.Net.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="requestTimeout">Request timeout to use</param>
|
/// <param name="requestTimeout">Request timeout to use</param>
|
||||||
/// <param name="proxy">Proxy settings to use</param>
|
/// <param name="proxy">Proxy settings to use</param>
|
||||||
void Configure(TimeSpan requestTimeout, ApiProxy? proxy);
|
/// <param name="isTracingEnabled">Should generate unique id for requests</param>
|
||||||
|
void Configure(TimeSpan requestTimeout, ApiProxy? proxy, bool isTracingEnabled=false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ namespace CryptoExchange.Net.Objects
|
|||||||
/// The time the server has to respond to a request before timing out
|
/// The time the server has to respond to a request before timing out
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(30);
|
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(30);
|
||||||
|
public bool IsRequestsTracingEnabled { get; set; } = false;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ctor
|
/// ctor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -21,10 +22,15 @@ namespace CryptoExchange.Net.Requests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="client"></param>
|
/// <param name="client"></param>
|
||||||
public Request(HttpRequestMessage request, HttpClient client)
|
/// <param name="isTracingEnabled">if true, should assign unique id for request</param>
|
||||||
|
public Request(HttpRequestMessage request, HttpClient client, bool isTracingEnabled=false)
|
||||||
{
|
{
|
||||||
httpClient = client;
|
httpClient = client;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
|
if (isTracingEnabled)
|
||||||
|
{
|
||||||
|
RequestId = Path.GetRandomFileName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -45,6 +51,8 @@ namespace CryptoExchange.Net.Requests
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Uri Uri => request.RequestUri;
|
public Uri Uri => request.RequestUri;
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string? RequestId { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void SetContent(string data, string contentType)
|
public void SetContent(string data, string contentType)
|
||||||
|
@ -12,10 +12,11 @@ namespace CryptoExchange.Net.Requests
|
|||||||
public class RequestFactory : IRequestFactory
|
public class RequestFactory : IRequestFactory
|
||||||
{
|
{
|
||||||
private HttpClient? httpClient;
|
private HttpClient? httpClient;
|
||||||
|
private bool isTracingEnabled;
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Configure(TimeSpan requestTimeout, ApiProxy? proxy)
|
public void Configure(TimeSpan requestTimeout, ApiProxy? proxy, bool isTracingEnabled = false)
|
||||||
{
|
{
|
||||||
|
this.isTracingEnabled = isTracingEnabled;
|
||||||
HttpMessageHandler handler = new HttpClientHandler()
|
HttpMessageHandler handler = new HttpClientHandler()
|
||||||
{
|
{
|
||||||
Proxy = proxy == null ? null : new WebProxy
|
Proxy = proxy == null ? null : new WebProxy
|
||||||
@ -25,7 +26,7 @@ namespace CryptoExchange.Net.Requests
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
httpClient = new HttpClient(handler) {Timeout = requestTimeout};
|
httpClient = new HttpClient(handler) { Timeout = requestTimeout };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -34,7 +35,7 @@ namespace CryptoExchange.Net.Requests
|
|||||||
if (httpClient == null)
|
if (httpClient == null)
|
||||||
throw new InvalidOperationException("Cant create request before configuring http client");
|
throw new InvalidOperationException("Cant create request before configuring http client");
|
||||||
|
|
||||||
return new Request(new HttpRequestMessage(method, uri), httpClient);
|
return new Request(new HttpRequestMessage(method, uri), httpClient, isTracingEnabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ namespace CryptoExchange.Net
|
|||||||
throw new ArgumentNullException(nameof(exchangeOptions));
|
throw new ArgumentNullException(nameof(exchangeOptions));
|
||||||
|
|
||||||
RequestTimeout = exchangeOptions.RequestTimeout;
|
RequestTimeout = exchangeOptions.RequestTimeout;
|
||||||
RequestFactory.Configure(exchangeOptions.RequestTimeout, exchangeOptions.Proxy);
|
RequestFactory.Configure(exchangeOptions.RequestTimeout, exchangeOptions.Proxy,exchangeOptions.IsRequestsTracingEnabled);
|
||||||
RateLimitBehaviour = exchangeOptions.RateLimitingBehaviour;
|
RateLimitBehaviour = exchangeOptions.RateLimitingBehaviour;
|
||||||
var rateLimiters = new List<IRateLimiter>();
|
var rateLimiters = new List<IRateLimiter>();
|
||||||
foreach (var rateLimiter in exchangeOptions.RateLimiters)
|
foreach (var rateLimiter in exchangeOptions.RateLimiters)
|
||||||
@ -190,14 +190,14 @@ namespace CryptoExchange.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (limitResult.Data > 0)
|
if (limitResult.Data > 0)
|
||||||
log.Write(LogVerbosity.Debug, $"Request {uri.AbsolutePath} was limited by {limitResult.Data}ms by {limiter.GetType().Name}");
|
log.Write(LogVerbosity.Debug, $"Request {request.RequestId} {uri.AbsolutePath} was limited by {limitResult.Data}ms by {limiter.GetType().Name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
string? paramString = null;
|
string? paramString = null;
|
||||||
if (method == HttpMethod.Post)
|
if (method == HttpMethod.Post)
|
||||||
paramString = " with request body " + request.Content;
|
paramString = " with request body " + request.Content;
|
||||||
|
|
||||||
log.Write(LogVerbosity.Debug, $"Sending {method}{(signed ? " signed" : "")} request to {request.Uri}{paramString ?? " "}{(apiProxy == null? "": $" via proxy {apiProxy.Host}")}");
|
log.Write(LogVerbosity.Debug, $"Sending {method}{(signed ? " signed" : "")} request to {request.Uri}{paramString ?? " "}{(apiProxy == null? "": $" via proxy {apiProxy.Host}")} {(request.RequestId==null?"":$" with id {request.RequestId}")}");
|
||||||
return await GetResponse<T>(request, cancellationToken).ConfigureAwait(false);
|
return await GetResponse<T>(request, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +224,7 @@ namespace CryptoExchange.Net
|
|||||||
var data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
var data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||||
responseStream.Close();
|
responseStream.Close();
|
||||||
response.Close();
|
response.Close();
|
||||||
log.Write(LogVerbosity.Debug, $"Data received: {data}");
|
log.Write(LogVerbosity.Debug, $"Data {(request.RequestId==null?"":$"for request {request.RequestId} ")}received: {data}");
|
||||||
|
|
||||||
var parseResult = ValidateJson(data);
|
var parseResult = ValidateJson(data);
|
||||||
if (!parseResult.Success)
|
if (!parseResult.Success)
|
||||||
@ -249,7 +249,7 @@ namespace CryptoExchange.Net
|
|||||||
{
|
{
|
||||||
using var reader = new StreamReader(responseStream);
|
using var reader = new StreamReader(responseStream);
|
||||||
var data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
var data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||||
log.Write(LogVerbosity.Debug, $"Error received: {data}");
|
log.Write(LogVerbosity.Debug, $"Error {(request.RequestId == null ? "" : $"for request {request.RequestId} ")}received: {data}");
|
||||||
responseStream.Close();
|
responseStream.Close();
|
||||||
response.Close();
|
response.Close();
|
||||||
var parseResult = ValidateJson(data);
|
var parseResult = ValidateJson(data);
|
||||||
@ -258,7 +258,7 @@ namespace CryptoExchange.Net
|
|||||||
}
|
}
|
||||||
catch (HttpRequestException requestException)
|
catch (HttpRequestException requestException)
|
||||||
{
|
{
|
||||||
log.Write(LogVerbosity.Warning, "Request exception: " + requestException.Message);
|
log.Write(LogVerbosity.Warning, $"Request {request.RequestId} exception: " + requestException.Message);
|
||||||
return new WebCallResult<T>(null, null, default, new ServerError(requestException.Message));
|
return new WebCallResult<T>(null, null, default, new ServerError(requestException.Message));
|
||||||
}
|
}
|
||||||
catch (TaskCanceledException canceledException)
|
catch (TaskCanceledException canceledException)
|
||||||
@ -266,14 +266,14 @@ namespace CryptoExchange.Net
|
|||||||
if(canceledException.CancellationToken == cancellationToken)
|
if(canceledException.CancellationToken == cancellationToken)
|
||||||
{
|
{
|
||||||
// Cancellation token cancelled
|
// Cancellation token cancelled
|
||||||
log.Write(LogVerbosity.Warning, "Request cancel requested");
|
log.Write(LogVerbosity.Warning, $"Request {request.RequestId} cancel requested");
|
||||||
return new WebCallResult<T>(null, null, default, new CancellationRequestedError());
|
return new WebCallResult<T>(null, null, default, new CancellationRequestedError());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Request timed out
|
// Request timed out
|
||||||
log.Write(LogVerbosity.Warning, "Request timed out");
|
log.Write(LogVerbosity.Warning, $"Request {request.RequestId} timed out");
|
||||||
return new WebCallResult<T>(null, null, default, new WebError("Request timed out"));
|
return new WebCallResult<T>(null, null, default, new WebError($"Request {request.RequestId} timed out"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user