Exemplo n.º 1
0
        private static UncommonRequestOptions SetRestRequestOptions(UncommonRequestOptions options)
        {
            options = options ?? new UncommonRequestOptions();

            if (SecurityContext != null && options.SecurityContext == null)
            {
                options.SecurityContext = SecurityContext;
            }

            return(options);
        }
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
                      };
            }
        }
Exemplo n.º 3
0
 public static async Task <UncommonResponse <TResponseType> > ProcessDeleteRequestAsync <TRequestType, TResponseType>(string requestUri, TRequestType requestContent, UncommonRequestOptions options = null)
 {
     return(await ProcessRequest <TRequestType, TResponseType>(EUncommonRequestMethod.DELETE, requestUri, requestContent, options).ConfigureAwait(false));
 }
Exemplo n.º 4
0
 public static async Task <UncommonResponse <NoResponseContent> > ProcessDeleteRequestAsync(string requestUri, UncommonRequestOptions options = null)
 {
     return(await ProcessRequest <NoRequestContent, NoResponseContent>(EUncommonRequestMethod.DELETE, requestUri, null, options).ConfigureAwait(false));
 }
Exemplo n.º 5
0
 public static async Task <UncommonResponse <NoResponseContent> > ProcessPatchRequestAsync <TRequestType>(string requestUri, TRequestType requestContent, UncommonRequestOptions options = null)
 {
     return(await ProcessRequest <TRequestType, NoResponseContent>(EUncommonRequestMethod.PATCH, requestUri, requestContent, options).ConfigureAwait(false));
 }
Exemplo n.º 6
0
        public static async Task <UncommonResponse <byte[]> > ProcessRawGetRequestAsync(string requestUri, UncommonRequestOptions options = null)
        {
            // Make sure the options are set and set the responseSerializer to use ByteArray
            options = SetRestRequestOptions(options);

            options.ResponseSerializer = EUncommonResponseSerializer.UseByteArray;

            var result = await ProcessRequest <NoRequestContent, byte[]>(EUncommonRequestMethod.GET, requestUri, null, options).ConfigureAwait(false);

            result.Result             = result.RawResponseContent;
            result.RawResponseContent = null;

            return(result);
        }
Exemplo n.º 7
0
 public static async Task <UncommonResponse <TResponseType> > ProcessGetRequestAsync <TResponseType>(string requestUri, UncommonRequestOptions options = null)
 {
     return(await ProcessRequest <NoRequestContent, TResponseType>(EUncommonRequestMethod.GET, requestUri, null, options).ConfigureAwait(false));
 }
Exemplo n.º 8
0
        private static async Task <UncommonResponse <TResponseType> > ProcessReponseContent <TResponseType>(HttpResponseMessage response, UncommonRequestOptions options)
        {
            var restResponse = new UncommonResponse <TResponseType>
            {
                StatusCode = response.StatusCode
            };

            if (typeof(TResponseType) != typeof(NoResponseContent))
            {
                switch (options.ResponseSerializer)
                {
                case EUncommonResponseSerializer.UseXmlSerializer:
                {
                    var resultAsStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    restResponse.Result = ConvertResponseToModelObjectFromXml <TResponseType>(resultAsStream);
                }
                break;

                case EUncommonResponseSerializer.UseJsonNet:
                {
                    var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    restResponse.Result = JsonConvert.DeserializeObject <TResponseType>(resultAsString, JsonSerializerSettings);
                }
                break;

                case EUncommonResponseSerializer.UseByteArray:
                {
                    var resultAsBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                    restResponse.RawResponseContent = resultAsBytes;
                }
                break;

                default:
                    // Wrong ResponseSerializer settings are used: response is not set.
                    // Possibly set an error ;)
                    break;
                }
            }

            return(restResponse);
        }
Exemplo n.º 9
0
        private static async Task <HttpContent> GenerateRequestContent <TRequestType>(TRequestType requestContent, UncommonRequestOptions options)
        {
            HttpContent httpContent;

            switch (options.RequestSerializer)
            {
            case EUncommonRequestSerializer.UseXmlSerializer:
            {
                var requestBody = await ConvertModelObjectByXmlToString(requestContent).ConfigureAwait(false);

                httpContent = new StringContent(requestBody);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
            }
            break;

            case EUncommonRequestSerializer.UseByteArray:
            {
                httpContent = new ByteArrayContent(requestContent as byte[]);
            }
            break;

            case EUncommonRequestSerializer.UseJsonNet:
            {
                var requestBody = JsonConvert.SerializeObject(requestContent, JsonSerializerSettings);

                httpContent = new StringContent(requestBody);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
            break;

            case EUncommonRequestSerializer.UseStringUrlPost:
            {
                httpContent = new StringContent(requestContent.ToString());
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            }
            break;

            case EUncommonRequestSerializer.UseFormUrlEncoded:
            {
                var pairs = requestContent as IEnumerable <KeyValuePair <string, string> >;

                var content = pairs ?? requestContent.GetProperties().AsEnumerable();

                httpContent = new FormUrlEncodedContent(content);
            }
            break;

            default:
                // Return value null indicates that wrong RequestSerializer settings are used.
                throw new NotSupportedException();
            }

            return(httpContent);
        }
Exemplo n.º 10
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);
        }