コード例 #1
0
        private IEnumerable <IOAuthQueryParameter> GetTwitterQueryParameters(ITwitterQuery twitterQuery)
        {
            var queryParameters = twitterQuery.QueryParameters;

            if (twitterQuery.OAuthCredentials == null)
            {
                if (twitterQuery.TemporaryCredentials != null)
                {
                    return(_webRequestGenerator.GenerateApplicationParameters(twitterQuery.TemporaryCredentials, queryParameters));
                }

                if (_credentialsAccessor.CurrentThreadCredentials == null)
                {
                    throw new TwitterNullCredentialsException();
                }

                twitterQuery.OAuthCredentials = _credentialsAccessor.CurrentThreadCredentials;
            }

            if (queryParameters == null)
            {
                queryParameters = _webRequestGenerator.GenerateParameters(twitterQuery.OAuthCredentials);
            }

            return(queryParameters);
        }
コード例 #2
0
 public QueryAfterExecuteExceptionEventArgs(
     ITwitterQuery twitterQuery,
     TwitterException exception)
     : base(twitterQuery, null, null)
 {
     Exception = exception;
 }
コード例 #3
0
        protected virtual Task <HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (_action != null)
            {
                _action(twitterQuery, request);
            }

            string authorizationHeader;

            if (_func != null)
            {
                authorizationHeader = _func(twitterQuery, request);
            }
            else
            {
                var credentials = twitterQuery.TwitterCredentials;

                if (!string.IsNullOrEmpty(credentials.AccessToken) &&
                    !string.IsNullOrEmpty(credentials.AccessTokenSecret))
                {
                    var uri = new Uri(twitterQuery.QueryURL);
                    var credentialsParameters = _webRequestGenerator.GenerateParameters(twitterQuery.TwitterCredentials);
                    authorizationHeader = _webRequestGenerator.GenerateAuthorizationHeader(uri, twitterQuery.HttpMethod, credentialsParameters);
                }
                else
                {
                    authorizationHeader = string.Format("Bearer {0}", credentials.ApplicationOnlyBearerToken);
                }
            }

            return(SendAsync(request, cancellationToken, authorizationHeader));
        }
コード例 #4
0
        public async Task <HttpResponseMessage> GetHttpResponseAsync(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
        {
            using (var client = GetHttpClient(twitterQuery, handler))
            {
                client.Timeout = twitterQuery.Timeout;

                var httpMethod = new HttpMethod(twitterQuery.HttpMethod.ToString());

                if (twitterQuery.HttpContent == null)
                {
                    return(await client.SendAsync(new HttpRequestMessage(httpMethod, twitterQuery.Url)).ConfigureAwait(false));
                }
                else
                {
                    if (httpMethod == HttpMethod.Post)
                    {
                        return(await client.PostAsync(twitterQuery.Url, twitterQuery.HttpContent).ConfigureAwait(false));
                    }

                    if (httpMethod == HttpMethod.Put)
                    {
                        return(await client.PutAsync(twitterQuery.Url, twitterQuery.HttpContent).ConfigureAwait(false));
                    }

                    throw new ArgumentException("Cannot send HttpContent in a WebRequest that is not POST or PUT.");
                }
            }
        }
コード例 #5
0
        public async Task <HttpResponseMessage> GetHttpResponse(ITwitterQuery twitterQuery, HttpContent httpContent = null, TwitterClientHandler handler = null)
        {
            handler = handler ?? new TwitterClientHandler();
            handler.TwitterQuery = twitterQuery;

            using (var client = new HttpClient(handler))
            {
                client.Timeout = twitterQuery.Timeout;

                var httpMethod = new HttpMethod(twitterQuery.HttpMethod.ToString());

                if (httpContent == null)
                {
                    return(await client.SendAsync(new HttpRequestMessage(httpMethod, twitterQuery.QueryURL)).ConfigureAwait(false));
                }
                else
                {
                    if (httpMethod != HttpMethod.Post)
                    {
                        throw new ArgumentException("Cannot send HttpContent in a WebRequest that is not POST.");
                    }

                    return(await client.PostAsync(twitterQuery.QueryURL, httpContent).ConfigureAwait(false));
                }
            }
        }
コード例 #6
0
        private string GetJsonResponseFromReader(StreamReader reader, ITwitterQuery twitterQuery)
        {
            var requestTask = reader.ReadLineAsync();

#if NET_CORE
            var resultingTask = Task.WhenAny(requestTask, Task.Delay(STREAM_DISCONNECTED_DELAY)).Result;
#else
            var resultingTask = TaskEx.WhenAny(requestTask, TaskEx.Delay(STREAM_DISCONNECTED_DELAY)).Result;
#endif

            if (resultingTask != requestTask)
            {
                requestTask.ContinueWith(json =>
                {
                    // We want to ensure that we are properly handling reuqest Tasks exceptions
                    // so that no scheduler actually receive any potential exception received.
                }, TaskContinuationOptions.OnlyOnFaulted);

                var twitterQueryParameter =
                    _twitterTimeoutExceptionFactory.GenerateParameterOverrideWrapper("twitterQuery", twitterQuery);
                var twitterTimeoutException = _twitterTimeoutExceptionFactory.Create(twitterQueryParameter);
                throw (Exception)twitterTimeoutException;
            }

            var jsonResponse = requestTask.Result;
            return(jsonResponse);
        }
コード例 #7
0
        public async Task<HttpResponseMessage> GetHttpResponse(ITwitterQuery twitterQuery, HttpContent httpContent = null, TwitterClientHandler handler = null)
        {
            handler = handler ?? new TwitterClientHandler();
            handler.TwitterQuery = twitterQuery;

            using (var client = new HttpClient(handler))
            {
                client.Timeout = twitterQuery.Timeout;

                var httpMethod = new HttpMethod(twitterQuery.HttpMethod.ToString());

                if (httpContent == null)
                {
                    return await client.SendAsync(new HttpRequestMessage(httpMethod, twitterQuery.QueryURL)).ConfigureAwait(false);
                }
                else
                {
                    if (httpMethod != HttpMethod.Post)
                    {
                        throw new ArgumentException("Cannot send HttpContent in a WebRequest that is not POST.");
                    }

                    return await client.PostAsync(twitterQuery.QueryURL, httpContent);
                }
            }
        }
コード例 #8
0
        protected override Task<HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers = _webRequestGenerator.GenerateApplicationParameters(twitterQuery.TwitterCredentials, _authenticationToken, new[] { _queryParameter });
            var authorizatinHeader = _webRequestGenerator.GenerateAuthorizationHeader(request.RequestUri, request.Method.ToTweetinviHttpMethod(), headers);

            return base.SendAsync(request, cancellationToken, authorizatinHeader);
        }
