mirror of
				https://github.com/JKorf/CryptoExchange.Net
				synced 2025-10-31 02:17:45 +00:00 
			
		
		
		
	Added DecimalStringWriter json converter, added support for specifying body content type on a per request basis
This commit is contained in:
		
							parent
							
								
									104ac7caad
								
							
						
					
					
						commit
						e2dde77023
					
				| @ -115,6 +115,7 @@ namespace CryptoExchange.Net.UnitTests | |||||||
|                 TimeSpan.FromSeconds(1), |                 TimeSpan.FromSeconds(1), | ||||||
|                 null, |                 null, | ||||||
|                 "{}", |                 "{}", | ||||||
|  |                 1, | ||||||
|                 "https://test.com/api", |                 "https://test.com/api", | ||||||
|                 null, |                 null, | ||||||
|                 HttpMethod.Get, |                 HttpMethod.Get, | ||||||
| @ -143,6 +144,7 @@ namespace CryptoExchange.Net.UnitTests | |||||||
|                 TimeSpan.FromSeconds(1), |                 TimeSpan.FromSeconds(1), | ||||||
|                 null, |                 null, | ||||||
|                 "{}", |                 "{}", | ||||||
|  |                 1, | ||||||
|                 "https://test.com/api", |                 "https://test.com/api", | ||||||
|                 null, |                 null, | ||||||
|                 HttpMethod.Get, |                 HttpMethod.Get, | ||||||
|  | |||||||
| @ -83,6 +83,7 @@ namespace CryptoExchange.Net | |||||||
|         /// <param name="cancellationToken">Cancellation token</param> |         /// <param name="cancellationToken">Cancellation token</param> | ||||||
|         /// <param name="parameters">The parameters of the request</param> |         /// <param name="parameters">The parameters of the request</param> | ||||||
|         /// <param name="signed">Whether or not the request should be authenticated</param> |         /// <param name="signed">Whether or not the request should be authenticated</param> | ||||||
|  |         /// <param name="requestBodyFormat">The format of the body content</param> | ||||||
|         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> |         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> | ||||||
|         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> |         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> | ||||||
|         /// <param name="requestWeight">Credits used for the request</param> |         /// <param name="requestWeight">Credits used for the request</param> | ||||||
| @ -97,6 +98,7 @@ namespace CryptoExchange.Net | |||||||
|             CancellationToken cancellationToken, |             CancellationToken cancellationToken, | ||||||
|             Dictionary<string, object>? parameters = null, |             Dictionary<string, object>? parameters = null, | ||||||
|             bool signed = false, |             bool signed = false, | ||||||
|  |             RequestBodyFormat? requestBodyFormat = null, | ||||||
|             HttpMethodParameterPosition? parameterPosition = null, |             HttpMethodParameterPosition? parameterPosition = null, | ||||||
|             ArrayParametersSerialization? arraySerialization = null, |             ArrayParametersSerialization? arraySerialization = null, | ||||||
|             int requestWeight = 1, |             int requestWeight = 1, | ||||||
| @ -108,7 +110,7 @@ namespace CryptoExchange.Net | |||||||
|             while (true) |             while (true) | ||||||
|             { |             { | ||||||
|                 currentTry++; |                 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) |                 if (!request) | ||||||
|                     return new WebCallResult(request.Error!); |                     return new WebCallResult(request.Error!); | ||||||
| 
 | 
 | ||||||
| @ -134,6 +136,7 @@ namespace CryptoExchange.Net | |||||||
|         /// <param name="cancellationToken">Cancellation token</param> |         /// <param name="cancellationToken">Cancellation token</param> | ||||||
|         /// <param name="parameters">The parameters of the request</param> |         /// <param name="parameters">The parameters of the request</param> | ||||||
|         /// <param name="signed">Whether or not the request should be authenticated</param> |         /// <param name="signed">Whether or not the request should be authenticated</param> | ||||||
|  |         /// <param name="requestBodyFormat">The format of the body content</param> | ||||||
|         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> |         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> | ||||||
|         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> |         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> | ||||||
|         /// <param name="requestWeight">Credits used for the request</param> |         /// <param name="requestWeight">Credits used for the request</param> | ||||||
| @ -148,6 +151,7 @@ namespace CryptoExchange.Net | |||||||
|             CancellationToken cancellationToken, |             CancellationToken cancellationToken, | ||||||
|             Dictionary<string, object>? parameters = null, |             Dictionary<string, object>? parameters = null, | ||||||
|             bool signed = false, |             bool signed = false, | ||||||
|  |             RequestBodyFormat? requestBodyFormat = null, | ||||||
|             HttpMethodParameterPosition? parameterPosition = null, |             HttpMethodParameterPosition? parameterPosition = null, | ||||||
|             ArrayParametersSerialization? arraySerialization = null, |             ArrayParametersSerialization? arraySerialization = null, | ||||||
|             int requestWeight = 1, |             int requestWeight = 1, | ||||||
| @ -160,7 +164,7 @@ namespace CryptoExchange.Net | |||||||
|             while (true) |             while (true) | ||||||
|             { |             { | ||||||
|                 currentTry++; |                 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) |                 if (!request) | ||||||
|                     return new WebCallResult<T>(request.Error!); |                     return new WebCallResult<T>(request.Error!); | ||||||
| 
 | 
 | ||||||
| @ -185,6 +189,7 @@ namespace CryptoExchange.Net | |||||||
|         /// <param name="cancellationToken">Cancellation token</param> |         /// <param name="cancellationToken">Cancellation token</param> | ||||||
|         /// <param name="parameters">The parameters of the request</param> |         /// <param name="parameters">The parameters of the request</param> | ||||||
|         /// <param name="signed">Whether or not the request should be authenticated</param> |         /// <param name="signed">Whether or not the request should be authenticated</param> | ||||||
|  |         /// <param name="requestBodyFormat">The format of the body content</param> | ||||||
|         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> |         /// <param name="parameterPosition">Where the parameters should be placed, overwrites the value set in the client</param> | ||||||
|         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> |         /// <param name="arraySerialization">How array parameters should be serialized, overwrites the value set in the client</param> | ||||||
|         /// <param name="requestWeight">Credits used for the request</param> |         /// <param name="requestWeight">Credits used for the request</param> | ||||||
| @ -198,6 +203,7 @@ namespace CryptoExchange.Net | |||||||
|             CancellationToken cancellationToken, |             CancellationToken cancellationToken, | ||||||
|             Dictionary<string, object>? parameters = null, |             Dictionary<string, object>? parameters = null, | ||||||
|             bool signed = false, |             bool signed = false, | ||||||
|  |             RequestBodyFormat? requestBodyFormat = null, | ||||||
|             HttpMethodParameterPosition? parameterPosition = null, |             HttpMethodParameterPosition? parameterPosition = null, | ||||||
|             ArrayParametersSerialization? arraySerialization = null, |             ArrayParametersSerialization? arraySerialization = null, | ||||||
|             int requestWeight = 1, |             int requestWeight = 1, | ||||||
| @ -242,7 +248,7 @@ namespace CryptoExchange.Net | |||||||
| 
 | 
 | ||||||
|             _logger.Log(LogLevel.Information, $"[{requestId}] Creating request for " + uri); |             _logger.Log(LogLevel.Information, $"[{requestId}] Creating request for " + uri); | ||||||
|             var paramsPosition = parameterPosition ?? ParameterPositions[method]; |             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 = ""; |             string? paramString = ""; | ||||||
|             if (paramsPosition == HttpMethodParameterPosition.InBody) |             if (paramsPosition == HttpMethodParameterPosition.InBody) | ||||||
| @ -417,6 +423,7 @@ namespace CryptoExchange.Net | |||||||
|         /// <param name="signed">Whether or not the request should be authenticated</param> |         /// <param name="signed">Whether or not the request should be authenticated</param> | ||||||
|         /// <param name="parameterPosition">Where the parameters should be placed</param> |         /// <param name="parameterPosition">Where the parameters should be placed</param> | ||||||
|         /// <param name="arraySerialization">How array parameters should be serialized</param> |         /// <param name="arraySerialization">How array parameters should be serialized</param> | ||||||
|  |         /// <param name="bodyFormat">Format of the body content</param> | ||||||
|         /// <param name="requestId">Unique id of a request</param> |         /// <param name="requestId">Unique id of a request</param> | ||||||
|         /// <param name="additionalHeaders">Additional headers to send with the request</param> |         /// <param name="additionalHeaders">Additional headers to send with the request</param> | ||||||
|         /// <returns></returns> |         /// <returns></returns> | ||||||
| @ -427,6 +434,7 @@ namespace CryptoExchange.Net | |||||||
|             bool signed, |             bool signed, | ||||||
|             HttpMethodParameterPosition parameterPosition, |             HttpMethodParameterPosition parameterPosition, | ||||||
|             ArrayParametersSerialization arraySerialization, |             ArrayParametersSerialization arraySerialization, | ||||||
|  |             RequestBodyFormat bodyFormat, | ||||||
|             int requestId, |             int requestId, | ||||||
|             Dictionary<string, string>? additionalHeaders) |             Dictionary<string, string>? additionalHeaders) | ||||||
|         { |         { | ||||||
|  | |||||||
| @ -0,0 +1,27 @@ | |||||||
|  | using Newtonsoft.Json; | ||||||
|  | using System; | ||||||
|  | using System.Globalization; | ||||||
|  | 
 | ||||||
|  | namespace Kraken.Net.Converters | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// Converter for serializing decimal values as string | ||||||
|  |     /// </summary> | ||||||
|  |     public class DecimalStringWriterConverter : JsonConverter | ||||||
|  |     { | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         public override bool CanRead => false; | ||||||
|  | 
 | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         public override bool CanConvert(Type objectType) => objectType == typeof(decimal) || objectType == typeof(decimal?); | ||||||
|  |          | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) | ||||||
|  |         { | ||||||
|  |             throw new NotImplementedException(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         /// <inheritdoc /> | ||||||
|  |         public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) => writer.WriteValue(((decimal?)value)?.ToString(CultureInfo.InvariantCulture) ?? null); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user