Exemplo n.º 1
0
 private void AddHeaders(HttpRequestMessage httpRequest, ApiHeaders optionalHeaders)
 {
     if (optionalHeaders != null)
     {
         foreach (var header in optionalHeaders)
         {
             try
             {
                 httpRequest.Headers.Add(header.Key, header.Value);
             }
             catch (Exception exception)
             {
                 // TODO: Log this Exception.
                 httpRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
             }
         }
     }
 }
Exemplo n.º 2
0
        public async Task <IApiClientResponse <TResponseDto> > PostAsync <TRequestDto, TResponseDto>(string relativeUrl, TRequestDto requestDto, ApiHeaders optionalHeaders)
        {
            var response = new ApiClientResponse <TResponseDto>()
            {
            };

            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (var httpContent = CreateContent(requestDto))
                    {
                        using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, UrlCombine(RestApiBaseUrl, relativeUrl)))
                        {
                            httpRequest.Content = httpContent;

                            AddHeaders(httpRequest, optionalHeaders);

                            using (var httpResponse = await httpClient.SendAsync(httpRequest))
                            {
                                response.HttpStatusCode      = (int)httpResponse.StatusCode;
                                response.HttpResponseHeaders = new ApiHeaders(httpResponse.Headers);

                                response.ResponseText = await httpResponse.Content.ReadAsStringAsync();

                                if (typeof(TResponseDto) != typeof(string))
                                {
                                    response.ResponseDto = await httpResponse.Content.ReadAsAsync <TResponseDto>();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                response.HttpStatusCode = (int)HttpStatusCode.InternalServerError;
            }

            return(response);
        }