コード例 #9
0
 public FrontPageService(IFeaturedArticlesQuery articlesQuery, ITwitterQuery twitterQuery,
                         IOptions <DeploymentSlot> deploymentSlot)
 {
     this.articlesQuery  = articlesQuery;
     this.twitterQuery   = twitterQuery;
     this.deploymentSlot = deploymentSlot.Value;
 }
コード例 #10
0
 public TwitterTimeoutException(ITwitterQuery twitterQuery)
     : base(twitterQuery.QueryURL, string.Format("{0} web request timed out.", twitterQuery.QueryURL))
 {
     Timeout = twitterQuery.Timeout;
     TwitterDescription = string.Format("Twitter was not able to perform your query within the Timeout limit of {0} ms.", twitterQuery.Timeout.TotalMilliseconds);
     CreationDate = DateTime.Now;
 }
コード例 #11
0
        private IEnumerable<IOAuthQueryParameter> GetTwitterQueryParameters(ITwitterQuery twitterQuery)
        {
            var queryParameters = twitterQuery.QueryParameters;

            if (twitterQuery.OAuthCredentials == null)
            {
                if (twitterQuery.TemporaryCredentials != null)
                {
                    return _webRequestGenerator.GenerateApplicationParameters(twitterQuery.TemporaryCredentials, queryParameters);
                }

                if (_credentialsAccessor.CurrentThreadCredentials == null)
                {
                    throw new TwitterNullCredentialsException();
                }

                twitterQuery.OAuthCredentials = _credentialsAccessor.CurrentThreadCredentials;
            }

            if (queryParameters == null)
            {
                queryParameters = _webRequestGenerator.GenerateParameters(twitterQuery.OAuthCredentials);
            }

            return queryParameters;
        }
コード例 #12
0
 public TwitterTimeoutException(ITwitterQuery twitterQuery)
     : base(twitterQuery, string.Format("{0} web request timed out.", twitterQuery.QueryURL))
 {
     Timeout            = twitterQuery.Timeout;
     TwitterDescription = string.Format("Twitter was not able to perform your query within the Timeout limit of {0} ms.", twitterQuery.Timeout.TotalMilliseconds);
     CreationDate       = DateTime.Now;
 }
コード例 #13
0
ファイル: TwitterException.cs プロジェクト: x0rzkov/tweetinvi
 public TwitterException(ITwitterExceptionInfo[] exceptionInfos, ITwitterQuery twitterQuery)
     : this(twitterQuery)
 {
     CreationDate          = DateTime.Now;
     TwitterExceptionInfos = exceptionInfos;
     TwitterQuery          = twitterQuery;
 }
