1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-10-20 01:50:54 +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,117 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace CryptoExchange.Net.Objects;
/// <summary>
/// Call result
/// </summary>
public record CallResult : ICallResult
{
private static CallResult _successResult = new CallResult();
/// <inheritdoc />
public Error? Error { get; init; }
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
public bool Success => Error == null;
/// <summary>
/// Create an error response
/// </summary>
/// <param name="error">The error</param>
public static CallResult Fail(Error error) => new CallResult { Error = error };
/// <summary>
/// Create a success result
/// </summary>
public static CallResult Ok() => _successResult;
/// <summary>
/// Create a success result
/// </summary>
/// <typeparam name="T">Result type</typeparam>
/// <param name="originalData">The original string data</param>
/// <param name="data">Data type</param>
public static CallResult<T> Ok<T>(T data, string? originalData = null) => new CallResult<T> { Data = data, OriginalData = originalData };
/// <summary>
/// Create an error response
/// </summary>
/// <typeparam name="T">Result type</typeparam>
/// <param name="originalData">The original string data</param>
/// <param name="error">The error</param>
public static CallResult<T> Fail<T>(Error error, string? originalData = null) => new CallResult<T> { Error = error, OriginalData = originalData };
/// <inheritdoc />
public override string ToString()
{
return Success ? $"Success" : $"Error: {Error}";
}
}
/// <inheritdoc />
public record CallResult<T> : CallResult, ICallResult<T>
{
/// <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; }
/// <summary>
/// The original data returned by the call, only available when `OutputOriginalData` is set to `true` in the client options
/// </summary>
public string? OriginalData { get; init; }
/// <summary>
/// Create an error response
/// </summary>
/// <param name="error">The error</param>
/// <param name="originalData">The original string data</param>
public static CallResult<T> Fail(Error error, string? originalData = null) => new CallResult<T> { Error = error, OriginalData = originalData };
/// <summary>
/// Create a success result
/// </summary>
/// <param name="data">The data</param>
/// <param name="originalData">The original string data</param>
/// <returns></returns>
public static CallResult<T> Ok(T data, string? originalData = null) => new CallResult<T> { Data = data, OriginalData = originalData };
}
/// <summary>
/// Call result for an exchange
/// </summary>
/// <typeparam name="T">Data type</typeparam>
public record ExchangeCallResult<T> : CallResult<T>
{
/// <summary>
/// Exchange name
/// </summary>
public string Exchange { get; set; } = string.Empty;
/// <summary>
/// Create an error response
/// </summary>
/// <param name="exchange">The exchange name</param>
/// <param name="error">The error</param>
/// <param name="originalData">The original string data</param>
public static ExchangeCallResult<T> Fail(string exchange, Error error, string? originalData = null) => new ExchangeCallResult<T> { Exchange = exchange, Error = error };
/// <summary>
/// Create a success result
/// </summary>
/// <param name="exchange">The exchange name</param>
/// <param name="data">The data</param>
/// <param name="originalData">The original string data</param>
/// <returns></returns>
public static ExchangeCallResult<T> Ok(string exchange, T data, string? originalData = null) => new ExchangeCallResult<T> { Exchange = exchange, Data = data };
}
@@ -0,0 +1,286 @@
using CryptoExchange.Net.SharedApis;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace CryptoExchange.Net.Objects;
/// <summary>
/// HTTP call result
/// </summary>
public record HttpResult : IHttpResult
{
/// <summary>
/// Create a new success HTTP result
/// </summary>
public static HttpResult<T> Ok<T>(
string exchange,
HttpStatusCode code,
Version version,
HttpResponseHeaders responseHeaders,
TimeSpan elapsed,
long? contentLength,
string? originalData,
int requestId,
string uri,
string? content,
HttpMethod method,
HttpRequestHeaders requestHeaders,
ResultDataSource source,
T data) =>
new HttpResult<T>(exchange, data, null)
{
ResponseStatusCode = code,
HttpVersion = version,
ResponseHeaders = responseHeaders,
ResponseTime = elapsed,
ResponseLength = contentLength,
OriginalData = originalData,
RequestId = requestId,
RequestUrl = uri,
RequestBody = content,
RequestMethod = method,
RequestHeaders = requestHeaders,
DataSource = source,
};
/// <summary>
/// Create a new success HTTP result
/// </summary>
public static HttpResult<T> Ok<T>(IHttpResult result, T data, PageRequest? pageRequest = null) =>
new HttpResult<T>(result.Exchange, data, null)
{
ResponseStatusCode = result.ResponseStatusCode,
HttpVersion = result.HttpVersion,
ResponseHeaders = result.ResponseHeaders,
ResponseTime = result.ResponseTime,
ResponseLength = result.ResponseLength,
OriginalData = result.OriginalData,
RequestId = result.RequestId,
RequestUrl = result.RequestUrl,
RequestBody = result.RequestBody,
RequestMethod = result.RequestMethod,
RequestHeaders = result.RequestHeaders,
DataSource = result.DataSource,
Error = result.Error,
Data = data,
NextPageRequest = pageRequest
};
/// <summary>
/// Create a new error HTTP result
/// </summary>
public static HttpResult<T> Fail<T>(string exchange, Error error) => new HttpResult<T>(exchange, default, error);
/// <summary>
/// Create a new error HTTP result
/// </summary>
public static HttpResult<T> Fail<T>(IHttpResult result, Error? error = null, T? data = default)
=> new HttpResult<T>(result.Exchange, data, error ?? result.Error)
{
ResponseStatusCode = result.ResponseStatusCode,
HttpVersion = result.HttpVersion,
ResponseHeaders = result.ResponseHeaders,
ResponseTime = result.ResponseTime,
ResponseLength = result.ResponseLength,
OriginalData = result.OriginalData,
RequestId = result.RequestId,
RequestUrl = result.RequestUrl,
RequestBody = result.RequestBody,
RequestMethod = result.RequestMethod,
RequestHeaders = result.RequestHeaders,
DataSource = result.DataSource,
};
/// <summary>
/// Create a new error HTTP result
/// </summary>
public static HttpResult<T> Fail<T>(
string exchange,
HttpStatusCode? code,
Version? version,
HttpResponseHeaders? responseHeaders,
TimeSpan elapsed,
long? contentLength,
string? originalData,
int requestId,
string uri,
string? content,
HttpMethod method,
HttpRequestHeaders requestHeaders,
ResultDataSource source,
Error error,
T? result = default) =>
new HttpResult<T>(exchange, result, error)
{
ResponseStatusCode = code,
HttpVersion = version,
ResponseHeaders = responseHeaders,
ResponseTime = elapsed,
ResponseLength = contentLength,
OriginalData = originalData,
RequestId = requestId,
RequestUrl = uri,
RequestBody = content,
RequestMethod = method,
RequestHeaders = requestHeaders,
DataSource = source,
};
/// <summary>
/// Create a new error HTTP result
/// </summary>
public static HttpResult Fail(string exchange, Error error) => new HttpResult() { Exchange = exchange, Error = error };
/// <summary>
/// Create a new error HTTP result
/// </summary>
public static HttpResult Fail(IHttpResult result, Error? error = null)
=> new HttpResult()
{
ResponseStatusCode = result.ResponseStatusCode,
HttpVersion = result.HttpVersion,
ResponseHeaders = result.ResponseHeaders,
ResponseTime = result.ResponseTime,
ResponseLength = result.ResponseLength,
OriginalData = result.OriginalData,
RequestId = result.RequestId,
RequestUrl = result.RequestUrl,
RequestBody = result.RequestBody,
RequestMethod = result.RequestMethod,
RequestHeaders = result.RequestHeaders,
DataSource = result.DataSource,
Exchange = result.Exchange,
Error = error ?? result.Error
};
/// <summary>
/// Create a new success HTTP result
/// </summary>
public static HttpResult Ok(IHttpResult result)
=> new HttpResult()
{
ResponseStatusCode = result.ResponseStatusCode,
HttpVersion = result.HttpVersion,
ResponseHeaders = result.ResponseHeaders,
ResponseTime = result.ResponseTime,
ResponseLength = result.ResponseLength,
OriginalData = result.OriginalData,
RequestId = result.RequestId,
RequestUrl = result.RequestUrl,
RequestBody = result.RequestBody,
RequestMethod = result.RequestMethod,
RequestHeaders = result.RequestHeaders,
DataSource = result.DataSource,
Exchange = result.Exchange,
};
/// <summary>
/// Exchange name
/// </summary>
public string Exchange { get; init; } = string.Empty;
/// <inheritdoc />
public Error? Error { get; internal set; }
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
public bool Success => Error == null;
/// <summary>
/// The original data returned by the call, only available when `OutputOriginalData` is set to `true` in the client options
/// </summary>
public string? OriginalData { get; init; }
/// <summary>
/// The request http method
/// </summary>
public HttpMethod? RequestMethod { get; init; }
/// <summary>
/// HTTP protocol version
/// </summary>
public Version? HttpVersion { get; init; }
/// <summary>
/// The headers sent with the request
/// </summary>
public HttpRequestHeaders? RequestHeaders { get; init; }
/// <summary>
/// The request id
/// </summary>
public int? RequestId { get; init; }
/// <summary>
/// The url which was requested
/// </summary>
public string? RequestUrl { get; init; }
/// <summary>
/// The body of the request
/// </summary>
public string? RequestBody { get; init; }
/// <summary>
/// Length in bytes of the response
/// </summary>
public long? ResponseLength { get; init; }
/// <summary>
/// The status code of the response. Note that a OK status does not always indicate success, check the Success parameter for this.
/// </summary>
public HttpStatusCode? ResponseStatusCode { get; init; }
/// <summary>
/// The response headers
/// </summary>
public HttpResponseHeaders? ResponseHeaders { get; init; }
/// <summary>
/// The time between sending the request and receiving the response
/// </summary>
public TimeSpan? ResponseTime { get; init; }
/// <summary>
/// The data source of this result
/// </summary>
public ResultDataSource DataSource { get; init; } = ResultDataSource.Server;
}
/// <inheritdoc />
public record HttpResult<T> : HttpResult, IHttpResult<T>
{
/// <summary>
/// ctor
/// </summary>
public HttpResult(string exchange, T? value, Error? error)
{
Exchange = exchange;
Data = value;
Error = error;
}
/// <inheritdoc />
public new Error? Error
{
get => base.Error;
internal set => 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; }
/// <summary>
/// Next page request, only potentially available when using Shared API's
/// </summary>
public PageRequest? NextPageRequest { get; init; }
}
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace CryptoExchange.Net.Objects;
/// <summary>
/// Call result
/// </summary>
public interface ICallResult
{
/// <summary>
/// An error if the call didn't succeed, will always be filled if Success = false
/// </summary>
Error? Error { get; }
/// <summary>
/// Whether the call was successful
/// </summary>
[MemberNotNullWhen(false, nameof(Error))]
bool Success { get; }
}
/// <summary>
/// Call result
/// </summary>
/// <typeparam name="T">Result data type</typeparam>
public interface ICallResult<T> : ICallResult
{
/// <inheritdoc />
new Error? Error { get; }
/// <inheritdoc />
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Data))]
new bool Success { get; }
/// <summary>
/// The result data, only available when Success = true
/// </summary>
T? Data { get; }
}
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace CryptoExchange.Net.Objects
{
/// <summary>
/// HTTP call result
/// </summary>
public interface IHttpResult : ICallResult
{
/// <summary>
/// Exchange name
/// </summary>
string Exchange { get; init; }
/// <summary>
/// The original data returned by the call, only available when `OutputOriginalData` is set to `true` in the client options
/// </summary>
string? OriginalData { get; init; }
/// <summary>
/// The request http method
/// </summary>
HttpMethod? RequestMethod { get; init; }
/// <summary>
/// HTTP protocol version
/// </summary>
Version? HttpVersion { get; init; }
/// <summary>
/// The headers sent with the request
/// </summary>
HttpRequestHeaders? RequestHeaders { get; init; }
/// <summary>
/// The request id
/// </summary>
int? RequestId { get; init; }
/// <summary>
/// The url which was requested
/// </summary>
string? RequestUrl { get; init; }
/// <summary>
/// The body of the request
/// </summary>
string? RequestBody { get; init; }
/// <summary>
/// Length in bytes of the response
/// </summary>
long? ResponseLength { get; init; }
/// <summary>
/// The status code of the response. Note that a OK status does not always indicate success, check the Success parameter for this.
/// </summary>
HttpStatusCode? ResponseStatusCode { get; init; }
/// <summary>
/// The response headers
/// </summary>
HttpResponseHeaders? ResponseHeaders { get; init; }
/// <summary>
/// The time between sending the request and receiving the response
/// </summary>
TimeSpan? ResponseTime { get; init; }
/// <summary>
/// The data source of this result
/// </summary>
ResultDataSource DataSource { get; init; }
}
/// <summary>
/// HTTP call result
/// </summary>
/// <typeparam name="T">Result data type</typeparam>
public interface IHttpResult<T> : IHttpResult, ICallResult<T>
{
}
}
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace CryptoExchange.Net.Objects
{
/// <summary>
/// WebSocket call result
/// </summary>
public interface IWebSocketResult : ICallResult
{
/// <summary>
/// Exchange name
/// </summary>
string Exchange { get; init; }
/// <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; }
}
/// <summary>
/// WebSocket call result
/// </summary>
/// <typeparam name="T">Data result type</typeparam>
public interface IWebSocketResult<T> : IWebSocketResult, ICallResult<T>
{
}
/// <summary>
/// Query result
/// </summary>
public interface IQueryResult : IWebSocketResult
{
/// <summary>
/// The original returned data, only available when OutputOriginalData is set to true in the client options
/// </summary>
public string? OriginalData { get; init; }
/// <summary>
/// The query request body
/// </summary>
public string? RequestBody { get; init; }
}
/// <summary>
/// Query result
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IQueryResult<T> : IQueryResult, IWebSocketResult<T>
{
}
}
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoExchange.Net.Objects;
/// <summary>
/// Void result
/// </summary>
public readonly struct Unit
{
/// <summary>
/// Void value
/// </summary>
public static readonly Unit Value = default;
/// <summary>
/// Type
/// </summary>
public static Type Type { get; } = typeof(Unit);
}
@@ -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; }
}