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); }
public QueryAfterExecuteExceptionEventArgs( ITwitterQuery twitterQuery, TwitterException exception) : base(twitterQuery, null, null) { Exception = exception; }
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)); }
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."); } } }
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)); } } }
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); }
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); } } }
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); }
public FrontPageService(IFeaturedArticlesQuery articlesQuery, ITwitterQuery twitterQuery, IOptions <DeploymentSlot> deploymentSlot) { this.articlesQuery = articlesQuery; this.twitterQuery = twitterQuery; this.deploymentSlot = deploymentSlot.Value; }
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; }
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; }
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; }
public TwitterException(ITwitterExceptionInfo[] exceptionInfos, ITwitterQuery twitterQuery) : this(twitterQuery) { CreationDate = DateTime.Now; TwitterExceptionInfos = exceptionInfos; TwitterQuery = twitterQuery; }
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)); }
public TwitterException GenerateTwitterException( WebException webException, ITwitterQuery twitterQuery, int statusCode) { return(_twitterExceptionFactory.Create(webException, twitterQuery, statusCode)); }
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); }
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; } })); }
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); }
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); }
protected TwitterException(ITwitterQuery twitterQuery, string message) : base(message) { CreationDate = DateTime.Now; URL = twitterQuery?.QueryURL; TwitterQuery = twitterQuery; }
// 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; } })); }
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); }
public HttpWebRequest GetQueryWebRequest(ITwitterQuery twitterQuery) { var url = twitterQuery.QueryURL; var httpMethod = twitterQuery.HttpMethod; var queryParameters = GetCredentialsQueryParameters(twitterQuery); return GetQueryWebRequestInternal(url, httpMethod, queryParameters); }
public HttpWebRequest GetQueryWebRequest(ITwitterQuery twitterQuery) { var url = twitterQuery.QueryURL; var httpMethod = twitterQuery.HttpMethod; var queryParameters = GetCredentialsQueryParameters(twitterQuery); return(GetQueryWebRequestInternal(url, httpMethod, queryParameters)); }
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)); }
public TwitterException AddWebException(WebException webException, ITwitterQuery twitterQuery) { var twitterException = GenerateTwitterException(webException, twitterQuery); AddTwitterException(twitterException); // Cannot throw from an interface :( return(twitterException); }
public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable <byte[]> binaries) { if (binaries.IsNullOrEmpty()) { return(ExecuteQuery(twitterQuery)); } return(_webRequestExecutor.ExecuteMultipartQuery(twitterQuery, contentId, binaries)); }
public string ExecuteMultipartQuery(ITwitterQuery twitterQuery, string contentId, IEnumerable<byte[]> binaries) { if (binaries.IsNullOrEmpty()) { return ExecuteQuery(twitterQuery); } return _webRequestExecutor.ExecuteMultipartQuery(twitterQuery, contentId, binaries); }
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)); }
public TwitterException( IWebExceptionInfoExtractor webExceptionInfoExtractor, IWebRequestResult webRequestResult, ITwitterQuery twitterQuery) : this(twitterQuery) { StatusCode = webRequestResult.StatusCode; TwitterExceptionInfos = webExceptionInfoExtractor.GetTwitterExceptionInfosFromStream(webRequestResult.ResultStream); TwitterDescription = webExceptionInfoExtractor.GetStatusCodeDescription(StatusCode); }
public AfterExecutingQueryEventArgs( ITwitterQuery twitterQuery, string httpContent, Dictionary <string, IEnumerable <string> > httpHeaders) : base(twitterQuery) { HttpContent = httpContent; HttpHeaders = httpHeaders; CompletedDateTime = DateTime.Now; }
public QueryAfterExecuteEventArgs( ITwitterQuery twitterQuery, string httpContent, Dictionary<string, IEnumerable<string>> httpHeaders) : base(twitterQuery) { HttpContent = httpContent; HttpHeaders = httpHeaders; CompletedDateTime = DateTime.Now; }
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)); }
public TwitterException TryLogWebException(WebException webException, ITwitterQuery twitterQuery) { var twitterException = GenerateTwitterException(webException, twitterQuery); if (LogExceptions) { AddTwitterException(twitterException); } return(twitterException); }
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; }
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); }
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; }
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); }
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); }
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); }
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); }
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); }
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; }
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>()); }
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; }
public QueryAfterExecuteEventArgs(ITwitterQuery twitterQuery, string jsonResult) : base(twitterQuery) { JsonResult = jsonResult; CompletedDateTime = DateTime.Now; }
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); }
public string ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null) { return _webRequestExecutor.ExecuteQuery(twitterQuery, handler); }
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; }
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; }
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; }
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; }
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; }
public string ExecuteQuery(ITwitterQuery twitterQuery) { var webRequest = _twitterRequestGenerator.GetQueryWebRequest(twitterQuery); return _webRequestExecutor.ExecuteWebRequest(webRequest); }
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)); }
public QueryBeforeExecuteEventArgs(ITwitterQuery twitterQuery) : base(twitterQuery) { BeforeExecutingDateTime = DateTime.Now; }
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); }
public QueryExecutionEventArgs(ITwitterQuery twitterQuery) { _twitterQuery = twitterQuery; }
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(); } }