コード例 #14
0
        protected override Task <HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var headers            = _webRequestGenerator.GenerateApplicationParameters(twitterQuery.TwitterCredentials, _authenticationToken, new[] { _queryParameter });
            var authorizatinHeader = _webRequestGenerator.GenerateAuthorizationHeader(request.RequestUri, request.Method.ToTweetinviHttpMethod(), headers);

            return(base.SendAsync(request, cancellationToken, authorizatinHeader));
        }
コード例 #15
0
 public TwitterException GenerateTwitterException(
     WebException webException,
     ITwitterQuery twitterQuery,
     int statusCode)
 {
     return(_twitterExceptionFactory.Create(webException, twitterQuery, statusCode));
 }
コード例 #16
0
        public async Task <string> SetTwitterQueryAuthorizationHeaderAsync(ITwitterQuery twitterQuery)
        {
            var credentials = twitterQuery.TwitterCredentials;

            if (!string.IsNullOrEmpty(credentials.AccessToken) && !string.IsNullOrEmpty(credentials.AccessTokenSecret))
            {
                var uri = new Uri(twitterQuery.Url);
                var credentialsParameters = GenerateParameters(twitterQuery.TwitterCredentials);

                if (twitterQuery.HttpContent != null && twitterQuery.IsHttpContentPartOfQueryParams)
                {
                    twitterQuery.AuthorizationHeader = await GenerateAuthorizationHeaderAsync(uri, twitterQuery.HttpContent, twitterQuery.HttpMethod, credentialsParameters).ConfigureAwait(false);
                }
                else
                {
                    twitterQuery.AuthorizationHeader = GenerateAuthorizationHeader(uri, twitterQuery.HttpMethod, credentialsParameters);
                }
            }
            else
            {
                twitterQuery.AuthorizationHeader = $"Bearer {credentials.BearerToken}";
            }

            return(twitterQuery.AuthorizationHeader);
        }
コード例 #17
0
        public IWebRequestResult ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable <byte[]> binaries)
        {
            return(ExecuteTwitterQuerySafely(twitterQuery, () =>
            {
                HttpResponseMessage httpResponseMessage = null;

                try
                {
                    httpResponseMessage = _httpClientWebHelper.GetHttpResponse(twitterQuery).Result;

                    var result = GetWebResultFromResponse(twitterQuery.QueryURL, httpResponseMessage);

                    var stream = result.ResultStream;

                    if (stream != null)
                    {
                        result.Binary = StreamToBinary(stream);
                        result.Text = Encoding.UTF8.GetString(result.Binary);
                    }

                    return result;
                }
                catch (Exception)
                {
                    if (httpResponseMessage != null)
                    {
                        httpResponseMessage.Dispose();
                    }

                    throw;
                }
            }));
        }
コード例 #18
0
        protected override Task<HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var authorizationHeader = GetBearerTokenAuthorizationHeader(twitterQuery.TwitterCredentials);
            request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");

            return SendAsync(request, cancellationToken, authorizationHeader);
        }
コード例 #19
0
        private bool TryPrepareRequest(
            string query,
            HttpMethod httpMethod,
            RateLimitTrackerOptions rateLimitTrackerOption,
            ITwitterCredentials credentials,
            out ITwitterQuery twitterQuery)
        {
            twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials ?? _credentialsAccessor.CurrentThreadCredentials);

            var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);

            _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

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

            if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait)
            {
                _rateLimitAwaiter.WaitForCurrentCredentialsRateLimit(query);
            }

            return(true);
        }
コード例 #20
0
ファイル: TwitterException.cs プロジェクト: x0rzkov/tweetinvi
 protected TwitterException(ITwitterQuery twitterQuery, string message)
     : base(message)
 {
     CreationDate = DateTime.Now;
     URL          = twitterQuery?.QueryURL;
     TwitterQuery = twitterQuery;
 }
コード例 #21
0
        // Multipart
        public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable <byte[]> binaries)
        {
            return(ExecuteTwitterQuerySafely(twitterQuery, () =>
            {
                HttpResponseMessage httpResponseMessage = null;

                try
                {
                    var multiPartContent = GetMultipartFormDataContent(contentId, binaries);
                    httpResponseMessage = _httpClientWebHelper.GetHttpResponse(twitterQuery, multiPartContent).Result;

                    var result = GetWebResultFromResponse(twitterQuery.QueryURL, httpResponseMessage);

                    var stream = result.ResultStream;

                    if (stream != null)
                    {
                        var responseReader = new StreamReader(stream);
                        return responseReader.ReadLine();
                    }

                    return null;
                }
                catch (Exception)
                {
                    if (httpResponseMessage != null)
                    {
                        httpResponseMessage.Dispose();
                    }

                    throw;
                }
            }));
        }
