public void Initialize()
        {
            _fakeBuilder           = new FakeClassBuilder <RateLimitHelper>();
            _credentialsRateLimits = A.Fake <ICredentialsRateLimits>();

            _webHelper       = new WebHelper(A.Fake <ITweetinviSettingsAccessor>());
            _attributeHelper = new AttributeHelper();
        }
        public void Initialize()
        {
            _fakeBuilder = new FakeClassBuilder<RateLimitHelper>();
            _credentialsRateLimits = A.Fake<ICredentialsRateLimits>();

            _webHelper = new WebHelper(A.Fake<ITweetinviSettingsAccessor>());
            _attributeHelper = new AttributeHelper();
        }
        public void Itinialize()
        {
            _fakeBuilder = new FakeClassBuilder<RateLimitCache>();

            _credentials = A.Fake<ITwitterCredentials>();
            _credentials2 = A.Fake<ITwitterCredentials>();
            _credentialsRateLimits = A.Fake<ICredentialsRateLimits>();
            _credentialsRateLimits2 = A.Fake<ICredentialsRateLimits>();
        }
        public void Itinialize()
        {
            _fakeBuilder = new FakeClassBuilder <RateLimitCache>();

            _credentials            = A.Fake <ITwitterCredentials>();
            _credentials2           = A.Fake <ITwitterCredentials>();
            _credentialsRateLimits  = A.Fake <ICredentialsRateLimits>();
            _credentialsRateLimits2 = A.Fake <ICredentialsRateLimits>();
        }
Example #5
0
 public void RefreshEntry(ITwitterCredentials credentials, ICredentialsRateLimits credentialsRateLimits)
 {
     lock (_lockCredentialsRateLimitsDictionary)
     {
         if (_credentialsRateLimits.ContainsKey(credentials))
         {
             _credentialsRateLimits[credentials] = credentialsRateLimits;
         }
         else
         {
             _credentialsRateLimits.Add(credentials, credentialsRateLimits);
         }
     }
 }
Example #6
0
 public void RefreshEntry(ITwitterCredentials credentials, ICredentialsRateLimits credentialsRateLimits)
 {
     lock (_lockCredentialsRateLimitsDictionary)
     {
         if (_credentialsRateLimits.ContainsKey(credentials))
         {
             _credentialsRateLimits[credentials] = credentialsRateLimits;
         }
         else
         {
             _credentialsRateLimits.Add(credentials, credentialsRateLimits);
         }
     }
 }
Example #7
0
        /// <summary>
        /// Get all the rate limits of all the Twitter endpoints
        /// </summary>
        public static ICredentialsRateLimits GetCurrentCredentialsRateLimits(bool useRateLimitCache = false)
        {
            ICredentialsRateLimits credentialsRateLimits = null;

            if (!useRateLimitCache)
            {
                credentialsRateLimits = HelpController.GetCurrentCredentialsRateLimits();
                RateLimitCacheManager.UpdateCredentialsRateLimits(Auth.Credentials, credentialsRateLimits);
            }
            else
            {
                credentialsRateLimits = RateLimitCacheManager.GetCredentialsRateLimits(Auth.Credentials);
            }

            return(credentialsRateLimits);
        }
Example #8
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);
        }
Example #9
0
        public void RefreshEntry(ITwitterCredentials credentials, ICredentialsRateLimits newCredentialsRateLimits)
        {
            lock (_lockCredentialsRateLimitsDictionary)
            {
                if (newCredentialsRateLimits == null)
                {
                    return;
                }

                ICredentialsRateLimits currentRateLimits;
                if (_credentialsRateLimits.TryGetValue(credentials, out currentRateLimits))
                {
                    var existingCustomEndpoints = currentRateLimits.OtherEndpointRateLimits;
                    existingCustomEndpoints.ForEach(x => newCredentialsRateLimits.OtherEndpointRateLimits.AddOrUpdate(x));
                }

                _credentialsRateLimits.AddOrUpdate(credentials, newCredentialsRateLimits);
            }
        }
Example #10
0
        public void RefreshEntry(ITwitterCredentials credentials, ICredentialsRateLimits newCredentialsRateLimits)
        {
            lock (_lockCredentialsRateLimitsDictionary)
            {
                if (newCredentialsRateLimits == null)
                {
                    return;
                }

                ICredentialsRateLimits currentRateLimits;
                if (_credentialsRateLimits.TryGetValue(credentials, out currentRateLimits))
                {
                    var existingCustomEndpoints = currentRateLimits.OtherEndpointRateLimits;
                    existingCustomEndpoints.ForEach(x => newCredentialsRateLimits.OtherEndpointRateLimits.AddOrUpdate(x));
                }

                _credentialsRateLimits.AddOrUpdate(credentials, newCredentialsRateLimits);
            }
        }
Example #11
0
        public IEndpointRateLimit GetEndpointRateLimitFromQuery(string query, ICredentialsRateLimits rateLimits)
        {
            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));

            if (matchingAttribute == null)
            {
                return null;
            }

            var matchingProperty = tokenAttributes[matchingAttribute];
            return GetRateLimitFromProperty(matchingProperty, rateLimits);
        }
Example #12
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.
            // Therfore 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;
        }
Example #13
0
        public Task RefreshEntry(IReadOnlyTwitterCredentials credentials, ICredentialsRateLimits newCredentialsRateLimits)
        {
            lock (_lockCredentialsRateLimitsDictionary)
            {
                if (newCredentialsRateLimits == null)
                {
                    return(Task.CompletedTask);
                }

                if (_credentialsRateLimits.TryGetValue(credentials, out var currentRateLimits))
                {
                    var existingCustomEndpoints = currentRateLimits.OtherEndpointRateLimits;
                    existingCustomEndpoints.ForEach(x => newCredentialsRateLimits.OtherEndpointRateLimits.AddOrUpdate(x));
                }

                _credentialsRateLimits.AddOrUpdate(credentials, newCredentialsRateLimits);

                return(Task.CompletedTask);
            }
        }
