Example #1
0
        public async Task QueryExecutedAsync(string query, ITwitterCredentials credentials, Dictionary <string, IEnumerable <string> > rateLimitHeaders)
        {
            if (rateLimitHeaders != null && rateLimitHeaders.Count > 0)
            {
                var rateLimit = await _rateLimitCacheManager.GetQueryRateLimitAsync(new GetEndpointRateLimitsParameters(query), credentials).ConfigureAwait(false);


                if (rateLimit == null)
                {
                    rateLimit = new EndpointRateLimit();
                    var rateLimits = await _rateLimitCacheManager.GetCredentialsRateLimitsAsync(credentials).ConfigureAwait(false);

                    if (rateLimits == null)
                    {
                        // If the user runs out of RateLimit requests
                        return;
                    }

                    var endpointUrl = new Uri(query).GetEndpointURL();
                    rateLimits.OtherEndpointRateLimits[new TwitterEndpointAttribute(endpointUrl)] = rateLimit;
                }

                IEnumerable <string> limitHeaders;
                if (rateLimitHeaders.TryGetValue("x-rate-limit-limit", out limitHeaders))
                {
                    var limit = limitHeaders.FirstOrDefault();
                    if (limit != null)
                    {
                        rateLimit.Limit = int.Parse(limit);
                    }
                }

                IEnumerable <string> remainingHeaders;
                if (rateLimitHeaders.TryGetValue("x-rate-limit-remaining", out remainingHeaders))
                {
                    var remaining = remainingHeaders.FirstOrDefault();
                    if (remaining != null)
                    {
                        rateLimit.Remaining = int.Parse(remaining);
                    }
                }

                IEnumerable <string> resetHeaders;
                if (rateLimitHeaders.TryGetValue("x-rate-limit-reset", out resetHeaders))
                {
                    var reset = resetHeaders.FirstOrDefault();
                    if (reset != null)
                    {
                        rateLimit.Reset = int.Parse(reset);
                    }
                }
            }
            else
            {
                await QueryExecutedAsync(query, credentials).ConfigureAwait(false);
            }
        }
Example #2
0
        public IEndpointRateLimit GetEndpointRateLimitFromQuery(string query, ICredentialsRateLimits rateLimits, bool createIfNotExist)
        {
            var queryBaseURL = _webHelper.GetBaseURL(query);

            if (rateLimits == null || queryBaseURL == null)
            {
                return(null);
            }

            var tokenAttributes   = _attributeHelper.GetAllPropertiesAttributes <ICredentialsRateLimits, TwitterEndpointAttribute>();
            var matchingAttribute = tokenAttributes.Keys.JustOneOrDefault(x => IsEndpointURLMatchingQueryURL(queryBaseURL, x));

            // In the default list of rate limits
            if (matchingAttribute != null)
            {
                var matchingProperty = tokenAttributes[matchingAttribute];
                return(GetRateLimitFromProperty(matchingProperty, rateLimits));
            }

            // In the other endpoint rate limits
            var matchingKeyPair = rateLimits.OtherEndpointRateLimits.FirstOrDefault(x => IsEndpointURLMatchingQueryURL(queryBaseURL, x.Key));

            if (!matchingKeyPair.Equals(default(KeyValuePair <TwitterEndpointAttribute, IEndpointRateLimit>)))
            {
                return(matchingKeyPair.Value);
            }

            if (!createIfNotExist)
            {
                return(null);
            }

            // Other endpoint rate limits do not yet exist.
            // Therefore we create a new one and return it.
            var attribute         = new TwitterEndpointAttribute(queryBaseURL);
            var endpointRateLimit = new EndpointRateLimit
            {
                IsCustomHeaderRateLimit = true
            };

            rateLimits.OtherEndpointRateLimits.Add(attribute, endpointRateLimit);

            return(endpointRateLimit);
        }