コード例 #22
0
        protected virtual Task<HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (_action != null)
            {
                _action(twitterQuery, request);
            }

            string authorizationHeader;

            if (_func != null)
            {
                authorizationHeader = _func(twitterQuery, request);
            }
            else
            {
                var credentials = twitterQuery.TwitterCredentials;

                if (!string.IsNullOrEmpty(credentials.AccessToken) &&
                    !string.IsNullOrEmpty(credentials.AccessTokenSecret))
                {
                    var uri = new Uri(twitterQuery.QueryURL);
                    var credentialsParameters = _webRequestGenerator.GenerateParameters(twitterQuery.TwitterCredentials);
                    authorizationHeader = _webRequestGenerator.GenerateAuthorizationHeader(uri, twitterQuery.HttpMethod, credentialsParameters);
                }
                else
                {
                    authorizationHeader = string.Format("Bearer {0}", credentials.ApplicationOnlyBearerToken);
                }
            }

            return SendAsync(request, cancellationToken, authorizationHeader);
        }
コード例 #23
0
        public HttpWebRequest GetQueryWebRequest(ITwitterQuery twitterQuery)
        {
            var url = twitterQuery.QueryURL;
            var httpMethod = twitterQuery.HttpMethod;
            var queryParameters = GetCredentialsQueryParameters(twitterQuery);

            return GetQueryWebRequestInternal(url, httpMethod, queryParameters);
        }
コード例 #24
0
        public HttpWebRequest GetQueryWebRequest(ITwitterQuery twitterQuery)
        {
            var url             = twitterQuery.QueryURL;
            var httpMethod      = twitterQuery.HttpMethod;
            var queryParameters = GetCredentialsQueryParameters(twitterQuery);

            return(GetQueryWebRequestInternal(url, httpMethod, queryParameters));
        }
コード例 #25
0
        protected override Task <HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var authorizationHeader = GetBearerTokenAuthorizationHeader(twitterQuery.TwitterCredentials);

            request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");

            return(SendAsync(request, cancellationToken, authorizationHeader));
        }
コード例 #26
0
        public TwitterException AddWebException(WebException webException, ITwitterQuery twitterQuery)
        {
            var twitterException = GenerateTwitterException(webException, twitterQuery);

            AddTwitterException(twitterException);

            // Cannot throw from an interface :(
            return(twitterException);
        }
コード例 #27
0
        public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable <byte[]> binaries)
        {
            if (binaries.IsNullOrEmpty())
            {
                return(ExecuteQuery(twitterQuery));
            }

            return(_webRequestExecutor.ExecuteMultipartQuery(twitterQuery, contentId, binaries));
        }
コード例 #28
0
        public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable<byte[]> binaries)
        {
            if (binaries.IsNullOrEmpty())
            {
                return ExecuteQuery(twitterQuery);
            }

            return _webRequestExecutor.ExecuteMultipartQuery(twitterQuery, contentId, binaries);
        }
コード例 #29
0
        private void QueryCompleted(ITwitterQuery twitterQuery, string jsonResult, RateLimitTrackerMode rateLimitTrackerMode)
        {
            if (rateLimitTrackerMode != RateLimitTrackerMode.None)
            {
                _rateLimitUpdater.QueryExecuted(twitterQuery.QueryURL, _credentialsAccessor.CurrentThreadCredentials);
            }

            _tweetinviEvents.RaiseAfterQueryExecuted(new QueryAfterExecuteEventArgs(twitterQuery, jsonResult));
        }
コード例 #30
0
ファイル: TwitterException.cs プロジェクト: x0rzkov/tweetinvi
 public TwitterException(
     IWebExceptionInfoExtractor webExceptionInfoExtractor,
     IWebRequestResult webRequestResult,
     ITwitterQuery twitterQuery)
     : this(twitterQuery)
 {
     StatusCode            = webRequestResult.StatusCode;
     TwitterExceptionInfos = webExceptionInfoExtractor.GetTwitterExceptionInfosFromStream(webRequestResult.ResultStream);
     TwitterDescription    = webExceptionInfoExtractor.GetStatusCodeDescription(StatusCode);
 }
コード例 #31
0
 public AfterExecutingQueryEventArgs(
     ITwitterQuery twitterQuery,
     string httpContent,
     Dictionary <string, IEnumerable <string> > httpHeaders)
     : base(twitterQuery)
 {
     HttpContent       = httpContent;
     HttpHeaders       = httpHeaders;
     CompletedDateTime = DateTime.Now;
 }
