From e2dde77023865ab4302a6e4912c5d8e40918f95d Mon Sep 17 00:00:00 2001 From: JKorf Date: Sat, 2 Dec 2023 14:20:41 +0100 Subject: [PATCH] Added DecimalStringWriter json converter, added support for specifying body content type on a per request basis --- .../CallResultTests.cs | 2 ++ CryptoExchange.Net/Clients/RestApiClient.cs | 14 +++++++--- .../DecimalStringWriterConverter.cs | 27 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 CryptoExchange.Net/Converters/DecimalStringWriterConverter.cs diff --git a/CryptoExchange.Net.UnitTests/CallResultTests.cs b/CryptoExchange.Net.UnitTests/CallResultTests.cs index f1f687c..b445664 100644 --- a/CryptoExchange.Net.UnitTests/CallResultTests.cs +++ b/CryptoExchange.Net.UnitTests/CallResultTests.cs @@ -115,6 +115,7 @@ namespace CryptoExchange.Net.UnitTests TimeSpan.FromSeconds(1), null, "{}", + 1, "https://test.com/api", null, HttpMethod.Get, @@ -143,6 +144,7 @@ namespace CryptoExchange.Net.UnitTests TimeSpan.FromSeconds(1), null, "{}", + 1, "https://test.com/api", null, HttpMethod.Get, diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index 2b37080..eb312a8 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -83,6 +83,7 @@ namespace CryptoExchange.Net /// Cancellation token /// The parameters of the request /// Whether or not the request should be authenticated + /// The format of the body content /// Where the parameters should be placed, overwrites the value set in the client /// How array parameters should be serialized, overwrites the value set in the client /// Credits used for the request @@ -97,6 +98,7 @@ namespace CryptoExchange.Net CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, + RequestBodyFormat? requestBodyFormat = null, HttpMethodParameterPosition? parameterPosition = null, ArrayParametersSerialization? arraySerialization = null, int requestWeight = 1, @@ -108,7 +110,7 @@ namespace CryptoExchange.Net while (true) { currentTry++; - var request = await PrepareRequestAsync(uri, method, cancellationToken, parameters, signed, parameterPosition, arraySerialization, requestWeight, deserializer, additionalHeaders, ignoreRatelimit).ConfigureAwait(false); + var request = await PrepareRequestAsync(uri, method, cancellationToken, parameters, signed, requestBodyFormat, parameterPosition, arraySerialization, requestWeight, deserializer, additionalHeaders, ignoreRatelimit).ConfigureAwait(false); if (!request) return new WebCallResult(request.Error!); @@ -134,6 +136,7 @@ namespace CryptoExchange.Net /// Cancellation token /// The parameters of the request /// Whether or not the request should be authenticated + /// The format of the body content /// Where the parameters should be placed, overwrites the value set in the client /// How array parameters should be serialized, overwrites the value set in the client /// Credits used for the request @@ -148,6 +151,7 @@ namespace CryptoExchange.Net CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, + RequestBodyFormat? requestBodyFormat = null, HttpMethodParameterPosition? parameterPosition = null, ArrayParametersSerialization? arraySerialization = null, int requestWeight = 1, @@ -160,7 +164,7 @@ namespace CryptoExchange.Net while (true) { currentTry++; - var request = await PrepareRequestAsync(uri, method, cancellationToken, parameters, signed, parameterPosition, arraySerialization, requestWeight, deserializer, additionalHeaders, ignoreRatelimit).ConfigureAwait(false); + var request = await PrepareRequestAsync(uri, method, cancellationToken, parameters, signed, requestBodyFormat, parameterPosition, arraySerialization, requestWeight, deserializer, additionalHeaders, ignoreRatelimit).ConfigureAwait(false); if (!request) return new WebCallResult(request.Error!); @@ -185,6 +189,7 @@ namespace CryptoExchange.Net /// Cancellation token /// The parameters of the request /// Whether or not the request should be authenticated + /// The format of the body content /// Where the parameters should be placed, overwrites the value set in the client /// How array parameters should be serialized, overwrites the value set in the client /// Credits used for the request @@ -198,6 +203,7 @@ namespace CryptoExchange.Net CancellationToken cancellationToken, Dictionary? parameters = null, bool signed = false, + RequestBodyFormat? requestBodyFormat = null, HttpMethodParameterPosition? parameterPosition = null, ArrayParametersSerialization? arraySerialization = null, int requestWeight = 1, @@ -242,7 +248,7 @@ namespace CryptoExchange.Net _logger.Log(LogLevel.Information, $"[{requestId}] Creating request for " + uri); var paramsPosition = parameterPosition ?? ParameterPositions[method]; - var request = ConstructRequest(uri, method, parameters?.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value), signed, paramsPosition, arraySerialization ?? this.arraySerialization, requestId, additionalHeaders); + var request = ConstructRequest(uri, method, parameters?.OrderBy(p => p.Key).ToDictionary(p => p.Key, p => p.Value), signed, paramsPosition, arraySerialization ?? this.arraySerialization, requestBodyFormat ?? this.requestBodyFormat, requestId, additionalHeaders); string? paramString = ""; if (paramsPosition == HttpMethodParameterPosition.InBody) @@ -417,6 +423,7 @@ namespace CryptoExchange.Net /// Whether or not the request should be authenticated /// Where the parameters should be placed /// How array parameters should be serialized + /// Format of the body content /// Unique id of a request /// Additional headers to send with the request /// @@ -427,6 +434,7 @@ namespace CryptoExchange.Net bool signed, HttpMethodParameterPosition parameterPosition, ArrayParametersSerialization arraySerialization, + RequestBodyFormat bodyFormat, int requestId, Dictionary? additionalHeaders) { diff --git a/CryptoExchange.Net/Converters/DecimalStringWriterConverter.cs b/CryptoExchange.Net/Converters/DecimalStringWriterConverter.cs new file mode 100644 index 0000000..ca646fb --- /dev/null +++ b/CryptoExchange.Net/Converters/DecimalStringWriterConverter.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using System; +using System.Globalization; + +namespace Kraken.Net.Converters +{ + /// + /// Converter for serializing decimal values as string + /// + public class DecimalStringWriterConverter : JsonConverter + { + /// + public override bool CanRead => false; + + /// + public override bool CanConvert(Type objectType) => objectType == typeof(decimal) || objectType == typeof(decimal?); + + /// + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + + /// + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) => writer.WriteValue(((decimal?)value)?.ToString(CultureInfo.InvariantCulture) ?? null); + } +}