1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 00:16:27 +00:00

Added options to pass a JsonSerializer to the SendRequest method to use for deserialization

This commit is contained in:
Jkorf 2021-04-28 13:13:48 +02:00
parent 93a64b2b1d
commit 8adebea929
2 changed files with 15 additions and 7 deletions

View File

@ -2771,7 +2771,7 @@
</summary>
<returns>The roundtrip time of the ping request</returns>
</member>
<member name="M:CryptoExchange.Net.RestClient.SendRequest``1(System.Uri,System.Net.Http.HttpMethod,System.Threading.CancellationToken,System.Collections.Generic.Dictionary{System.String,System.Object},System.Boolean,System.Boolean,System.Nullable{CryptoExchange.Net.Objects.PostParameters},System.Nullable{CryptoExchange.Net.Objects.ArrayParametersSerialization},System.Int32)">
<member name="M:CryptoExchange.Net.RestClient.SendRequest``1(System.Uri,System.Net.Http.HttpMethod,System.Threading.CancellationToken,System.Collections.Generic.Dictionary{System.String,System.Object},System.Boolean,System.Boolean,System.Nullable{CryptoExchange.Net.Objects.PostParameters},System.Nullable{CryptoExchange.Net.Objects.ArrayParametersSerialization},System.Int32,Newtonsoft.Json.JsonSerializer)">
<summary>
Execute a request
</summary>
@ -2784,13 +2784,16 @@
<param name="checkResult">Whether or not the resulting object should be checked for missing properties in the mapping (only outputs if log verbosity is Debug)</param>
<param name="postPosition">Where the post parameters should be placed</param>
<param name="arraySerialization">How array parameters should be serialized</param>
<param name="credits">Credits used for the request</param>
<param name="deserializer">The JsonSerializer to use for deserialization</param>
<returns></returns>
</member>
<member name="M:CryptoExchange.Net.RestClient.GetResponse``1(CryptoExchange.Net.Interfaces.IRequest,System.Threading.CancellationToken)">
<member name="M:CryptoExchange.Net.RestClient.GetResponse``1(CryptoExchange.Net.Interfaces.IRequest,Newtonsoft.Json.JsonSerializer,System.Threading.CancellationToken)">
<summary>
Executes the request and returns the string result
</summary>
<param name="request">The request object to execute</param>
<param name="deserializer">The JsonSerializer to use for deserialization</param>
<param name="cancellationToken">Cancellation token</param>
<returns></returns>
</member>

View File

@ -169,10 +169,14 @@ namespace CryptoExchange.Net
/// <param name="checkResult">Whether or not the resulting object should be checked for missing properties in the mapping (only outputs if log verbosity is Debug)</param>
/// <param name="postPosition">Where the post parameters should be placed</param>
/// <param name="arraySerialization">How array parameters should be serialized</param>
/// <param name="credits">Credits used for the request</param>
/// <param name="deserializer">The JsonSerializer to use for deserialization</param>
/// <returns></returns>
[return: NotNull]
protected virtual async Task<WebCallResult<T>> SendRequest<T>(Uri uri, HttpMethod method, CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null, bool signed = false, bool checkResult = true, PostParameters? postPosition = null, ArrayParametersSerialization? arraySerialization = null, int credits=1) where T : class
Dictionary<string, object>? parameters = null, bool signed = false, bool checkResult = true,
PostParameters? postPosition = null, ArrayParametersSerialization? arraySerialization = null, int credits = 1,
JsonSerializer? deserializer = null) where T : class
{
var requestId = NextId();
log.Write(LogVerbosity.Debug, $"[{requestId}] Creating request for " + uri);
@ -201,16 +205,17 @@ namespace CryptoExchange.Net
paramString = " with request body " + request.Content;
log.Write(LogVerbosity.Debug, $"[{requestId}] Sending {method}{(signed ? " signed" : "")} request to {request.Uri}{paramString ?? " "}{(apiProxy == null ? "" : $" via proxy {apiProxy.Host}")}");
return await GetResponse<T>(request, cancellationToken).ConfigureAwait(false);
return await GetResponse<T>(request, deserializer, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Executes the request and returns the string result
/// </summary>
/// <param name="request">The request object to execute</param>
/// <param name="deserializer">The JsonSerializer to use for deserialization</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
protected virtual async Task<WebCallResult<T>> GetResponse<T>(IRequest request, CancellationToken cancellationToken)
protected virtual async Task<WebCallResult<T>> GetResponse<T>(IRequest request, JsonSerializer? deserializer, CancellationToken cancellationToken)
{
try
{
@ -238,12 +243,12 @@ namespace CryptoExchange.Net
if (error != null)
return WebCallResult<T>.CreateErrorResult(response.StatusCode, response.ResponseHeaders, error);
var deserializeResult = Deserialize<T>(parseResult.Data, null, null, request.RequestId);
var deserializeResult = Deserialize<T>(parseResult.Data, null, deserializer, request.RequestId);
return new WebCallResult<T>(response.StatusCode, response.ResponseHeaders, deserializeResult.Data, deserializeResult.Error);
}
else
{
var desResult = await Deserialize<T>(responseStream, null, request.RequestId, sw.ElapsedMilliseconds).ConfigureAwait(false);
var desResult = await Deserialize<T>(responseStream, deserializer, request.RequestId, sw.ElapsedMilliseconds).ConfigureAwait(false);
responseStream.Close();
response.Close();