コード例 #32
0
 public QueryAfterExecuteEventArgs(
     ITwitterQuery twitterQuery,
     string httpContent,
     Dictionary<string, IEnumerable<string>> httpHeaders)
     : base(twitterQuery)
 {
     HttpContent = httpContent;
     HttpHeaders = httpHeaders;
     CompletedDateTime = DateTime.Now;
 }
コード例 #33
0
        private void QueryCompleted(ITwitterQuery twitterQuery, IWebRequestResult webRequestResult, RateLimitTrackerMode rateLimitTrackerMode)
        {
            if (rateLimitTrackerMode != RateLimitTrackerMode.None)
            {
                var rateLimitHeaders = webRequestResult.Headers.Where(kvp => kvp.Key.StartsWith("x-rate-limit-")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                _rateLimitUpdater.QueryExecuted(twitterQuery.QueryURL, twitterQuery.TwitterCredentials, rateLimitHeaders);
            }

            _tweetinviEvents.RaiseAfterQueryExecuted(new QueryAfterExecuteEventArgs(twitterQuery, webRequestResult.Response, webRequestResult.Headers));
        }
コード例 #34
0
        public TwitterException TryLogWebException(WebException webException, ITwitterQuery twitterQuery)
        {
            var twitterException = GenerateTwitterException(webException, twitterQuery);

            if (LogExceptions)
            {
                AddTwitterException(twitterException);
            }

            return(twitterException);
        }
コード例 #35
0
        public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable<IMedia> medias)
        {
            if (medias == null || medias.IsEmpty())
            {
                return ExecuteQuery(twitterQuery);
            }

            var webRequest = _twitterRequestGenerator.GenerateMultipartWebRequest(twitterQuery, contentId, medias);
            var result = _webRequestExecutor.ExecuteMultipartRequest(webRequest);

            return result;
        }
コード例 #36
0
        public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable <IMedia> medias)
        {
            if (medias == null || medias.IsEmpty())
            {
                return(ExecuteQuery(twitterQuery));
            }

            var webRequest = _twitterRequestGenerator.GenerateMultipartWebRequest(twitterQuery, contentId, medias);
            var result     = _webRequestExecutor.ExecuteMultipartRequest(webRequest);

            return(result);
        }
コード例 #37
0
        public HttpClient GetHttpClient(ITwitterQuery twitterQuery, ITwitterClientHandler twitterHandler = null)
        {
            var handler = (twitterHandler as TwitterClientHandler) ?? new TwitterClientHandler();
            handler.TwitterQuery = twitterQuery;

            var client = new HttpClient(handler)
            {
                Timeout = twitterQuery.Timeout,
            };

            return client;
        }
コード例 #38
0
ファイル: TwitterException.cs プロジェクト: x0rzkov/tweetinvi
 public TwitterException(
     IWebExceptionInfoExtractor webExceptionInfoExtractor,
     WebException webException,
     ITwitterQuery twitterQuery,
     int defaultStatusCode = DEFAULT_STATUS_CODE)
     : this(twitterQuery, webException.Message)
 {
     WebException          = webException;
     StatusCode            = webExceptionInfoExtractor.GetWebExceptionStatusNumber(webException, defaultStatusCode);
     TwitterExceptionInfos = webExceptionInfoExtractor.GetTwitterExceptionInfo(webException);
     TwitterDescription    = webExceptionInfoExtractor.GetStatusCodeDescription(StatusCode);
 }
コード例 #39
0
        private bool TryPrepareRequest(
            string query,
            HttpMethod httpMethod,
            RateLimitTrackerOptions rateLimitTrackerOption,
            ITwitterCredentials credentials,
            out ITwitterQuery twitterQuery)
        {
            credentials = credentials ?? _credentialsAccessor.CurrentThreadCredentials;

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

            twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials);

            var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);


            if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackOnly ||
                rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait)
            {
                var timeToWait = _rateLimitAwaiter.TimeToWaitBeforeTwitterRequest(query, twitterQuery.TwitterCredentials);

                twitterQuery.DateWhenCredentialsWillHaveRequiredRateLimits = DateTime.Now.AddMilliseconds(timeToWait);
                _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

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

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

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

            _tweetinviEvents.RaiseBeforeExecuteAfterRateLimitAwait(beforeQueryExecuteEventArgs);

            return(true);
        }
コード例 #40
0
        public HttpClient GetHttpClient(ITwitterQuery twitterQuery, ITwitterClientHandler twitterHandler = null)
        {
            var handler = (twitterHandler as TwitterClientHandler) ?? new TwitterClientHandler();

            handler.TwitterQuery = twitterQuery;

            var client = new HttpClient(handler)
            {
                Timeout = twitterQuery.Timeout,
            };

            return(client);
        }
