Exemplo n.º 1
0
        private static HttpRequestMessage CreateRequestMessage(EUncommonRequestMethod restMethod, string requestUri, HttpContent content, UncommonRequestOptions options)
        {
            var method  = new HttpMethod(restMethod.ToString("F"));
            var request = new HttpRequestMessage(method, requestUri)
            {
                Content = content
            };

            SetHttpAcceptHeader(request, options.ResponseSerializer);
            if (options.Headers != null)
            {
                foreach (var header in options.Headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            if (options.Authorized && options.SecurityContext != null)
            {
                request.Headers.Add("Authorization", options.SecurityContext.GenerateAuthorizationHeader());
            }

            return(request);
        }
Exemplo n.º 2
0
        private static async Task <UncommonResponse <TResponseType> > ProcessRequest <TRequestType, TResponseType>(EUncommonRequestMethod method, string requestUri, TRequestType requestContent, UncommonRequestOptions options)
        {
            HttpResponseMessage response = null;

            try
            {
                options = SetRestRequestOptions(options);

                using (var client = new UncommonHttpClient())
                {
                    client.Timeout = new TimeSpan(0, 0, 0, 0, options.Timeout);

                    HttpContent httpContent = null;
                    if (typeof(TRequestType) != typeof(NoRequestContent))
                    {
                        httpContent = await GenerateRequestContent(requestContent, options).ConfigureAwait(false);
                    }

                    var request = CreateRequestMessage(method, requestUri, httpContent, options);
                    response = await client.SendAsync(request, CancellationToken.None).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        return(await ProcessReponseContent <TResponseType>(response, options).ConfigureAwait(false));
                    }

                    var requestException = new UncommonRequestException
                    {
                        Information            = "RequestException",
                        RequestExceptionStatus = EUncommonRequestExceptionStatus.ServiceError,
                        StatusCode             = response.StatusCode,
                    };

                    var resultAsString = response.Content.ReadAsStringAsync().Result;
                    requestException.ExceptionResponseAsString = resultAsString;

                    throw requestException;
                }
            }
            catch (UncommonRequestException)
            {
                throw;
            }
            catch (JsonSerializationException ex)
            {
                throw new UncommonRequestException
                      {
                          Information            = "JsonSerializationException",
                          InnerException         = ex,
                          RequestExceptionStatus = EUncommonRequestExceptionStatus.SerializationError,
                          StatusCode             = HttpStatusCode.OK
                      };
            }
            catch (HttpRequestException ex)
            {
                var requestException = new UncommonRequestException
                {
                    Information            = "HttpRequestException",
                    InnerException         = ex.InnerException ?? ex,
                    RequestExceptionStatus = EUncommonRequestExceptionStatus.ServiceError,
                    StatusCode             = response != null ? response.StatusCode : HttpStatusCode.BadRequest
                };

                throw requestException;
            }
            catch (TaskCanceledException ex)
            {
                // most likely a timeout
                throw new UncommonRequestException
                      {
                          Information            = "TaskCanceledException",
                          InnerException         = ex,
                          RequestExceptionStatus = EUncommonRequestExceptionStatus.Timeout
                      };
            }
            catch (Exception ex)
            {
                throw new UncommonRequestException
                      {
                          Information            = ex.Message,
                          InnerException         = ex,
                          RequestExceptionStatus = EUncommonRequestExceptionStatus.Undefined,
                          StatusCode             = HttpStatusCode.NotFound
                      };
            }
        }