Esempio n. 1
0
        // Get User
        public IAuthenticatedUser GetAuthenticatedUser(ITwitterCredentials credentials = null, IGetAuthenticatedUserParameters parameters = null)
        {
            IUserDTO userDTO;

            if (credentials == null)
            {
                credentials = _credentialsAccessor.CurrentThreadCredentials;
                userDTO = _userFactoryQueryExecutor.GetAuthenticatedUser(parameters);
            }
            else
            {
                userDTO = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () =>
                {
                    return _userFactoryQueryExecutor.GetAuthenticatedUser(parameters);
                });
            }

            var authenticatedUser = GenerateAuthenticatedUserFromDTO(userDTO);

            if (authenticatedUser != null)
            {
                authenticatedUser.SetCredentials(credentials);
            }

            return authenticatedUser;
        }
 public LinkedIn.Results.IResult<Results.UserIdListResult> GetFollowerIds(ITwitterCredentials credentials, string username)
 {
     return this.executer.Execute(
         credentials, new FollowerIdsCommand {
             ScreenName = username,
         });
 }
 public IResult<string> UpdateStatus(ITwitterCredentials credentials, string status)
 {
     return this.executer.Execute(
         credentials, new StatusesUpdateCommand {
             Status = status,
         });
 }
 public IResult<Results.Tweet[]> UserTimeline(ITwitterCredentials credentials, string username)
 {
     return this.executer.Execute(
         credentials, new StatusesUserTimelineCommand {
             Username = username
         });
 }
        private ICredentialsRateLimits GetTokenRateLimitsFromTwitter(ITwitterCredentials credentials)
        {
            if (_isRetrievingData)
            {
                return null;
            }

            var isApplicationOnlyCreds = string.IsNullOrEmpty(credentials.AccessToken) || string.IsNullOrEmpty(credentials.AccessTokenSecret);
            if (isApplicationOnlyCreds && string.IsNullOrEmpty(credentials.ApplicationOnlyBearerToken))
            {
                return null;
            }

            _isRetrievingData = true;
            var result = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () =>
            {
                var twitterQuery = _twitterQueryFactory.Create(_helpQueryGenerator.GetCredentialsLimitsQuery(), HttpMethod.GET, credentials);

                try
                {
                    string jsonResponse = _twitterRequester.ExecuteQuery(twitterQuery);
                    return _jsonObjectConverter.DeserializeObject<ICredentialsRateLimits>(jsonResponse);
                }
                catch (TwitterException)
                {
                    return null;
                }
            });

            _isRetrievingData = false;
            return result;
        }
        public string ExecuteQuery(
            string queryURL, 
            HttpMethod httpMethod, 
            TwitterClientHandler handler = null,
            ITwitterCredentials credentials = null)
        {
            CleanupQueryURL(ref queryURL);
            var rateLimitTrackerOption = _tweetinviSettingsAccessor.RateLimitTrackerOption;
            
            ITwitterQuery twitterQuery;
            if (!TryPrepareRequest(queryURL, httpMethod, rateLimitTrackerOption, credentials, out twitterQuery))
            {
                return null;
            }

            try
            {
                var jsonResult = _twitterRequester.ExecuteQuery(twitterQuery, handler);

                QueryCompleted(twitterQuery, jsonResult, rateLimitTrackerOption);

                return jsonResult;
            }
            catch (TwitterException ex)
            {
                HandleException(queryURL, rateLimitTrackerOption, ex.StatusCode, twitterQuery);
                
                throw;
            }
        }
 public ITokenRateLimits GetCredentialsRateLimits(ITwitterCredentials credentials)
 {
     var savedCredentials = _credentialsAccessor.CurrentThreadCredentials;
     _credentialsAccessor.CurrentThreadCredentials = credentials;
     var rateLimits = GetCurrentCredentialsRateLimits();
     _credentialsAccessor.CurrentThreadCredentials = savedCredentials;
     return rateLimits;
 }
        public void Itinialize()
        {
            _fakeBuilder = new FakeClassBuilder<RateLimitCache>();

            _credentials = A.Fake<ITwitterCredentials>();
            _credentials2 = A.Fake<ITwitterCredentials>();
            _credentialsRateLimits = A.Fake<ICredentialsRateLimits>();
            _credentialsRateLimits2 = A.Fake<ICredentialsRateLimits>();
        }
 public QueryAwaitingEventArgs(
     string query,
     ITokenRateLimit queryRateLimit,
     ITwitterCredentials twitterCredentials)
 {
     _query = query;
     _queryRateLimit = queryRateLimit;
     _twitterCredentials = twitterCredentials;
 }