コード例 #41
0
        private async Task <StreamReader> GetStreamReader(HttpClient client, ITwitterQuery twitterQuery)
        {
            try
            {
                var uri             = new Uri(twitterQuery.QueryURL);
                var endpoint        = uri.GetEndpointURL();
                var queryParameters = uri.Query.Remove(0, 1);
                var httpMethod      = new HttpMethod(twitterQuery.HttpMethod.ToString());

                HttpRequestMessage request;

                if (httpMethod == HttpMethod.Post)
                {
                    request = new HttpRequestMessage(httpMethod, endpoint)
                    {
                        Content = new StringContent(queryParameters, Encoding.UTF8, "application/x-www-form-urlencoded")
                    };
                }
                else
                {
                    request = new HttpRequestMessage(httpMethod, twitterQuery.QueryURL);
                }


                var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

                _currentResponseHttpStatusCode = (int)response.StatusCode;
                var body = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                return(new StreamReader(body, Encoding.GetEncoding("utf-8")));
            }
            catch (WebException wex)
            {
                client.Dispose();
                HandleWebException(wex);
            }
            catch (Exception ex)
            {
                client.Dispose();

                if (ex is AggregateException && ex.InnerException is WebException)
                {
                    HandleWebException(ex.InnerException as WebException);
                }

                _lastException = ex;
                SetStreamState(StreamState.Stop);
            }

            return(null);
        }
コード例 #42
0
        protected override Task<HttpResponseMessage> SendAsync(ITwitterQuery twitterQuery, HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var credentials = twitterQuery.TwitterCredentials;
            var accessToken = credentials.AccessToken ?? credentials.ApplicationOnlyBearerToken;
            request.Content = new StringContent("access_token=" + accessToken, Encoding.UTF8, "application/x-www-form-urlencoded");

            if (credentials.AccessToken != null)
            {
                return base.SendAsync(twitterQuery, request, cancellationToken);
            }

            var authorizationHeader = BearerHttpHandler.GetBearerTokenAuthorizationHeader(credentials);
            return base.SendAsync(request, cancellationToken, authorizationHeader);
        }
コード例 #43
0
        public IEnumerable<IOAuthQueryParameter> GetCredentialsQueryParameters(ITwitterQuery twitterQuery)
        {
            var queryParameters = twitterQuery.QueryParameters;

            if (twitterQuery.TwitterCredentials == null)
            {
                if (_credentialsAccessor.CurrentThreadCredentials == null)
                {
                    throw new TwitterNullCredentialsException();
                }

                twitterQuery.TwitterCredentials = _credentialsAccessor.CurrentThreadCredentials;
            }

            if (queryParameters == null)
            {
                queryParameters = _webRequestGenerator.GenerateParameters(twitterQuery.TwitterCredentials);
            }

            return queryParameters;
        }
コード例 #44
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>());
        }
コード例 #45
0
        private bool TryPrepareRequest(
            string query, 
            HttpMethod httpMethod, 
            RateLimitTrackerOptions rateLimitTrackerOption,
            ITwitterCredentials credentials,
            out ITwitterQuery twitterQuery)
        {
            twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials ?? _credentialsAccessor.CurrentThreadCredentials);

            var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);
            _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

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

            if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait)
            {
                _rateLimitAwaiter.WaitForCurrentCredentialsRateLimit(query);
            }

            return true;
        }
コード例 #46
0
 public QueryAfterExecuteEventArgs(ITwitterQuery twitterQuery, string jsonResult)
     : base(twitterQuery)
 {
     JsonResult = jsonResult;
     CompletedDateTime = DateTime.Now;
 }
コード例 #47
0
        public IMultipartWebRequest GenerateMultipartWebRequest(ITwitterQuery twitterQuery, string contentId, IEnumerable<IMedia> medias)
        {
            var baseURL = _webHelper.GetBaseURL(twitterQuery.QueryURL);
            var requestConfiguration = new MultipartRequestConfiguration();
            var multipartElements = medias.Select(media => GenerateMultipartElement(media, contentId, requestConfiguration));

            var requestContent = _webRequestGenerator.GenerateMultipartContent(twitterQuery.QueryURL, twitterQuery.HttpMethod, requestConfiguration, multipartElements);

            var baseTwitterQuery = twitterQuery.Clone();
            baseTwitterQuery.QueryURL = baseURL;

            var request = GetQueryWebRequest(baseTwitterQuery);
            request.ContentType = "multipart/form-data;boundary=" + requestConfiguration.Boundary;

            return new MultipartWebRequest(request, requestContent, _tweetinviSettingsAccessor);
        }
