1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-10-21 18:40:57 +00:00

Shared API's V2 (#289)

Added V2 Shared API's infrastructure
Added request coalescing
Added rate limiting admission logic
Fixed some rate limiting issues
This commit is contained in:
Jan Korf
2026-09-23 09:15:39 +02:00
committed by GitHub
parent cecfbcba48
commit ae7504c26c
482 changed files with 12870 additions and 2150 deletions
@@ -0,0 +1,28 @@
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Sockets;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CryptoExchange.Net.SharedApis
{
/// <summary>
/// Operation for subscribing to incremental order book updates for a symbol
/// </summary>
public interface ISubscribeIncrementalOrderBookSocket : ISharedSubscription
{
/// <summary>
/// Order book subscription options
/// </summary>
SubscribeOrderBookOptions SubscribeOrderBookOptions { get; }
/// <summary>
/// Subscribe to incremental order book updates for a symbol
/// </summary>
/// <param name="request">Request info</param>
/// <param name="handler">Update handler</param>
/// <param name="ct">Cancellation token, can be used to stop the updates</param>
/// <returns></returns>
Task<WebSocketResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(SubscribeOrderBookRequest request, Action<DataEvent<SharedOrderBook>> handler, CancellationToken ct = default);
}
}
@@ -0,0 +1,57 @@
using CryptoExchange.Net.Objects;
using System;
using System.Linq;
namespace CryptoExchange.Net.SharedApis
{
/// <summary>
/// Options for subscribing to order book snapshot updates
/// </summary>
public class SubscribeIncrementalOrderBookOptions : CapabilityOptions<SubscribeOrderBookRequest, ISubscribeIncrementalOrderBookSocket>
{
/// <inheritdoc />
public override string Description => "Subscribe to incremental order book updates for a symbol";
private static readonly RequestParameterDescription[] _defaultParameterRules = new[]
{
RequestParameterRule<SubscribeOrderBookRequest>.Optional(x => x.Symbol, "The symbol to subscribe to", new SharedSymbol(TradingMode.Spot, "ETH", "USDT")),
RequestParameterRule<SubscribeOrderBookRequest>.Optional(x => x.Symbols, "The symbols to subscribe to", new[] { new SharedSymbol(TradingMode.Spot, "ETH", "USDT") }),
RequestParameterRule<SubscribeOrderBookRequest>.Optional(x => x.Limit, "The order book depth", 100),
};
/// <summary>
/// Order book depths supported for updates
/// </summary>
public int[] SupportedLimits { get; }
/// <summary>
/// The type of updates the subscription produces
/// </summary>
public SharedOrderBookSubscriptionType UpdateType { get; }
/// <summary>
/// ctor
/// </summary>
public SubscribeIncrementalOrderBookOptions(string exchange, bool needsAuthentication, int[] limits, SharedOrderBookSubscriptionType updateType)
: base(exchange, needsAuthentication, nameof(ISubscribeIncrementalOrderBookSocket.SubscribeToOrderBookUpdatesAsync), _defaultParameterRules)
{
SupportedLimits = limits;
UpdateType = updateType;
}
/// <summary>
/// Validate a request
/// </summary>
public override Error? ValidateRequest(SubscribeOrderBookRequest request, ISubscribeIncrementalOrderBookSocket client)
{
var error = base.ValidateRequest(request, client);
if (error != null)
return error;
if (request.Limit != null && !SupportedLimits.Contains(request.Limit.Value))
return ArgumentError.Invalid(nameof(SubscribeOrderBookRequest.Limit), "Limit not supported");
return null;
}
}
}