1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-12 08:53:01 +00:00

Compare commits

..

5 Commits

Author SHA1 Message Date
JKorf 0a0c66541e Updated to version 7.7.3 2024-06-26 19:11:13 +02:00
JKorf bb4199620e Added caching docs 2024-06-26 15:33:31 +02:00
JKorf 8a83cd2cb8 Array comparison updates for unit tests 2024-06-26 15:13:21 +02:00
JKorf fcfeaf568f Fixed request ids not matching 2024-06-26 11:21:52 +02:00
JKorf 25567ea434 Added nullable int converter for System.Text.Json 2024-06-25 20:52:35 +02:00
8 changed files with 122 additions and 23 deletions
+8 -3
View File
@@ -215,11 +215,14 @@ namespace CryptoExchange.Net.Clients
while (true)
{
currentTry++;
var prepareResult = await PrepareAsync(baseAddress, definition, cancellationToken, additionalHeaders, weight).ConfigureAwait(false);
var requestId = ExchangeHelpers.NextId();
var prepareResult = await PrepareAsync(requestId, baseAddress, definition, cancellationToken, additionalHeaders, weight).ConfigureAwait(false);
if (!prepareResult)
return new WebCallResult<T>(prepareResult.Error!);
var request = CreateRequest(
requestId,
baseAddress,
definition,
uriParameters,
@@ -249,6 +252,7 @@ namespace CryptoExchange.Net.Clients
/// <summary>
/// Prepare before sending a request. Sync time between client and server and check rate limits
/// </summary>
/// <param name="requestId">Request id</param>
/// <param name="baseAddress">Host and schema</param>
/// <param name="definition">Request definition</param>
/// <param name="cancellationToken">Cancellation token</param>
@@ -257,13 +261,13 @@ namespace CryptoExchange.Net.Clients
/// <returns></returns>
/// <exception cref="Exception"></exception>
protected virtual async Task<CallResult> PrepareAsync(
int requestId,
string baseAddress,
RequestDefinition definition,
CancellationToken cancellationToken,
Dictionary<string, string>? additionalHeaders = null,
int? weight = null)
{
var requestId = ExchangeHelpers.NextId();
var requestWeight = weight ?? definition.Weight;
// Time sync
@@ -324,6 +328,7 @@ namespace CryptoExchange.Net.Clients
/// <summary>
/// Creates a request object
/// </summary>
/// <param name="requestId">Id of the request</param>
/// <param name="baseAddress">Host and schema</param>
/// <param name="definition">Request definition</param>
/// <param name="uriParameters">The query parameters of the request</param>
@@ -331,6 +336,7 @@ namespace CryptoExchange.Net.Clients
/// <param name="additionalHeaders">Additional headers to send with the request</param>
/// <returns></returns>
protected virtual IRequest CreateRequest(
int requestId,
string baseAddress,
RequestDefinition definition,
ParameterCollection? uriParameters,
@@ -343,7 +349,6 @@ namespace CryptoExchange.Net.Clients
var uri = new Uri(baseAddress.AppendPath(definition.Path));
var arraySerialization = definition.ArraySerialization ?? ArraySerialization;
var bodyFormat = definition.RequestBodyFormat ?? RequestBodyFormat;
var requestId = ExchangeHelpers.NextId();
var parameterPosition = definition.ParameterPosition ?? ParameterPositions[definition.Method];
var headers = new Dictionary<string, string>();
@@ -0,0 +1,40 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CryptoExchange.Net.Converters.SystemTextJson
{
/// <summary>
/// Int converter
/// </summary>
public class IntConverter : JsonConverter<int?>
{
/// <inheritdoc />
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
if (reader.TokenType == JsonTokenType.String)
{
var value = reader.GetString();
if (string.IsNullOrEmpty(value))
return null;
return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
}
return reader.GetInt32();
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else
writer.WriteNumberValue(value.Value);
}
}
}
@@ -21,6 +21,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
new EnumConverter(),
new BoolConverter(),
new DecimalConverter(),
new IntConverter()
}
};
}
+3 -3
View File
@@ -6,9 +6,9 @@
<PackageId>CryptoExchange.Net</PackageId>
<Authors>JKorf</Authors>
<Description>CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.</Description>
<PackageVersion>7.7.2</PackageVersion>
<AssemblyVersion>7.7.2</AssemblyVersion>
<FileVersion>7.7.2</FileVersion>
<PackageVersion>7.7.3</PackageVersion>
<AssemblyVersion>7.7.3</AssemblyVersion>
<FileVersion>7.7.3</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>OKX;OKX.Net;Mexc;Mexc.Net;Kucoin;Kucoin.Net;Kraken;Kraken.Net;Huobi;Huobi.Net;CoinEx;CoinEx.Net;Bybit;Bybit.Net;Bitget;Bitget.Net;Bitfinex;Bitfinex.Net;Binance;Binance.Net;CryptoCurrency;CryptoCurrency Exchange</PackageTags>
<RepositoryType>git</RepositoryType>
@@ -29,7 +29,12 @@ namespace CryptoExchange.Net.Testing.Comparers
{
var nested = nestedJsonProperty.Split('.');
foreach (var nest in nested)
jsonObject = jsonObject![nest];
{
if (int.TryParse(nest, out var index))
jsonObject = jsonObject![index];
else
jsonObject = jsonObject![nest];
}
}
if (userSingleArrayItem)
@@ -92,9 +97,9 @@ namespace CryptoExchange.Net.Testing.Comparers
continue;
int i = 0;
foreach (var item in jObj.Values())
foreach (var item in jObj.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
@@ -112,9 +117,9 @@ 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 jObjs.Values())
foreach (var item in jObjs.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultData), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
@@ -228,11 +233,11 @@ namespace CryptoExchange.Net.Testing.Comparers
continue;
int i = 0;
foreach (var item in jtoken.Values())
foreach (var item in jtoken.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), propertyType, arrayProp.Name, "Array index " + i, ignoreProperties);
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
}
@@ -270,7 +275,10 @@ namespace CryptoExchange.Net.Testing.Comparers
var enumerator = list.GetEnumerator();
foreach (var jObj in jObjs)
{
enumerator.MoveNext();
if (!enumerator.MoveNext())
{
}
if (jObj.Type == JTokenType.Object)
{
foreach (var subProp in ((JObject)jObj).Properties())
@@ -311,9 +319,9 @@ 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 jObjs.Values())
foreach (var item in jObjs.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(propertyValue), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
@@ -26,8 +26,13 @@ namespace CryptoExchange.Net.Testing.Comparers
if (nestedJsonProperty != null)
{
var nested = nestedJsonProperty.Split('.');
foreach(var nest in nested)
jsonObject = jsonObject![nest];
foreach (var nest in nested)
{
if (int.TryParse(nest, out var index))
jsonObject = jsonObject![index];
else
jsonObject = jsonObject![nest];
}
}
if (userSingleArrayItem)
@@ -94,9 +99,9 @@ namespace CryptoExchange.Net.Testing.Comparers
continue;
int i = 0;
foreach (var item in jObj.Values())
foreach (var item in jObj.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), arrayProp.PropertyType, arrayProp.Name, "Array index " + i, ignoreProperties!);
i++;
@@ -219,9 +224,9 @@ namespace CryptoExchange.Net.Testing.Comparers
continue;
int i = 0;
foreach (var item in jtoken.Values())
foreach (var item in jtoken.Children())
{
var arrayProp = resultProps.SingleOrDefault(p => p.Item2!.Index == i).p;
var arrayProp = resultProps.Where(p => p.Item2 != null).SingleOrDefault(p => p.Item2!.Index == i).p;
if (arrayProp != null)
CheckPropertyValue(method, item, arrayProp.GetValue(resultObj), propertyType, arrayProp.Name, "Array index " + i, ignoreProperties);
+5
View File
@@ -46,6 +46,11 @@ Make a one time donation in a crypto currency of your choice. If you prefer to d
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
## Release notes
* Version 7.7.3 - 26 Jun 2024
* Fixed request ids not matching in logging
* Added nullable int converter for System.Text.Json
* Small fixes in tests
* Version 7.7.2 - 25 Jun 2024
* Fixed ratelimiting issue possibly creating negative delays
+35
View File
@@ -100,6 +100,7 @@
<li class="nav-item"><a class="nav-link" href="#idocs_orderbooks">Orderbooks</a></li>
<li class="nav-item"><a class="nav-link" href="#idocs_logging">Logging</a></li>
<li class="nav-item"><a class="nav-link" href="#idocs_ratelimiting">Ratelimiting</a></li>
<li class="nav-item"><a class="nav-link" href="#idocs_caching">Caching</a></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="#idocs_examples">Examples</a>
@@ -1956,6 +1957,16 @@ var client = new OKXRestClient();</code></pre>
<td>The interval of how often the time synchronization between client and server should be executed</td>
<td><code>TimeSpan.FromHours(1)</code></td>
</tr>
<tr>
<td>CachingEnabled</td>
<td>Whether or not client side caching should be enabled for GET requests, see <a href="#idocs_caching">Caching</a></td>
<td><code>false</code></td>
</tr>
<tr>
<td>CachingMaxAge</td>
<td>The max age of data to return from the cache. If the same data is requested and the data is available in the client side cache and not older than this value the cached value is returned, else a new request will be done</td>
<td><code>TimeSpan.FromSeconds(5)</code></td>
</tr>
<tr>
<td>[API].ApiCredentials</td>
<td>Same as the in the base options, allows overriding per sub-API</td>
@@ -2474,6 +2485,30 @@ var binanceClient = new BinanceRestClient(new HttpClient(), logFactory, options
</div>
</div>
</section>
<section id="idocs_caching">
<h2>Caching</h2>
<p>
Every REST API client based on the CryptoExchange.Net base library automatically supports caching of GET HTTP requests. A few advantages of caching:
<ol>
<li>Performance improvement, data response will be much faster as no roundtrip to the server is needed</li>
<li>Reduced resource usage, returning data from the cache uses less resources than reading the server response, though there is some memory overhead</li>
<li>Prevent rate limiting, the cache can be queried as many times as you like without having to worry about getting rate limited by the server</li>
</ol>
<div class="alert alert-info">Caching is only applied for successful GET requests as GET requests by definition should not change state. Other HTTP method (POST, DELETE, etc) generally do change state, so caching those call would prevent an action being executed.</div>
</p>
<p>
To enable caching for GET requests set <code>CachingEnabled</code> to <code>true</code> in the client options. Optionally set the <code>CachingMaxAge</code> option to the desired value (default is 5 seconds).
</p>
<p>
To determine whether a request has gotten the data from the server or from the local cache the <code>DataSource</code> property on the call result can inspected:
<pre><code>var result = await bitfinexRestClient.SpotApi.Account.Get30DaySummaryAndFeesAsync();
var responseSource = result.DataSource;</code></pre>
</p>
</section>
<hr class="divider">