コード例 #48
0
 public string ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
 {
     return _webRequestExecutor.ExecuteQuery(twitterQuery, handler);
 }
コード例 #49
0
        private string GetJsonResponseFromReader(StreamReader reader, ITwitterQuery twitterQuery)
        {
            var requestTask = reader.ReadLineAsync();
            var resultingTask = Task.WhenAny(requestTask, Task.Delay(STREAM_DISCONNECTED_DELAY)).Result;

            if (resultingTask != requestTask)
            {
                var twitterQueryParameter = _twitterTimeoutExceptionFactory.GenerateParameterOverrideWrapper("twitterQuery", twitterQuery);
                var twitterTimeoutException = _twitterTimeoutExceptionFactory.Create(twitterQueryParameter);
                throw (Exception)twitterTimeoutException;
            }

            var jsonResponse = requestTask.Result;
            return jsonResponse;
        }
コード例 #50
0
        private void QueryCompleted(ITwitterQuery twitterQuery, string jsonResult, RateLimitTrackerMode rateLimitTrackerMode)
        {
            if (rateLimitTrackerMode != RateLimitTrackerMode.None)
            {
                _rateLimitUpdater.QueryExecuted(twitterQuery.QueryURL, _credentialsAccessor.CurrentThreadCredentials);
            }

            _tweetinviEvents.RaiseAfterQueryExecuted(new QueryAfterExecuteEventArgs(twitterQuery, jsonResult));
        }
コード例 #51
0
        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.Timeout = 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;
        }
コード例 #52
0
ファイル: StreamTask.cs プロジェクト: garrq/tweetinvi
        private async Task<StreamReader> GetStreamReader(HttpClient client, ITwitterQuery twitterQuery)
        {
            try
            {
                var uri = new Uri(twitterQuery.QueryURL);
                var endpoint = uri.GetEndpointURL();
                var queryParameters = uri.Query.Remove(0, 1);
                var httpMethod = new HttpMethod(twitterQuery.HttpMethod.ToString());

                HttpRequestMessage request;

                if (httpMethod == HttpMethod.Post)
                {
                    request = new HttpRequestMessage(httpMethod, endpoint)
                    {
                        Content = new StringContent(queryParameters, Encoding.UTF8, "application/x-www-form-urlencoded")
                    };
                }
                else
                {
                    request = new HttpRequestMessage(httpMethod, twitterQuery.QueryURL);
                }


                var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
                var body = await response.Content.ReadAsStreamAsync();

                return new StreamReader(body, Encoding.GetEncoding("utf-8"));
            }
            catch (WebException wex)
            {
                client.Dispose();
                HandleWebException(wex);
            }
            catch (Exception ex)
            {
                client.Dispose();

                if (ex is AggregateException && ex.InnerException is WebException)
                {
                    HandleWebException(ex.InnerException as WebException);
                }

                _lastException = ex;
                SetStreamState(StreamState.Stop);
            }

            return null;
        }
コード例 #53
0
        private bool TryPrepareRequest(
            string query, 
            HttpMethod httpMethod, 
            RateLimitTrackerOptions rateLimitTrackerOption,
            ITwitterCredentials credentials,
            out ITwitterQuery twitterQuery)
        {
            credentials = credentials ?? _credentialsAccessor.CurrentThreadCredentials;

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

            twitterQuery = _twitterQueryFactory.Create(query, httpMethod, credentials);

            var beforeQueryExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);


            if (rateLimitTrackerOption == RateLimitTrackerOptions.TrackOnly ||
                rateLimitTrackerOption == RateLimitTrackerOptions.TrackAndAwait)
            {
                var timeToWait = _rateLimitAwaiter.TimeToWaitBeforeTwitterRequest(query, twitterQuery.TwitterCredentials);

                twitterQuery.DateWhenCredentialsWillHaveRequiredRateLimits = DateTime.Now.AddMilliseconds(timeToWait);
                _tweetinviEvents.RaiseBeforeQueryExecute(beforeQueryExecuteEventArgs);

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

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

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

            _tweetinviEvents.RaiseBeforeExecuteAfterRateLimitAwait(beforeQueryExecuteEventArgs);

            return true;
        }
コード例 #54
0
ファイル: StreamTask.cs プロジェクト: garrq/tweetinvi
        private string GetJsonResponseFromReader(StreamReader reader, ITwitterQuery twitterQuery)
        {
            var requestTask = reader.ReadLineAsync();
            var resultingTask = TaskEx.WhenAny(requestTask, TaskEx.Delay(STREAM_DISCONNECTED_DELAY)).Result;

            if (resultingTask != requestTask)
            {
                var urlParameter = new ConstructorNamedParameter("url", twitterQuery.QueryURL);
                var twitterTimeoutException = _twitterTimeoutExceptionFactory.Create(urlParameter);
                throw (Exception)twitterTimeoutException;
            }

            var jsonResponse = requestTask.Result;
            return jsonResponse;
        }
