1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-17 11:23:00 +00:00

Feature/9.0.0 (#236)

* Added support for Native AOT compilation
* Updated all IEnumerable response types to array response types
* Added Pass support for ApiCredentials, removing the need for most implementations to add their own ApiCredentials type
* Added KeepAliveTimeout setting setting ping frame timeouts for SocketApiClient
* Added IBookTickerRestClient Shared interface for requesting book tickers
* Added ISpotTriggerOrderRestClient Shared interface for managing spot trigger orders
* Added ISpotOrderClientIdClient Shared interface for managing spot orders by client order id
* Added IFuturesTriggerOrderRestClient Shared interface for managing futures trigger orders
* Added IFuturesOrderClientIdClient Shared interface for managing futures orders by client order id
* Added IFuturesTpSlRestClient Shared interface for setting TP/SL on open futures positions
* Added GenerateClientOrderId to ISpotOrderRestClient and IFuturesOrderRestClient interface
* Added OptionalExchangeParameters and Supported properties to EndpointOptions
* Refactor Shared interfaces quantity parameters and properties to use SharedQuantity
* Added SharedSymbol property to Shared interface models returning a symbol
* Added TriggerPrice, IsTriggerOrder, TakeProfitPrice, StopLossPrice and IsCloseOrder to SharedFuturesOrder response model
* Added MaxShortLeverage and MaxLongLeverage to SharedFuturesSymbol response model
* Added StopLossPrice and TakeProfitPrice to SharedPosition response model
* Added TriggerPrice and IsTriggerOrder to SharedSpotOrder response model
* Added QuoteVolume property to SharedSpotTicker response model
* Added AssetAlias configuration models
* Added static ExchangeSymbolCache for tracking symbol information from exchanges
* Added static CallResult.SuccessResult to be used instead of constructing success CallResult instance
* Added static ApplyRules, RandomHexString and RandomLong helper methods to ExchangeHelpers class
* Added AsErrorWithData To CallResult
* Added OriginalData property to CallResult
* Added support for adjusting the rate limit key per call, allowing for ratelimiting depending on request parameters
* Added implementation for integration testing ISymbolOrderBook instances
* Added implementation for integration testing socket subscriptions
* Added implementation for testing socket queries
* Updated request cancellation logging to Debug level
* Updated logging SourceContext to include the client type
* Updated some logging logic, errors no longer contain any data, exception are not logged as string but instead forwarded to structured logging
* Fixed warning for Enum parsing throwing exception and output warnings for each object in a response to only once to prevent slowing down execution
* Fixed memory leak in AsyncAutoRestEvent
* Fixed logging for ping frame timeout
* Fixed warning getting logged when user stops SymbolOrderBook instance
* Fixed socket client `UnsubscribeAll` not unsubscribing dedicated connections
* Fixed memory leak in Rest client cache
* Fixed integers bigger than int16 not getting correctly parsed to enums
* Fixed issue where the default options were overridden when using SetApiCredentials
* Removed Newtonsoft.Json dependency
* Removed legacy Rest client code
* Removed legacy ISpotClient and IFuturesClient support
This commit is contained in:
Jan Korf
2025-05-13 10:15:30 +02:00
committed by GitHub
parent 3d6267da93
commit 6b14cdbf06
182 changed files with 3159 additions and 3950 deletions
@@ -4,10 +4,11 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.SystemTextJson;
using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net.Testing.Comparers
{
@@ -15,14 +16,13 @@ namespace CryptoExchange.Net.Testing.Comparers
{
internal static void CompareData(
string method,
object resultData,
object? resultData,
string json,
string? nestedJsonProperty,
List<string>? ignoreProperties = null,
bool userSingleArrayItem = false)
{
var resultProperties = resultData.GetType().GetProperties().Select(p => (p, (JsonPropertyNameAttribute?)p.GetCustomAttributes(typeof(JsonPropertyNameAttribute), true).SingleOrDefault()));
var jsonObject = JToken.Parse(json);
var jsonObject = JsonDocument.Parse(json).RootElement;
if (nestedJsonProperty != null)
{
var nested = nestedJsonProperty.Split('.');
@@ -31,32 +31,43 @@ namespace CryptoExchange.Net.Testing.Comparers
if (int.TryParse(nest, out var index))
jsonObject = jsonObject![index];
else
jsonObject = jsonObject![nest];
jsonObject = jsonObject!.GetProperty(nest);
}
}
if (userSingleArrayItem)
jsonObject = ((JArray)jsonObject!)[0];
jsonObject = jsonObject[0];
if (resultData == null)
{
if (jsonObject.ValueKind == JsonValueKind.Null)
return;
if (jsonObject.ValueKind == JsonValueKind.Object && jsonObject.GetPropertyCount() == 0)
return;
throw new Exception("ResultData null");
}
if (resultData.GetType().GetInterfaces().Contains(typeof(IDictionary)))
{
var dict = (IDictionary)resultData;
var jObj = (JObject)jsonObject!;
var properties = jObj.Properties();
foreach (var dictProp in properties)
var jObj = jsonObject!;
foreach (var dictProp in jObj.EnumerateObject())
{
if (!dict.Contains(dictProp.Name))
throw new Exception($"{method}: Dictionary has no value for {dictProp.Name} while input json `{dictProp.Name}` has value {dictProp.Value}");
if (dictProp.Value.Type == JTokenType.Object)
if (dictProp.Value.ValueKind == JsonValueKind.Object)
{
// TODO Some additional checking for objects
foreach (var prop in ((JObject)dictProp.Value).Properties())
foreach (var prop in dictProp.Value.EnumerateObject())
CheckObject(method, prop, dict[dictProp.Name]!, ignoreProperties!);
}
else
{
if (dict[dictProp.Name] == default && dictProp.Value.Type != JTokenType.Null)
if (dict[dictProp.Name] == default && dictProp.Value.ValueKind != JsonValueKind.Null)
{
if (dictProp.Value.ToString() == "")
continue;
@@ -67,28 +78,27 @@ namespace CryptoExchange.Net.Testing.Comparers
}
}
}
else if (jsonObject!.Type == JTokenType.Array)
else if (jsonObject!.ValueKind == JsonValueKind.Array)
{
var jArray = (JArray)jsonObject;
if (resultData is IEnumerable list)
{
var enumerator = list.GetEnumerator();
foreach (var jObj in jArray)
foreach (var jObj in jsonObject.EnumerateArray())
{
if (!enumerator.MoveNext())
{
}
if (jObj.Type == JTokenType.Object)
if (jObj.ValueKind == JsonValueKind.Object)
{
foreach (var subProp in ((JObject)jObj).Properties())
foreach (var subProp in jObj.EnumerateObject())
{
if (ignoreProperties?.Contains(subProp.Name) == true)
continue;
CheckObject(method, subProp, enumerator.Current, ignoreProperties!);
}
}
else if (jObj.Type == JTokenType.Array)
else if (jObj.ValueKind == JsonValueKind.Array)
{
var resultObj = enumerator.Current;
if (resultObj is string)
@@ -98,23 +108,23 @@ namespace CryptoExchange.Net.Testing.Comparers
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
if (jsonConverter != typeof(ArrayConverter))
if (jsonConverter != typeof(ArrayConverter<>))
// Not array converter?
continue;
int i = 0;
foreach (var item in jObj.Children())
foreach (var item in jObj.EnumerateObject())
{
var arrayProp = resultProps.Where(p => p.Item2 != null).FirstOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
CheckPropertyValue(method, item.Value, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
}
}
else
{
var value = enumerator.Current;
if (value == default && ((JValue)jObj).Type != JTokenType.Null)
if (value == default && jObj.ValueKind != JsonValueKind.Null)
throw new Exception($"{method}: Array has no value while input json array has value {jObj}");
}
}
@@ -123,33 +133,37 @@ namespace CryptoExchange.Net.Testing.Comparers
{
var resultProps = resultData.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
int i = 0;
foreach (var item in jArray.Children())
foreach (var item in jsonObject.EnumerateArray())
{
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).FirstOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultData), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
}
}
}
else
else if (jsonObject.ValueKind == JsonValueKind.Object)
{
foreach (var item in jsonObject)
foreach (var item in jsonObject.EnumerateObject())
{
if (item is JProperty prop)
{
if (ignoreProperties?.Contains(prop.Name) == true)
//if (item is JProperty prop)
//{
if (ignoreProperties?.Contains(item.Name) == true)
continue;
CheckObject(method, prop, resultData, ignoreProperties);
}
CheckObject(method, item, resultData, ignoreProperties);
//}
}
}
else
{
//?
}
Debug.WriteLine($"Successfully validated {method}");
}
private static void CheckObject(string method, JProperty prop, object obj, List<string>? ignoreProperties)
private static void CheckObject(string method, JsonProperty prop, object obj, List<string>? ignoreProperties)
{
var publicProperties = obj.GetType().GetProperties(
System.Reflection.BindingFlags.Public
@@ -184,9 +198,9 @@ namespace CryptoExchange.Net.Testing.Comparers
CheckPropertyValue(method, prop.Value, propertyValue, property.PropertyType, property.Name, prop.Name, ignoreProperties);
}
private static void CheckPropertyValue(string method, JToken propValue, object? propertyValue, Type propertyType, string? propertyName = null, string? propName = null, List<string>? ignoreProperties = null)
private static void CheckPropertyValue(string method, JsonElement propValue, object? propertyValue, Type propertyType, string? propertyName = null, string? propName = null, List<string>? ignoreProperties = null)
{
if (propertyValue == default && propValue.Type != JTokenType.Null && !string.IsNullOrEmpty(propValue.ToString()))
if (propertyValue == default && propValue.ValueKind != JsonValueKind.Null && !string.IsNullOrEmpty(propValue.ToString()))
{
if (propertyType == typeof(DateTime?) && (propValue.ToString() == "" || propValue.ToString() == "0" || propValue.ToString() == "-1" || propValue.ToString() == "01/01/0001 00:00:00"))
return;
@@ -196,26 +210,24 @@ namespace CryptoExchange.Net.Testing.Comparers
throw new Exception($"{method}: Property `{propertyName}` has no value while input json `{propName}` has value {propValue}");
}
if ((propertyValue == default && (propValue.Type == JTokenType.Null || string.IsNullOrEmpty(propValue.ToString()))) || propValue.ToString() == "0")
if ((propertyValue == default && (propValue.ValueKind == JsonValueKind.Null || string.IsNullOrEmpty(propValue.ToString()))) || propValue.ToString() == "0")
return;
if (propertyValue!.GetType().GetInterfaces().Contains(typeof(IDictionary)))
{
var dict = (IDictionary)propertyValue;
var jObj = (JObject)propValue;
var properties = jObj.Properties();
foreach (var dictProp in properties)
foreach (var dictProp in propValue.EnumerateObject())
{
if (!dict.Contains(dictProp.Name))
throw new Exception($"{method}: Property `{propertyName}` has no value while input json `{propName}` has value {propValue}");
if (dictProp.Value.Type == JTokenType.Object)
if (dictProp.Value.ValueKind == JsonValueKind.Object)
{
CheckPropertyValue(method, dictProp.Value, dict[dictProp.Name]!, dict[dictProp.Name]!.GetType(), null, null, ignoreProperties);
}
else
{
if (dict[dictProp.Name] == default && dictProp.Value.Type != JTokenType.Null)
if (dict[dictProp.Name] == default && dictProp.Value.ValueKind != JsonValueKind.Null)
// Property value not correct
throw new Exception($"{method}: Dictionary entry `{dictProp.Name}` has no value while input json has value {propValue} for");
}
@@ -224,26 +236,25 @@ namespace CryptoExchange.Net.Testing.Comparers
else if (propertyValue.GetType().GetInterfaces().Contains(typeof(IEnumerable))
&& propertyValue.GetType() != typeof(string))
{
if (propValue.Type != JTokenType.Array)
if (propValue.ValueKind != JsonValueKind.Array)
return;
var jArray = (JArray)propValue;
var list = (IEnumerable)propertyValue;
var enumerator = list.GetEnumerator();
foreach (JToken jToken in jArray)
foreach (var jToken in propValue.EnumerateArray())
{
var moved = enumerator.MoveNext();
if (!moved)
throw new Exception("Enumeration not moved; incorrect amount of results?");
var typeConverter = enumerator.Current.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true);
if (typeConverter.Length != 0 && ((JsonConverterAttribute)typeConverter.First()).ConverterType != typeof(ArrayConverter))
if (typeConverter.Length != 0 && ((JsonConverterAttribute)typeConverter.First()).ConverterType != typeof(ArrayConverter<>))
// Custom converter for the type, skip
continue;
if (jToken.Type == JTokenType.Object)
if (jToken.ValueKind == JsonValueKind.Object)
{
foreach (var subProp in ((JObject)jToken).Properties())
foreach (var subProp in jToken.EnumerateObject())
{
if (ignoreProperties?.Contains(subProp.Name) == true)
continue;
@@ -251,18 +262,18 @@ namespace CryptoExchange.Net.Testing.Comparers
CheckObject(method, subProp, enumerator.Current, ignoreProperties);
}
}
else if (jToken.Type == JTokenType.Array)
else if (jToken.ValueKind == JsonValueKind.Array)
{
var resultObj = enumerator.Current;
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
if (jsonConverter != typeof(ArrayConverter))
if (jsonConverter != typeof(ArrayConverter<>))
// Not array converter?
continue;
int i = 0;
foreach (var item in jToken.Children())
foreach (var item in jToken.EnumerateArray())
{
var arrayProp = resultProps.Where(p => p.Item2 != null).FirstOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
@@ -274,61 +285,60 @@ namespace CryptoExchange.Net.Testing.Comparers
else
{
var value = enumerator.Current;
if (value == default && ((JValue)jToken).Type != JTokenType.Null)
if (value == default && jToken.ValueKind != JsonValueKind.Null)
throw new Exception($"{method}: Property `{propertyName}` has no value while input json `{propName}` has value {jToken}");
CheckValues(method, propertyName!, propertyType, (JValue)jToken, value!);
CheckValues(method, propertyName!, propertyType, jToken, value!);
}
}
}
else
{
if (propValue.Type == JTokenType.Object)
if (propValue.ValueKind == JsonValueKind.Object)
{
foreach (var item in propValue)
foreach (var item in propValue.EnumerateObject())
{
if (item is JProperty prop)
{
if (ignoreProperties?.Contains(prop.Name) == true)
//if (item is JProperty prop)
//{
if (ignoreProperties?.Contains(item.Name) == true)
continue;
CheckObject(method, prop, propertyValue, ignoreProperties);
}
CheckObject(method, item, propertyValue, ignoreProperties);
//}
}
}
else if (propValue.Type == JTokenType.Array)
else if (propValue.ValueKind == JsonValueKind.Array)
{
var jArray = (JArray)propValue;
if (propertyValue is IEnumerable list)
{
var enumerator = list.GetEnumerator();
foreach (var jObj in jArray)
foreach (var jObj in propValue.EnumerateArray())
{
if (!enumerator.MoveNext())
{
}
if (jObj.Type == JTokenType.Object)
if (jObj.ValueKind == JsonValueKind.Object)
{
foreach (var subProp in ((JObject)jObj).Properties())
foreach (var subProp in jObj.EnumerateObject())
{
if (ignoreProperties?.Contains(subProp.Name) == true)
continue;
CheckObject(method, subProp, enumerator.Current, ignoreProperties!);
}
}
else if (jObj.Type == JTokenType.Array)
else if (jObj.ValueKind == JsonValueKind.Array)
{
var resultObj = enumerator.Current;
var resultProps = resultObj.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
var arrayConverterProperty = resultObj.GetType().GetCustomAttributes(typeof(JsonConverterAttribute), true).FirstOrDefault();
var jsonConverter = ((JsonConverterAttribute)arrayConverterProperty!).ConverterType;
if (jsonConverter != typeof(ArrayConverter))
if (jsonConverter != typeof(ArrayConverter<>))
// Not array converter?
continue;
int i = 0;
foreach (var item in jObj.Values())
foreach (var item in jObj.EnumerateArray())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
@@ -339,7 +349,7 @@ namespace CryptoExchange.Net.Testing.Comparers
else
{
var value = enumerator.Current;
if (value == default && ((JValue)jObj).Type != JTokenType.Null)
if (value == default && jObj.ValueKind != JsonValueKind.Null)
throw new Exception($"{method}: Array has no value while input json array has value {jObj}");
}
}
@@ -348,7 +358,7 @@ namespace CryptoExchange.Net.Testing.Comparers
{
var resultProps = propertyValue.GetType().GetProperties().Select(p => (p, p.GetCustomAttributes(typeof(ArrayPropertyAttribute), true).Cast<ArrayPropertyAttribute>().SingleOrDefault()));
int i = 0;
foreach (var item in jArray.Children())
foreach (var item in propValue.EnumerateArray())
{
var arrayProp = resultProps.Where(p => p.Item2 != null).FirstOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
@@ -359,63 +369,78 @@ namespace CryptoExchange.Net.Testing.Comparers
}
else
{
CheckValues(method, propertyName!, propertyType, (JValue)propValue, propertyValue);
CheckValues(method, propertyName!, propertyType, propValue, propertyValue);
}
}
}
private static void CheckValues(string method, string property, Type propertyType, JValue jsonValue, object objectValue)
private static void CheckValues(string method, string property, Type propertyType, JsonElement jsonValue, object objectValue)
{
if (jsonValue.Type == JTokenType.String)
if (jsonValue.ValueKind == JsonValueKind.String)
{
var stringValue = jsonValue.GetString();
if (objectValue is decimal dec)
{
if (jsonValue.Value<decimal>() != dec)
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<decimal>()} vs {dec}");
if (decimal.Parse(stringValue!, CultureInfo.InvariantCulture) != dec)
throw new Exception($"{method}: {property} not equal: {stringValue} vs {dec}");
}
else if (objectValue is DateTime time)
{
var jsonStr = jsonValue.Value<string>()!;
if (!string.IsNullOrEmpty(jsonStr) && time != DateTimeConverter.ParseFromString(jsonStr))
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<string>()} vs {time}");
if (!string.IsNullOrEmpty(stringValue) && time != DateTimeConverter.ParseFromString(stringValue!))
throw new Exception($"{method}: {property} not equal: {stringValue} vs {time}");
}
else if (objectValue is bool bl)
{
var jsonStr = jsonValue.Value<string>();
if (bl && (jsonStr != "1" && jsonStr != "true" && jsonStr != "True"))
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<string>()} vs {bl}");
if (!bl && (jsonStr != "0" && jsonStr != "-1" && jsonStr != "false" && jsonStr != "False"))
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<string>()} vs {bl}");
if (bl && (stringValue != "1" && stringValue != "true" && stringValue != "True"))
throw new Exception($"{method}: {property} not equal: {stringValue} vs {bl}");
if (!bl && (stringValue != "0" && stringValue != "-1" && stringValue != "false" && stringValue != "False"))
throw new Exception($"{method}: {property} not equal: {stringValue} vs {bl}");
}
else if (propertyType.IsEnum || Nullable.GetUnderlyingType(propertyType)?.IsEnum == true)
{
// TODO enum comparing
}
else if (!jsonValue.Value<string>()!.Equals(Convert.ToString(objectValue, CultureInfo.InvariantCulture), StringComparison.InvariantCultureIgnoreCase))
else if (!stringValue!.Equals(Convert.ToString(objectValue, CultureInfo.InvariantCulture), StringComparison.InvariantCultureIgnoreCase))
{
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<string>()} vs {objectValue}");
throw new Exception($"{method}: {property} not equal: {stringValue} vs {objectValue}");
}
}
else if (jsonValue.Type == JTokenType.Integer)
else if (jsonValue.ValueKind == JsonValueKind.Number)
{
var value = jsonValue.GetDecimal();
if (objectValue is DateTime time)
{
if (time != DateTimeConverter.ParseFromDouble(jsonValue.Value<long>()!))
throw new Exception($"{method}: {property} not equal: {DateTimeConverter.ParseFromDouble(jsonValue.Value<long>()!)} vs {time}");
if (time != DateTimeConverter.ParseFromDouble((double)value))
throw new Exception($"{method}: {property} not equal: {DateTimeConverter.ParseFromDouble((double)value!)} vs {time}");
}
else if (propertyType.IsEnum || Nullable.GetUnderlyingType(propertyType)?.IsEnum == true)
{
// TODO enum comparing
}
else if (jsonValue.Value<long>() != Convert.ToInt64(objectValue))
else if(objectValue is decimal dec)
{
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<long>()} vs {Convert.ToInt64(objectValue)}");
if (dec != value)
throw new Exception($"{method}: {property} not equal: {dec} vs {value}");
}
else if (objectValue is double dbl)
{
if ((decimal)dbl != value)
throw new Exception($"{method}: {property} not equal: {dbl} vs {value}");
}
else if(objectValue is string objStr)
{
if (objStr != value.ToString())
throw new Exception($"{method}: {property} not equal: {value} vs {objStr}");
}
else if (value != Convert.ToInt64(objectValue, CultureInfo.InvariantCulture))
{
throw new Exception($"{method}: {property} not equal: {value} vs {Convert.ToInt64(objectValue)}");
}
}
else if (jsonValue.Type == JTokenType.Boolean)
else if (jsonValue.ValueKind == JsonValueKind.True || jsonValue.ValueKind == JsonValueKind.False)
{
if (jsonValue.Value<bool>() != (bool)objectValue)
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<bool>()} vs {(bool)objectValue}");
if (jsonValue.GetBoolean() != (bool)objectValue)
throw new Exception($"{method}: {property} not equal: {jsonValue.GetBoolean()} vs {(bool)objectValue}");
}
}
}