Example #14
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;
        }
Example #15
0
        private void InitializeData()
        {
            _credentials                   = A.Fake <ITwitterCredentials>();
            _credentials.AccessToken       = TestHelper.GenerateString();
            _credentials.AccessTokenSecret = TestHelper.GenerateString();

            _credentialsRateLimits  = A.Fake <ICredentialsRateLimits>();
            _endpointRateLimit      = A.Fake <IEndpointRateLimit>();
            _credentialsRateLimits2 = A.Fake <ICredentialsRateLimits>();

            _refreshedCredentialsRateLimits = A.Fake <ICredentialsRateLimits>();
            _refreshedEndpointRateLimit     = A.Fake <IEndpointRateLimit>();

            _endpointRateLimit.CallsTo(x => x.Remaining).Returns(0);
            _endpointRateLimit.CallsTo(x => x.ResetDateTime).Returns(DateTime.Now.AddMinutes(1));

            _twitterQuery = A.Fake <ITwitterQuery>();
            _twitterQuery.CallsTo(x => x.QueryURL).Returns(TEST_QUERY);
            _twitterQuery.CallsTo(x => x.HttpMethod).Returns(HttpMethod.GET);
            _twitterQuery.CallsTo(x => x.QueryParameters).Returns(Enumerable.Empty <IOAuthQueryParameter>());
        }
Example #16
0
        public IEndpointRateLimit GetEndpointRateLimitFromQuery(string query, ICredentialsRateLimits rateLimits)
        {
            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));

            if (matchingAttribute == null)
            {
                return(null);
            }

            var matchingProperty = tokenAttributes[matchingAttribute];

            return(GetRateLimitFromProperty(matchingProperty, rateLimits));
        }
Example #17
0
 private IEndpointRateLimit GetRateLimitFromProperty(PropertyInfo propertyInfo, ICredentialsRateLimits rateLimits)
 {
     var rateLimit = propertyInfo.GetValue(rateLimits, null) as IEndpointRateLimit;
     return rateLimit;
 }
Example #18
0
 public bool IsQueryAssociatedWithEndpointRateLimit(string query, ICredentialsRateLimits rateLimits)
 {
     return GetEndpointRateLimitFromQuery(query, rateLimits) != null;
 }
Example #19
0
 private void ArrangeQueryExecutorGetTokenRateLimits(ICredentialsRateLimits result)
 {
     _fakeHelpQueryExecutor
         .CallsTo(x => x.GetCurrentCredentialsRateLimits())
         .Returns(result);
 }
        private void InitializeData()
        {
            _credentials = A.Fake<ITwitterCredentials>();
            _credentials.AccessToken = TestHelper.GenerateString();
            _credentials.AccessTokenSecret = TestHelper.GenerateString();

            _credentialsRateLimits = A.Fake<ICredentialsRateLimits>();
            _endpointRateLimit = A.Fake<IEndpointRateLimit>();
            _credentialsRateLimits2 = A.Fake<ICredentialsRateLimits>();

            _refreshedCredentialsRateLimits = A.Fake<ICredentialsRateLimits>();
            _refreshedEndpointRateLimit = A.Fake<IEndpointRateLimit>();

            _endpointRateLimit.CallsTo(x => x.Remaining).Returns(0);
            _endpointRateLimit.CallsTo(x => x.ResetDateTime).Returns(DateTime.Now.AddMinutes(1));

            _twitterQuery = A.Fake<ITwitterQuery>();
            _twitterQuery.CallsTo(x => x.QueryURL).Returns(TEST_QUERY);
            _twitterQuery.CallsTo(x => x.HttpMethod).Returns(HttpMethod.GET);
            _twitterQuery.CallsTo(x => x.QueryParameters).Returns(Enumerable.Empty<IOAuthQueryParameter>());
        }
 public void UpdateCredentialsRateLimits(ITwitterCredentials credentials, ICredentialsRateLimits credentialsRateLimits)
 {
     _rateLimitCache.RefreshEntry(credentials, credentialsRateLimits);
 }
 public virtual Task UpdateCredentialsRateLimitsAsync(IReadOnlyTwitterCredentials credentials, ICredentialsRateLimits credentialsRateLimits)
 {
     return(_rateLimitCache.RefreshEntryAsync(credentials, credentialsRateLimits));
 }
Example #23
0
        private IEndpointRateLimit GetRateLimitFromProperty(PropertyInfo propertyInfo, ICredentialsRateLimits rateLimits)
        {
            var rateLimit = propertyInfo.GetValue(rateLimits, null) as IEndpointRateLimit;

            return(rateLimit);
        }
Example #24
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);
        }
Example #25
0
 public bool IsQueryAssociatedWithEndpointRateLimit(string query, ICredentialsRateLimits rateLimits)
 {
     return(GetEndpointRateLimitFromQuery(query, rateLimits) != null);
 }
 public void UpdateCredentialsRateLimits(ITwitterCredentials credentials, ICredentialsRateLimits credentialsRateLimits)
 {
     _rateLimitCache.RefreshEntry(credentials, credentialsRateLimits);
 }
 private void ArrangeQueryExecutorGetTokenRateLimits(ICredentialsRateLimits result)
 {
     _fakeHelpQueryExecutor
     .CallsTo(x => x.GetCurrentCredentialsRateLimits())
     .Returns(result);
 }