コード例 #55
0
 public string ExecuteQuery(ITwitterQuery twitterQuery)
 {
     var webRequest = _twitterRequestGenerator.GetQueryWebRequest(twitterQuery);
     return _webRequestExecutor.ExecuteWebRequest(webRequest);
 }
コード例 #56
0
        private void HandleException(string queryURL, RateLimitTrackerMode rateLimitTrackerMode, int statusCode, ITwitterQuery queryParameter)
        {
            if (rateLimitTrackerMode != RateLimitTrackerMode.None && statusCode == TweetinviConsts.STATUS_CODE_TOO_MANY_REQUEST)
            {
                _rateLimitUpdater.ClearRateLimitsForQuery(queryURL);
            }

            _tweetinviEvents.RaiseAfterQueryExecuted(new QueryAfterExecuteEventArgs(queryParameter, null));
        }
コード例 #57
0
 public QueryBeforeExecuteEventArgs(ITwitterQuery twitterQuery) : base(twitterQuery)
 {
     BeforeExecutingDateTime = DateTime.Now;
 }
コード例 #58
0
ファイル: StreamTask.cs プロジェクト: garrq/tweetinvi
        private HttpClient GetHttpClient(ITwitterQuery twitterQuery)
        {
            if (twitterQuery == null)
            {
                SetStreamState(StreamState.Stop);
                return null;
            }

            twitterQuery.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

            var queryBeforeExecuteEventArgs = new QueryBeforeExecuteEventArgs(twitterQuery);
            _tweetinviEvents.RaiseBeforeQueryExecute(queryBeforeExecuteEventArgs);

            if (queryBeforeExecuteEventArgs.Cancel)
            {
                SetStreamState(StreamState.Stop);
                return null;
            }

            return _httpClientWebHelper.GetHttpClient(twitterQuery);
        }
コード例 #59
0
 public QueryExecutionEventArgs(ITwitterQuery twitterQuery)
 {
     _twitterQuery = twitterQuery;
 }
コード例 #60
0
ファイル: StreamTask.cs プロジェクト: garrq/tweetinvi
        public void Start()
        {
            if (StreamState == StreamState.Stop && !_isNew)
            {
                return;
            }

            this.Raise(StreamStarted);
            SetStreamState(StreamState.Resume);

            _twitterQuery = _generateTwitterQuery();

            if (_twitterQuery.TwitterCredentials == null)
            {
                throw new TwitterNullCredentialsException();
            }

            if (!_twitterQuery.TwitterCredentials.AreSetupForUserAuthentication())
            {
                throw new TwitterInvalidCredentialsException(_twitterQuery.TwitterCredentials);
            }

            _currentHttpClient = GetHttpClient(_twitterQuery);
            _currentStreamReader = GetStreamReader(_currentHttpClient, _twitterQuery).Result;

            int numberOfRepeatedFailures = 0;

            while (StreamState != StreamState.Stop)
            {
                if (StreamState == StreamState.Pause)
                {
                    using (EventWaitHandle tmpEvent = new ManualResetEvent(false))
                    {
                        tmpEvent.WaitOne(TimeSpan.FromMilliseconds(STREAM_RESUME_DELAY));
                    }

                    continue;
                }

                try
                {
                    var json = GetJsonResponseFromReader(_currentStreamReader, _twitterQuery);

                    var isJsonResponseValid = json.IsMatchingJsonFormat();
                    if (!isJsonResponseValid)
                    {
                        if (json == string.Empty)
                        {
                            continue;
                        }

                        if (json != null)
                        {
                            throw new WebException(json);
                        }

                        if (TryHandleInvalidResponse(numberOfRepeatedFailures))
                        {
                            ++numberOfRepeatedFailures;
                            continue;
                        }

                        throw new WebException("Stream cannot be read.");
                    }

                    numberOfRepeatedFailures = 0;

                    if (StreamState == StreamState.Resume && !_processObject(json))
                    {
                        SetStreamState(StreamState.Stop);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    if (!ShouldContinueAfterHandlingException(ex))
                    {
                        SetStreamState(StreamState.Stop);
                        break;
                    }
                }
            }

            if (_currentStreamReader != null)
            {
                _currentStreamReader.Dispose();
            }

            if (_currentHttpClient != null)
            {
                _currentHttpClient.Dispose();
            }
        }