Esempio n. 10
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;
            }
        }
        public ICredentialsRateLimits GetCredentialsRateLimits(ITwitterCredentials credentials)
        {
            var rateLimits = _rateLimitCache.GetCredentialsRateLimits(credentials);
            if (rateLimits == null)
            {
                rateLimits = RefreshCredentialsRateLimits(credentials);
            }

            return rateLimits;
        }
        public ClientProvider(ITwitterCredentials credentials)
        {
            if (credentials == null) throw new ArgumentNullException(nameof(credentials));

            _container = new WindsorContainer();
            _container.AddFacility<TypedFactoryFacility>();

            _container.Register(Component.For<ITwitterCredentials>().Instance(credentials));
            _container.Register(Classes.FromAssemblyInThisApplication().Pick().WithServiceAllInterfaces().LifestyleTransient());
        }
Esempio n. 13
0
        public void CheckAuthorization()
        {
            _credentials = _credentialsStore.Load();

            if (string.IsNullOrEmpty(_credentials.ConsumerKey) ||
                string.IsNullOrEmpty(_credentials.ConsumerSecret))
                AuthorizationStatus = AuthorizationStatus.InvalidApplication;
            else
                CheckCredentials();
        }
        public RequestAuthorizer(ITwitterCredentials credentials, IHttpUtils oAuthUtils)
        {
            if (credentials == null)
                throw new ArgumentNullException("credentials");

            if (oAuthUtils == null)
                throw new ArgumentNullException("oAuthUtils");

            _credentials = credentials;
            _oAuthUtils = oAuthUtils;
        }
Esempio n. 15
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);
            }
        }
Esempio n. 16
0
        public ILoggedUser GetLoggedUser(ITwitterCredentials credentials)
        {
            var userDTO = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () =>
            {
                return _userFactoryQueryExecutor.GetLoggedUser();
            });

            var loggedUser = GenerateLoggedUserFromDTO(userDTO);
            loggedUser.SetCredentials(credentials);

            return loggedUser;
        }
Esempio n. 17
0
        public ICredentialsRateLimits GetCredentialsRateLimits(ITwitterCredentials credentials)
        {
            lock (_lockCredentialsRateLimitsDictionary)
            {
                ICredentialsRateLimits credentialsRateLimits;
                if (credentials != null && _credentialsRateLimits.TryGetValue(credentials, out credentialsRateLimits))
                {
                    return credentialsRateLimits;
                }

                return null;
            }
        }
        public IEndpointRateLimit GetQueryRateLimit(string query, ITwitterCredentials credentials)
        {
            var rateLimits = _rateLimitCache.GetCredentialsRateLimits(credentials);
            var queryRateLimit = _rateLimitHelper.GetEndpointRateLimitFromQuery(query, rateLimits);

            if (rateLimits == null || DoesQueryNeedsToRefreshTheCacheInformation(queryRateLimit))
            {
                rateLimits = RefreshCredentialsRateLimits(credentials);
                queryRateLimit = _rateLimitHelper.GetEndpointRateLimitFromQuery(query, rateLimits);
            }

            return queryRateLimit;
        }
 public void RefreshEntry(ITwitterCredentials credentials, ITokenRateLimits credentialsRateLimits)
 {
     lock (_lockTokenRateLimitsDictionary)
     {
         if (_tokenRateLimits.ContainsKey(credentials))
         {
             _tokenRateLimits[credentials] = credentialsRateLimits;
         }
         else
         {
             _tokenRateLimits.Add(credentials, credentialsRateLimits);
         }
     }
 }
        public void TestInitialize()
        {
            _fakeBuilder = new FakeClassBuilder<RateLimitAwaiter>();
            _fakeRateLimitCacheManager = _fakeBuilder.GetFake<IRateLimitCacheManager>();
            _fakeCredentialsAccessor = _fakeBuilder.GetFake<ICredentialsAccessor>();
            _fakeThreadHelper = _fakeBuilder.GetFake<IThreadHelper>();

            _credentials = A.Fake<ITwitterCredentials>();
            _endpointRateLimit = A.Fake<IEndpointRateLimit>();
            _endpointRateLimit.CallsTo(x => x.Remaining).Returns(0);
            _endpointRateLimit.CallsTo(x => x.ResetDateTimeInMilliseconds).Returns(TIME_TO_WAIT);

            _fakeRateLimitCacheManager.CallsTo(x => x.GetQueryRateLimit(TEST_QUERY, _credentials)).Returns(_endpointRateLimit);
            _fakeCredentialsAccessor.CallsTo(x => x.CurrentThreadCredentials).Returns(_credentials);
        }
 public ITokenRateLimits GetTokenRateLimits(ITwitterCredentials credentials)
 {
     lock (_lockTokenRateLimitsDictionary)
     {
         ITokenRateLimits credentialsRateLimits;
         if (_tokenRateLimits.TryGetValue(credentials, out credentialsRateLimits))
         {
             return credentialsRateLimits;
         }
         else
         {
             return null;
         }
     }
 }
