Esempio n. 1
0
        public IEnumerable <IEndpointRateLimit> GetTokenRateLimitsFromMethod(Expression <Action> expression, ICredentialsRateLimits rateLimits)
        {
            if (expression == null)
            {
                return(null);
            }

            var body = expression.Body;

            var methodCallExpression = body as MethodCallExpression;

            if (methodCallExpression != null)
            {
                var method          = methodCallExpression.Method;
                var attributes      = _attributeHelper.GetAttributes <CustomTwitterEndpointAttribute>(method);
                var tokenAttributes = _attributeHelper.GetAllPropertiesAttributes <ICredentialsRateLimits, TwitterEndpointAttribute>();
                var validKeys       = tokenAttributes.Keys.Where(x => attributes.Any(a => a.EndpointURL == x.EndpointURL));
                return(validKeys.Select(key => GetRateLimitFromProperty(tokenAttributes[key], rateLimits)));
            }

            return(null);
        }
Esempio n. 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);
        }