Exemple #1
0
        public void QueryExecuted(string query, ITwitterCredentials credentials, int numberOfRequests = 1)
        {
            var rateLimit = _rateLimitCacheManager.GetQueryRateLimit(query, credentials);

            if (rateLimit != null)
            {
                var newRemainingValue = Math.Max(rateLimit.Remaining - numberOfRequests, 0);
                rateLimit.Remaining = newRemainingValue;
            }
        }
Exemple #2
0
        public void WaitForCredentialsRateLimit(string query, ITwitterCredentials credentials)
        {
            var queryRateLimit = _rateLimitCacheManager.GetQueryRateLimit(query, credentials);
            var timeToWait     = GetTimeToWaitFromQueryRateLimit(queryRateLimit);

            if (timeToWait > 0)
            {
                _queryAwaitingForRateLimitWeakEvent.Raise(this, new QueryAwaitingEventArgs(query, queryRateLimit, credentials));
                _threadHelper.Sleep(timeToWait);
            }
        }
        public async Task QueryExecuted(string query, ITwitterCredentials credentials, int numberOfRequests = 1)
        {
            var getRateLimitsFromCacheParameters = new GetEndpointRateLimitsParameters(query, RateLimitsSource.CacheOnly);
            var rateLimit = await _rateLimitCacheManager.GetQueryRateLimit(getRateLimitsFromCacheParameters, credentials).ConfigureAwait(false);

            if (rateLimit != null)
            {
                var newRemainingValue = Math.Max(rateLimit.Remaining - numberOfRequests, 0);
                rateLimit.Remaining = newRemainingValue;
            }
        }
Exemple #4
0
        public async Task PrepareTwitterRequest(ITwitterRequest request)
        {
            var twitterQuery = request.Query;

            twitterQuery.Url = CleanupQueryUrl(twitterQuery.Url); // TODO : THIS LOGIC SHOULD HAPPEN BEFORE ARRIVING HERE

            var rateLimitTrackerMode = request.ExecutionContext.RateLimitTrackerMode;

            if (rateLimitTrackerMode == RateLimitTrackerMode.None)
            {
                return;
            }

            // Use the RateLimitCacheManager instead of RateLimitHelper to get the queryRateLimits to ensure the cache is up to date!
            var credentialRateLimits = await _rateLimitCacheManager.GetCredentialsRateLimits(twitterQuery.TwitterCredentials).ConfigureAwait(false);

            IEndpointRateLimit queryRateLimit = null;

            // If we were not able to retrieve the credentials few ms before there is no reason why it would work now.
            if (credentialRateLimits != null)
            {
                var getEndpointRateLimitsFromCache = new GetEndpointRateLimitsParameters(twitterQuery.Url, RateLimitsSource.CacheOnly);
                queryRateLimit = await _rateLimitCacheManager.GetQueryRateLimit(getEndpointRateLimitsFromCache, twitterQuery.TwitterCredentials).ConfigureAwait(false);
            }

            var timeToWait = _rateLimitAwaiter.GetTimeToWaitFromQueryRateLimit(queryRateLimit, request.ExecutionContext);

            twitterQuery.CredentialsRateLimits = credentialRateLimits;
            twitterQuery.QueryRateLimit        = queryRateLimit;
            twitterQuery.DateWhenCredentialsWillHaveTheRequiredRateLimits = DateTime.UtcNow.Add(timeToWait);
        }
        private bool TryPrepareRequest(
            IHttpRequestParameters requestParameters,
            RateLimitTrackerMode rateLimitTrackerMode,
            ITwitterCredentials credentials,
            out ITwitterQuery twitterQuery)
        {
            credentials = credentials ?? _credentialsAccessor.CurrentThreadCredentials;

            if (credentials == null)
            {
                throw new TwitterNullCredentialsException();
            }

            twitterQuery             = _twitterQueryFactory.Create(requestParameters.Query, requestParameters.HttpMethod, credentials);
            twitterQuery.HttpContent = requestParameters.HttpContent;
            twitterQuery.Timeout     = requestParameters.Timeout ?? twitterQuery.Timeout;

            var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);


            if (rateLimitTrackerMode == RateLimitTrackerMode.TrackOnly ||
                rateLimitTrackerMode == RateLimitTrackerMode.TrackAndAwait)
            {
                // Use the RateLimitCacheManager instead of RateLimitHelper to get the queryRateLimits to ensure the cache is up to date!
                var credentialRateLimits = _rateLimitCacheManager.GetCredentialsRateLimits(twitterQuery.TwitterCredentials);

                IEndpointRateLimit queryRateLimit = null;

                // If we were not able to retrieve the credentials few ms before there is no reason why it would work now.
                if (credentialRateLimits != null)
                {
                    queryRateLimit = _rateLimitCacheManager.GetQueryRateLimit(requestParameters.Query, twitterQuery.TwitterCredentials);
                }

                var timeToWait = _rateLimitAwaiter.GetTimeToWaitFromQueryRateLimit(queryRateLimit);

                twitterQuery.CredentialsRateLimits = credentialRateLimits;
                twitterQuery.QueryRateLimit        = queryRateLimit;
                twitterQuery.DateWhenCredentialsWillHaveTheRequiredRateLimits = DateTime.Now.AddMilliseconds(timeToWait);

                _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

                if (beforeQueryExecuteEventArgs.Cancel)
                {
                    twitterQuery = null;
                    return(false);
                }

                if (rateLimitTrackerMode == RateLimitTrackerMode.TrackAndAwait)
                {
                    _rateLimitAwaiter.Wait(timeToWait);
                }
            }
            else
            {
                _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

                if (beforeQueryExecuteEventArgs.Cancel)
                {
                    twitterQuery = null;
                    return(false);
                }
            }

            _tweetinviEvents.RaiseBeforeExecuteAfterRateLimitAwait(beforeQueryExecuteEventArgs);

            return(true);
        }
Exemple #6
0
 /// <summary>
 /// Get the rate limits information for an url
 /// </summary>
 public static ITokenRateLimit GetQueryRateLimit(string query)
 {
     return(_rateLimitCacheManager.GetQueryRateLimit(query, Auth.Credentials));
 }
Exemple #7
0
 public Task <IEndpointRateLimit> GetEndpointRateLimit(IGetEndpointRateLimitsParameters parameters)
 {
     return(_rateLimitCacheManager.GetQueryRateLimit(parameters, _client.Credentials));
 }