From 8080ecccc0ef850534b8708e39faef3d607d08cf Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 4 Jun 2024 09:54:24 +0200 Subject: [PATCH] Added support for Patch requests, added SetBody to ParameterCollection for directly setting the request body --- .../Authentication/AuthenticationProvider.cs | 14 ++++++++++++++ CryptoExchange.Net/Clients/RestApiClient.cs | 9 +++++++-- CryptoExchange.Net/Objects/Constants.cs | 4 ++++ .../Objects/ParameterCollection.cs | 14 ++++++++++++++ docs/index.html | 16 +++++++++++++--- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs index e13e0fb..7e143d4 100644 --- a/CryptoExchange.Net/Authentication/AuthenticationProvider.cs +++ b/CryptoExchange.Net/Authentication/AuthenticationProvider.cs @@ -434,6 +434,20 @@ namespace CryptoExchange.Net.Authentication return DateTimeConverter.ConvertToMilliseconds(GetTimestamp(apiClient)).Value.ToString(CultureInfo.InvariantCulture); } + /// + /// Return the serialized request body + /// + /// + /// + /// + protected string GetSerializedBody(IMessageSerializer serializer, IDictionary parameters) + { + if (parameters.Count == 1 && parameters.ContainsKey(Constants.BodyPlaceHolderKey)) + return serializer.Serialize(parameters[Constants.BodyPlaceHolderKey]); + else + return serializer.Serialize(parameters); + } + /// public void Dispose() { diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index 1b0ab6c..68c2af0 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -75,7 +75,8 @@ namespace CryptoExchange.Net.Clients { HttpMethod.Get, HttpMethodParameterPosition.InUri }, { HttpMethod.Post, HttpMethodParameterPosition.InBody }, { HttpMethod.Delete, HttpMethodParameterPosition.InBody }, - { HttpMethod.Put, HttpMethodParameterPosition.InBody } + { HttpMethod.Put, HttpMethodParameterPosition.InBody }, + { new HttpMethod("Patch"), HttpMethodParameterPosition.InBody }, }; /// @@ -804,7 +805,11 @@ namespace CryptoExchange.Net.Clients if (contentType == Constants.JsonContentHeader) { // Write the parameters as json in the body - var stringData = CreateSerializer().Serialize(parameters); + string stringData; + if (parameters.Count == 1 && parameters.ContainsKey(Constants.BodyPlaceHolderKey)) + stringData = CreateSerializer().Serialize(parameters[Constants.BodyPlaceHolderKey]); + else + stringData = CreateSerializer().Serialize(parameters); request.SetContent(stringData, contentType); } else if (contentType == Constants.FormContentHeader) diff --git a/CryptoExchange.Net/Objects/Constants.cs b/CryptoExchange.Net/Objects/Constants.cs index 82e5af5..386897b 100644 --- a/CryptoExchange.Net/Objects/Constants.cs +++ b/CryptoExchange.Net/Objects/Constants.cs @@ -13,5 +13,9 @@ /// Form content type header /// public const string FormContentHeader = "application/x-www-form-urlencoded"; + /// + /// Placeholder key for when request body should be set to the value of this KVP + /// + public const string BodyPlaceHolderKey = "_BODY_"; } } diff --git a/CryptoExchange.Net/Objects/ParameterCollection.cs b/CryptoExchange.Net/Objects/ParameterCollection.cs index d2949d0..e2438b4 100644 --- a/CryptoExchange.Net/Objects/ParameterCollection.cs +++ b/CryptoExchange.Net/Objects/ParameterCollection.cs @@ -3,6 +3,7 @@ using CryptoExchange.Net.Converters.SystemTextJson; using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; namespace CryptoExchange.Net.Objects { @@ -193,5 +194,18 @@ namespace CryptoExchange.Net.Objects Add(key, int.Parse(stringVal)); } } + + /// + /// Set the request body. Can be used to specify a simple value or array as the body instead of an object + /// + /// Body to set + /// + public void SetBody(object body) + { + if (this.Any()) + throw new InvalidOperationException("Can't set body when other parameters already specified"); + + Add(Constants.BodyPlaceHolderKey, body); + } } } diff --git a/docs/index.html b/docs/index.html index ac2e0da..4245dcf 100644 --- a/docs/index.html +++ b/docs/index.html @@ -82,16 +82,17 @@