Esempio n. 22
0
 public void Clear(ITwitterCredentials credentials)
 {
     // We want to lock both the refresh dictionary access so that we ensure the dictionary
     // is not cleared during a refresh or when it is being accessed
     lock (_lockRefresh)
     {
         lock (_lockCredentialsRateLimitsDictionary)
         {
             if (_credentialsRateLimits.ContainsKey(credentials))
             {
                 _credentialsRateLimits.Remove(credentials);
             }
         }
     }
 }
Esempio n. 23
0
        //Will run in GUI, waits for input.
        public void authenticateUser(string pin)
        {
            // With this pin code it is now possible to get the credentials back from Twitter
            this.userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pin, authenticationContext);

            // Use the user credentials in your application
            Auth.SetCredentials(this.userCredentials);

            // Get the AuthenticatedUser from the current thread credentials
            this.User = Tweetinvi.User.GetAuthenticatedUser();

            // Stores the usercredentials for later use
            this.storeUserCredentials(this.userCredentials.AccessToken, this.userCredentials.AccessTokenSecret);

            this.twitterUser = new TwitterUser(this.user);
        }
 public IResult<StatusesResult> Tweets(
     ITwitterCredentials credentials, string query, Results.Language? language,
     Results.TweetsResultType? resultType, int? count, DateTime? until, string sinceId, string maxId,
     bool includeEntities)
 {
     return this.executer.Execute(
         credentials, new SearchTweetsCommand {
             Query = query,
             Language = language,
             ResultType = resultType,
             Count = count,
             Until = until,
             SinceId = sinceId,
             MaxId = maxId,
             IncludeEntities = includeEntities,
         });
 }
Esempio n. 25
0
        private ITokenRateLimits GetTokenRateLimitsFromTwitter(ITwitterCredentials credentials)
        {
            if (_isRetrievingData)
            {
                return null;
            }

            _isRetrievingData = true;
            var result = _credentialsAccessor.ExecuteOperationWithCredentials(credentials, () =>
            {
                var twitterQuery = _twitterQueryFactory.Create(_helpQueryGenerator.GetCredentialsLimitsQuery(), HttpMethod.GET, credentials);
                string jsonResponse = _twitterRequester.ExecuteQuery(twitterQuery);
                return _jsonObjectConverter.DeserializeObject<ITokenRateLimits>(jsonResponse);
            });

            _isRetrievingData = false;
            return result;
        }
Esempio n. 26
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);
            }
        }
Esempio n. 27
0
        public void QueryExecuted(string query, ITwitterCredentials credentials, Dictionary<string, IEnumerable<string>> rateLimitHeaders)
        {
            if (rateLimitHeaders != null && rateLimitHeaders.Count > 0)
            {
                var rateLimit = _rateLimitCacheManager.GetOrCreateQueryRateLimit(query, credentials);

                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
            {
                QueryExecuted(query, credentials);
            }
        }
 public static async Task<ITokenRateLimits> GetCredentialsRateLimits(ITwitterCredentials credentials)
 {
     return await Sync.ExecuteTaskAsync(() => RateLimit.GetCredentialsRateLimits(credentials));
 }
 public IResult<RateLimitStatusResult> Tweets(ITwitterCredentials credentials)
 {
     return this.executer.Execute(credentials, new ApplicationRateLimitStatusCommand());
 }
 public IResult<Results.Tweet[]> HomeTimeline(ITwitterCredentials credentials)
 {
     return this.executer.Execute(
         credentials, new StatusesHomeTimelineCommand {});
 }