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

CryptoExchange V12 (#281)

* Result types:
  * (Web)CallResult types are replaced by HttpResult, WebSocketResult and QueryResult with the same logic
  * Updated result types to record type
  * Result creation can be done with (Http/WebSocket/Query)Result.Ok(..) and .Fail(..)
  * Removed implicit result type conversion to bool, `if (result)` no longer works, instead use `if (result.Success)`
  * Replaced CallResult.SuccessResult with CallResult.Ok()
  * Fixed result object nullability hinting, for example Data might be null if Success isn't checked for true

* Parameters & serialization:
  * Added support for `enabled` and `disabled` strings to bool converter
  * Removed ParameterCollection type, has been replaced by Parameters type
  * Removed ArraySerialization, OrderParameters and ParameterOrderComparer properties from RestApiClient, moved to ParameterSerializationsSettings
  * Updated RestRequestConfiguration in AuthenticationProvider.ProcessRequest to contain the full RequestDefinition instead of copied fields	

* Clients:
  * Updated Api client constructor logging parameter from ILogger to ILoggerFactory? 
  * Added Api client constructor exchange name parameter
  * Added ToString overrides on base API types
  * Added Exchange property on BaseApiClient
  * Added ApiCredentials property on IRestApiClient and ISocketApiClient interfaces
  * Updated ILogger source from client name to topic specific client name
  * Removed logging from client creation
  * Fixed BaseRestClient SetApiCredentials not marked as virtual

* Rest:
  * Added BaseAddress to RequestDefinition object
  * Updated RestApiClient AuthenticationProvider logic from private to protected and virtual
  * Removed RestApiClient.SendAsync baseAddress parameter removed
  * Removed RestApiClient.SendAsync without type parameter

* WebSocket:
  * Updated MessageRouting definition into CreateForEvent for subscriptions and CreateForQuery for queries
  * Improved Query type safety with CeateForQuery which allows second parameter for specifying the result type
  * Renamed MessageRouter.CreateWithoutHandler to CreateVoid
  * Updated SocketApiClient.GetSocketConnection to check connection uri instead of Tag for finding compatible connections
  * Removed unused UnhandledMessageExpected property SocketApiClient
  * Fixed issue in SocketApiClient.GetSocketConnection causing requests to always wait the full max 10 seconds when there was a reconnecting socket
	
* Shared APIs:
  * Updated Option definitions to always require the exchange name as first parameter
  * Added missing dedicated option types
  * Added Discover method on ISharedClient interface, returning info on supported capabilities and operations
  * Added SharedRequest GetParamValue helper method accepting multiple parameter names
  * Added ResetStaticExchangeParameters method on ExchangeParameters
  * Added Status property to SharedWithdrawal model
  * Added TradingModes property to SharedBalance model
  * Updated ExchangeSymbolCache to support multiple environments and additional key separation
  * Updated Shared ExchangeParameters parameter names to be case insensitive
  * Updated code comments
  * Replaced ExchangeResult with ExchangeCallResult type
  * Removed AsExchangeResult/ExchangeWebResult
  * Removed TradingMode from the response model, only maintained on models where it makes sense
  * Removed IListenKey support, listen keys now rely on internal management with TokenManager

* Rate limiting:
  * Fixed websocket connection attempts counting towards rate limit even when server could not be reached
  * Removed host from rate limit methods, now part of the already provided RequestDefinition
  * Added amount parameter to RateLimit Reset method to allow partially resetting the limit

* Added TokenManager implementation for automatic listenkey/token management
* Added UserClientProvider base class
* Added async streaming on UserDataTracker items with StreamUpdatesAsync
* Added cancellation token support to UserDataTracker starting
* Added Unit type for non-result types
* Added ServerError constructor taking ErrorType and message to make it easier to create
* Added SupportedEnvironments property to PlatformInfo
* Updated SymbolOrderBook DoResyncAsync to return CallResult instead of CallResult<bool> which was redundant
* Various small performance improvements
This commit is contained in:
Jan Korf
2026-06-29 10:38:09 +02:00
committed by GitHub
parent afb84a1bf0
commit e823114623
285 changed files with 7029 additions and 3658 deletions
@@ -0,0 +1,320 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace CryptoExchange.Net.Objects;
/// <summary>
/// WebSocket call result
/// </summary>
public record WebSocketResult : IWebSocketResult
{
/// <summary>
/// ctor
/// </summary>
public WebSocketResult(string exchange, Error? error)
{
Exchange = exchange;
Error = error;
}
/// <summary>
/// Create a new success WebSocket result
/// </summary>
public static WebSocketResult<T> Ok<T>(IWebSocketResult result, T data) =>
new WebSocketResult<T>(result.Exchange, data, null)
{
ConnectionId = result.ConnectionId,
Url = result.Url,
RequestId = result.RequestId,
ResponseTime = result.ResponseTime,
Error = result.Error,
Data = data
};
/// <summary>
/// Create a new success WebSocket result
/// </summary>
public static WebSocketResult<T> Ok<T>(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? url,
T data) =>
new WebSocketResult<T>(exchange, data, null)
{
ResponseTime = elapsed,
RequestId = requestId,
ConnectionId = connectionId,
Url = url
};
/// <summary>
/// Create a new success WebSocket result
/// </summary>
public static WebSocketResult Ok(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? url) =>
new WebSocketResult(exchange, null)
{
ResponseTime = elapsed,
RequestId = requestId,
ConnectionId = connectionId,
Url = url
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static WebSocketResult Fail(string exchange, Error error) => new WebSocketResult(exchange, error);
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static WebSocketResult Fail(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? url,
Error error) =>
new WebSocketResult(exchange, error)
{
ResponseTime = elapsed,
RequestId = requestId,
ConnectionId = connectionId,
Url = url
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static WebSocketResult<T> Fail<T>(IWebSocketResult result, Error? error = null, T? data = default)
=> new WebSocketResult<T>(result.Exchange, data, error ?? result.Error)
{
ConnectionId = result.ConnectionId,
Url = result.Url,
RequestId = result.RequestId,
ResponseTime = result.ResponseTime,
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static WebSocketResult<T> Fail<T>(string exchange, Error error) => new WebSocketResult<T>(exchange, default, error);
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static WebSocketResult<T> Fail<T>(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? url,
Error error) =>
new WebSocketResult<T>(exchange, default, error)
{
ResponseTime = elapsed,
RequestId = requestId,
ConnectionId = connectionId,
Url = url
};
/// <summary>
/// Exchange name
/// </summary>
public string Exchange { get; init; }
/// <inheritdoc />
public Error? Error { get; init; }
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
public bool Success => Error == null;
/// <summary>
/// The request id
/// </summary>
public int? RequestId { get; init; }
/// <summary>
/// The url which was requested
/// </summary>
public int? ConnectionId { get; init; }
/// <summary>
/// The websocket url
/// </summary>
public string? Url { get; init; }
/// <summary>
/// The time between sending the request and receiving the response
/// </summary>
public TimeSpan? ResponseTime { get; init; }
}
/// <inheritdoc />
public record WebSocketResult<T> : WebSocketResult, IWebSocketResult<T>
{
/// <summary>
/// ctor
/// </summary>
public WebSocketResult(string exchange, T? value, Error? error): base(exchange, error)
{
Data = value;
}
/// <inheritdoc />
public new Error? Error
{
get => base.Error;
init => base.Error = value;
}
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Data))]
public new bool Success => Error == null;
/// <summary>
/// The data returned by the call, only available when Success = true
/// </summary>
public T? Data { get; init; }
}
/// <inheritdoc />
public record QueryResult : WebSocketResult
{
/// <summary>
/// ctor
/// </summary>
public QueryResult(string exchange, Error? error) : base(exchange, error)
{
}
/// <summary>
/// Create a new error Query result
/// </summary>
public new static QueryResult Fail(string exchange, Error error) => new QueryResult(exchange, error);
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static QueryResult Fail(IQueryResult result, Error? error = null)
=> new QueryResult(result.Exchange, error ?? result.Error)
{
ConnectionId = result.ConnectionId,
Url = result.Url,
RequestId = result.RequestId,
ResponseTime = result.ResponseTime,
};
/// <summary>
/// Create a new success query result
/// </summary>
public static QueryResult<T> Ok<T>(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? requestBody,
string? url,
string? originalData,
T data) =>
new QueryResult<T>(exchange, data, null)
{
ResponseTime = elapsed,
RequestId = requestId,
RequestBody = requestBody,
ConnectionId = connectionId,
Url = url,
OriginalData = originalData,
};
/// <summary>
/// Create a new success WebSocket result
/// </summary>
public static QueryResult<T> Ok<T>(IQueryResult result, T data) =>
new QueryResult<T>(result.Exchange, data, null)
{
ConnectionId = result.ConnectionId,
Url = result.Url,
RequestId = result.RequestId,
RequestBody = result.RequestBody,
ResponseTime = result.ResponseTime,
Error = result.Error,
OriginalData = result.OriginalData,
Data = data
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static QueryResult<T> Fail<T>(
string exchange,
int connectionId,
TimeSpan elapsed,
int requestId,
string? requestBody,
string? url,
string? originalData,
Error error) =>
new QueryResult<T>(exchange, default, error)
{
ResponseTime = elapsed,
RequestId = requestId,
RequestBody = requestBody,
ConnectionId = connectionId,
OriginalData = originalData,
Url = url
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public static QueryResult<T> Fail<T>(IQueryResult result, Error? error = null, T? data = default)
=> new QueryResult<T>(result.Exchange, data, error ?? result.Error)
{
ConnectionId = result.ConnectionId,
Url = result.Url,
RequestId = result.RequestId,
RequestBody = result.RequestBody,
OriginalData = result.OriginalData,
ResponseTime = result.ResponseTime,
};
/// <summary>
/// Create a new error WebSocket result
/// </summary>
public new static QueryResult<T> Fail<T>(string exchange, Error error) => new QueryResult<T>(exchange, default, error);
/// <inheritdoc />
public string? RequestBody { get; init; }
}
/// <inheritdoc />
public record QueryResult<T> : QueryResult, IQueryResult<T>
{
/// <summary>
/// ctor
/// </summary>
public QueryResult(string exchange, T? value, Error? error) : base(exchange, error)
{
Data = value;
}
/// <inheritdoc />
public new Error? Error
{
get => base.Error;
init => base.Error = value;
}
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Data))]
public new bool Success => Error == null;
/// <inheritdoc />
public T? Data { get; set; }
/// <inheritdoc />
public string? OriginalData { get; init; }
}