using CryptoExchange.Net.RateLimiting.Interfaces; using System.Collections.Concurrent; using System.IO; using System.Net.Http; namespace CryptoExchange.Net.Objects { /// /// Request definitions cache /// public class RequestDefinitionCache { private readonly ConcurrentDictionary _definitions = new(); /// /// Get a definition if it is already in the cache or create a new definition and add it to the cache /// /// The HttpMethod /// The base address/host /// Endpoint path /// Endpoint is authenticated /// public RequestDefinition GetOrCreate(HttpMethod method, string baseAddress, string path, bool authenticated = false) => GetOrCreate(method, baseAddress, path, null, 0, authenticated, null, null, null, null, null, null, null); /// /// Get a definition if it is already in the cache or create a new definition and add it to the cache /// /// The HttpMethod /// The base address/host /// Endpoint path /// The rate limit gate /// Request weight /// Endpoint is authenticated /// public RequestDefinition GetOrCreate(HttpMethod method, string baseAddress, string path, IRateLimitGate rateLimitGate, int weight = 1, bool authenticated = false) => GetOrCreate(method, baseAddress, path, rateLimitGate, weight, authenticated, null, null, null, null, null, null, null); /// /// Get a definition if it is already in the cache or create a new definition and add it to the cache /// /// The base address/host /// The HttpMethod /// Endpoint path /// The rate limit gate /// The rate limit guard for this specific endpoint /// Request weight /// Endpoint is authenticated /// Request body format /// Parameter position /// Array serialization type /// Prevent request caching /// Try parse the response even when status is not success /// Force trailing `/` /// Optional request identifier override /// Whether concurrent identical requests should be prevented from sharing the same request/response /// public RequestDefinition GetOrCreate( HttpMethod method, string baseAddress, string path, IRateLimitGate? rateLimitGate, int weight, bool authenticated, IRateLimitGuard? limitGuard = null, RequestBodyFormat? requestBodyFormat = null, HttpMethodParameterPosition? parameterPosition = null, ArrayParametersSerialization? arraySerialization = null, bool? preventCaching = null, bool? tryParseOnNonSuccess = null, bool? forcePathEndWithSlash = null, string? identifier = null, bool? preventRequestCoalescing = null) { var identifierToUse = identifier ?? $"{path}{method.Method}{baseAddress}"; if (!_definitions.TryGetValue(identifierToUse, out var def)) { def = new RequestDefinition(baseAddress, path, method) { Authenticated = authenticated, LimitGuard = limitGuard, RateLimitGate = rateLimitGate, Weight = weight, ArraySerialization = arraySerialization, RequestBodyFormat = requestBodyFormat, ParameterPosition = parameterPosition, PreventCaching = preventCaching ?? false, TryParseOnNonSuccess = tryParseOnNonSuccess ?? false, ForcePathEndWithSlash = forcePathEndWithSlash ?? false, PreventRequestCoalescing = preventRequestCoalescing ?? false, }; _definitions.TryAdd(identifierToUse, def); } return def; } } }