mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 07:56:12 +00:00
Added support for Patch requests, added SetBody to ParameterCollection for directly setting the request body
This commit is contained in:
parent
4b6fa9a1b1
commit
8080ecccc0
@ -434,6 +434,20 @@ namespace CryptoExchange.Net.Authentication
|
||||
return DateTimeConverter.ConvertToMilliseconds(GetTimestamp(apiClient)).Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the serialized request body
|
||||
/// </summary>
|
||||
/// <param name="serializer"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
protected string GetSerializedBody(IMessageSerializer serializer, IDictionary<string, object> parameters)
|
||||
{
|
||||
if (parameters.Count == 1 && parameters.ContainsKey(Constants.BodyPlaceHolderKey))
|
||||
return serializer.Serialize(parameters[Constants.BodyPlaceHolderKey]);
|
||||
else
|
||||
return serializer.Serialize(parameters);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
|
@ -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 },
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -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)
|
||||
|
@ -13,5 +13,9 @@
|
||||
/// Form content type header
|
||||
/// </summary>
|
||||
public const string FormContentHeader = "application/x-www-form-urlencoded";
|
||||
/// <summary>
|
||||
/// Placeholder key for when request body should be set to the value of this KVP
|
||||
/// </summary>
|
||||
public const string BodyPlaceHolderKey = "_BODY_";
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the request body. Can be used to specify a simple value or array as the body instead of an object
|
||||
/// </summary>
|
||||
/// <param name="body">Body to set</param>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public void SetBody(object body)
|
||||
{
|
||||
if (this.Any())
|
||||
throw new InvalidOperationException("Can't set body when other parameters already specified");
|
||||
|
||||
Add(Constants.BodyPlaceHolderKey, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,16 +82,17 @@
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_installation">Installation</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_di">Dependency Injection</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_general">General Client Usage</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_rest">REST API Client</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_socket">Websocket API Client</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_common">Common clients</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_common">Common Clients</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options">Options & Authorization</a>
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_auth">Authorization</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options_set">Setting options</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options_def">Option definitions</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options_set">Setting Options</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_options_def">Option Definitions</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="#idocs_features">Additional Features</a>
|
||||
@ -812,6 +813,15 @@
|
||||
|
||||
<hr class="divider">
|
||||
|
||||
<section id="idocs_general">
|
||||
<h2>General client usage</h2>
|
||||
<p>All clients work with the same principles:</p>
|
||||
<ul>
|
||||
<li>Mandatory parameters are non-nullable while optional parameters are nullable and will have a default value of null.</li>
|
||||
<li>Any operation will return a form of <code>CallResult</code>. This result can and should be check for success. The clients will not throw exceptions.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<!-- HTML Structure
|
||||
============================ -->
|
||||
<section id="idocs_rest">
|
||||
|
Loading…
x
Reference in New Issue
Block a user