Пример #1
0
        // Simple Query
        public Task <ITwitterResponse> ExecuteQueryAsync(ITwitterRequest request, ITwitterClientHandler handler = null)
        {
            return(ExecuteTwitterQuerySafelyAsync(request, async() =>
            {
                HttpResponseMessage httpResponseMessage = null;

                try
                {
                    httpResponseMessage = await _httpClientWebHelper.GetHttpResponseAsync(request.Query, handler).ConfigureAwait(false);

                    var result = CreateTwitterResponseFromHttpResponse(request.Query.Url, httpResponseMessage);
                    if (!result.IsSuccessStatusCode)
                    {
                        throw _twitterExceptionFactory.Create(result, request);
                    }

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

                    return result;
                }
                catch (Exception)
                {
                    httpResponseMessage?.Dispose();
                    throw;
                }
            }));
        }
Пример #2
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;
        }
Пример #3
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);
        }
Пример #4
0
        public HttpClient GetHttpClient(ITwitterQuery twitterQuery, ITwitterClientHandler twitterHandler = null)
        {
            var oAuthWebRequestGenerator = _oAuthWebRequestGeneratorFactory.Create();
            var handler = (twitterHandler as TwitterClientHandler) ?? new TwitterClientHandler(oAuthWebRequestGenerator);

            handler.TwitterQuery = twitterQuery;

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

            return(client);
        }
Пример #5
0
        // Simple Query
        public IWebRequestResult ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
        {
            return(ExecuteTwitterQuerySafely(twitterQuery, () =>
            {
                HttpResponseMessage httpResponseMessage = null;

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

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

                    if (!result.IsSuccessStatusCode)
                    {
                        throw _exceptionHandler.TryLogFailedWebRequestResult(result, twitterQuery);
                    }

                    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;
                }
            }));
        }
Пример #6
0
        // Simple Query
        public string ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
        {
            return(ExecuteTwitterQuerySafely(twitterQuery, () =>
            {
                HttpResponseMessage httpResponseMessage = null;

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

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

                    if (!result.IsSuccessStatusCode)
                    {
                        throw _exceptionHandler.TryLogFailedWebRequestResult(result);
                    }

                    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;
                }
            }));
        }
Пример #7
0
        public async Task<HttpResponseMessage> GetHttpResponse(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.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, twitterQuery.HttpContent).ConfigureAwait(false);
                }
            }
        }
Пример #8
0
 public string ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
 {
     return(_webRequestExecutor.ExecuteQuery(twitterQuery, handler));
 }
 public string ExecuteQuery(ITwitterQuery twitterQuery, ITwitterClientHandler handler = null)
 {
     return _webRequestExecutor.ExecuteQuery(twitterQuery, handler);
 }
Пример #10
0
        public async Task <HttpResponseMessage> GetHttpResponse(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)
                    {
                        throw new ArgumentException("Cannot send HttpContent in a WebRequest that is not POST.");
                    }

                    return(await client.PostAsync(twitterQuery.Url, twitterQuery.HttpContent).ConfigureAwait(false));
                }
            }
        }