/// <summary> /// Processes the HTTP headers. /// Override because the rate limits are processed as well. /// </summary> /// <param name="httpResponseMessage">The HTTP response message.</param> protected override void ProcessHttpHeaders(HttpResponseMessage httpResponseMessage) { base.ProcessHttpHeaders(httpResponseMessage); if (httpResponseMessage.Headers.Contains("x-ratelimit-remaining")) { RateLimits = new RateLimits() { Remaining = int.Parse(httpResponseMessage.Headers.GetValues("x-ratelimit-remaining").First()), Limit = int.Parse(httpResponseMessage.Headers.GetValues("x-ratelimit-limit").First()), ResetsAt = DateTime.Now.AddSeconds(int.Parse(httpResponseMessage.Headers.GetValues("x-ratelimit-reset").First())), RetryAfter = httpResponseMessage.Headers.RetryAfter?.Delta?.Seconds }; } else if (httpResponseMessage.Headers.RetryAfter != null) { var retryAfter = httpResponseMessage.Headers.RetryAfter?.Delta?.Seconds ?? 10; RateLimits = new RateLimits() { Remaining = 0, Limit = 0, ResetsAt = DateTime.Now.AddSeconds(retryAfter), RetryAfter = retryAfter }; } }
public void ProcessResults_Handles_RateLimits_Results() { var helpReqProc = new HelpRequestProcessor <Help> { Type = HelpType.RateLimits, BaseUrl = "https://api.twitter.com/1.1/" }; List <Help> helpList = helpReqProc.ProcessResults(RateLimitsResponse); Assert.IsNotNull(helpList); Assert.IsNotNull(helpList.SingleOrDefault()); Help help = helpList.Single(); Assert.AreEqual(HelpType.RateLimits, help.Type); Assert.AreEqual("15411837-3wGGrD7CY0Hb0tguLA3pSH7EMwSWWcnuD3DEQ1E27", help.RateLimitAccountContext); Assert.IsNotNull(help.RateLimits); Assert.IsTrue(help.RateLimits.Any()); Dictionary <string, List <RateLimits> > rateLimits = help.RateLimits; Assert.IsTrue(rateLimits.ContainsKey("lists")); List <RateLimits> limitsList = rateLimits["lists"]; Assert.IsNotNull(limitsList); Assert.IsTrue(limitsList.Any()); RateLimits limits = limitsList.First(); Assert.IsNotNull(limits); Assert.AreEqual("/lists/subscriptions", limits.Resource); Assert.AreEqual(15, limits.Limit); Assert.AreEqual(15, limits.Remaining); Assert.AreEqual(1348087186ul, limits.Reset); }
/// <summary> /// Initializes a new instance of the <see cref="BolRetailerApi"/> class. /// Setup the necessary objects. This object can be used to inject via DI. /// </summary> /// <param name="clientId">The client identifier.</param> /// <param name="clientSecret">The client secret.</param> /// <param name="httpClient">The HTTP client.</param> /// <param name="testMode">if set to <c>true</c> [test mode].</param> public BolRetailerApi(string clientId, string clientSecret, HttpClient httpClient, bool testMode = false) { RateLimits = new RateLimits(); EndPoints = testMode ? new TestEndPoints() : new EndPoints(); _httpClient = httpClient; _authorizationToken = new AuthorizationToken(clientId, clientSecret, TokenService); }
/// <summary> /// Initializes a new instance of the <see cref="OrdersService" /> class. /// </summary> /// <param name="httpClient">The HTTP client.</param> /// <param name="endPoints">The end points.</param> /// <param name="authorizationToken">The authorization token.</param> /// <param name="rateLimits">The rate limits.</param> public OrdersService( HttpClient httpClient, IEndPoints endPoints, IAuthorizationToken authorizationToken, RateLimits rateLimits = null) : base(httpClient, endPoints, authorizationToken, rateLimits) { }
public void Is_Invalid() { var rateLimits = new RateLimits(); rateLimits.ResetsAt = DateTime.Now; var isValid = rateLimits.IsStillValid(); Assert.IsFalse(isValid); }
public void Is_Valid() { var rateLimits = new RateLimits(); rateLimits.ResetsAt = DateTime.Now.AddSeconds(10); var isValid = rateLimits.IsStillValid(); Assert.IsTrue(isValid); }
public Response() { Success = true; Status = ""; Error = ""; Content = ""; StatusCode = HttpStatusCode.OK; FullHeaders = new WebHeaderCollection(); rateLimits = new RateLimits(); }
/// <summary> /// Initializes a new instance of the <see cref="AuthenticatedClientBase"/> class. /// </summary> /// <param name="httpClient">The HTTP client.</param> /// <param name="endPoints">The end points.</param> /// <param name="authorizationToken">The authorization token.</param> /// <param name="rateLimits">The rate limits.</param> protected AuthenticatedClientBase( HttpClient httpClient, IEndPoints endPoints, IAuthorizationToken authorizationToken, RateLimits rateLimits = null) : base(httpClient, endPoints) { _authorizationToken = authorizationToken; RateLimits = rateLimits ?? new RateLimits(); }
public void RemainingTestConversion() { var timeRemaining1SecAgo = ((DateTimeOffset)DateTime.Now.AddSeconds(-1)).ToUnixTimeSeconds(); var rateLimits = new RateLimits { Reset = timeRemaining1SecAgo }; Assert.AreEqual(1, rateLimits.ResetSeconds); }
private RateLimitViewModel makeRateVm(RateLimits rl) { RateLimitViewModel rlvm = new RateLimitViewModel() { CallsPerWindow = rl.CallsPerWindow, RateLimitId = rl.RateLimitsId, RateWindowInMinutes = (int)Math.Ceiling(rl.RateWindow.TotalMinutes) }; return(rlvm); }
private void SaveRateLimit(string xApiKey, HttpResponseMessage response) { if (response.Headers.Contains("X-RateLimit-Limit")) { var rateLimit = int.Parse(response.Headers.GetValues("X-RateLimit-Limit").FirstOrDefault() ?? "60"); var rateLimitRemaining = int.Parse(response.Headers.GetValues("X-RateLimit-Remaining").FirstOrDefault() ?? "60"); var rateLimitResetTime = int.Parse(response.Headers.GetValues("X-RateLimit-Reset").FirstOrDefault() ?? "3600"); var rateLimitObj = new RateLimits { HourlyLimit = rateLimit, Remaining = rateLimitRemaining, ResetTime = DateTimeOffset.UtcNow.AddSeconds(rateLimitResetTime), }; _cache.Set($"ratelimit:{xApiKey}", rateLimitObj, rateLimitObj.ResetTime); } }
private int GetNextCallTime() { var nextCallTime = 0; if (RateLimits.Any()) { lock (_lock) { nextCallTime = RateLimits .Max(l => l.GetNextAllowedCallTime(CallTracker)); CallTracker.CallWillHappenIn(nextCallTime); CallTracker.TrimCallsForRateLimits(RateLimits); } } return(nextCallTime); }
public override void DeserializeJson(JObject json) { var limitsArrayToken = json["Limits"]; if (limitsArrayToken != null && limitsArrayToken is JArray) { foreach (var limitsToken in limitsArrayToken as JArray) { var limits = new RateLimits { m_description = Endpoint }; var limitsJson = limitsToken as Newtonsoft.Json.Linq.JObject; // Check which subpaths these rate limits apply to var subpaths = limitsJson["Subpaths"] as JArray; if (subpaths != null) { foreach (var subpathToken in subpaths) { limits.m_applicableSubpaths.Add(subpathToken.ToString()); } } else { // If subpaths field isn't specified, apply the limits to all calls to the endpoint limits.m_applicableSubpaths.Add("*"); } Utils.SafeAssign(ref limits.m_description, limitsJson["Description"]); Utils.SafeAssign(ref limits.m_sustainedTimePeriodSeconds, limitsJson["SustainedCallPeriod"]); Utils.SafeAssign(ref limits.m_sustainedCallLimit, limitsJson["SustainedCallLimit"]); Utils.SafeAssign(ref limits.m_burstTimePeriodSeconds, limitsJson["BurstCallPeriod"]); Utils.SafeAssign(ref limits.m_burstCallLimit, limitsJson["BurstCallLimit"]); Utils.SafeAssign(ref limits.m_avgTimeBetweenReqsMs, limitsJson["AvgTimeBetweenReqsMs"]); Utils.SafeAssign(ref limits.m_avgElapsedCallTimeMs, limitsJson["AvgElapsedCallTimeMs"]); Utils.SafeAssign(ref limits.m_maxElapsedCallTimeMs, limitsJson["MaxElapsedCallTimeMs"]); m_rateLimits.Add(limits); } } }
public IActionResult SendRateLimit(RateLimits rl) { Response.Headers.Add("Retry-After", rl.RetryAfter.ToString()); return(SendError(429, "API rate limit exceeded")); }
public RazorRateLimit(RateLimits rateLimits, IMemoryCache memoryCache, ILogger <RazorRateLimit> logger) { this.memoryCache = memoryCache; this.logger = logger; this.rateLimits = rateLimits; }