예제 #1
0
        /// <summary>
        /// Sends the request asynchronously.
        /// </summary>
        /// <typeparam name="TRequest">The type of the request.</typeparam>
        /// <typeparam name="TResponse">The type of the response.</typeparam>
        /// <param name="httpMethod">The HTTP method.</param>
        /// <param name="requestUrl">The request URL.</param>
        /// <param name="requestBody">The request body.</param>
        /// <returns>The response.</returns>
        /// <exception cref="ClientException">The client exception.</exception>
        private async Task <TResponse> SendRequestAsync <TRequest, TResponse>(HttpMethod httpMethod, string requestUrl, TRequest requestBody = default(TRequest))
        {
            var request = new HttpRequestMessage(httpMethod, requestUrl);

            if (requestBody != null)
            {
                request.Content = new StringContent(JsonConvert.SerializeObject(requestBody, settings), Encoding.UTF8, JsonContentTypeHeader);
            }

            HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                string responseContent = null;
                if (response.Content != null)
                {
                    responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                }

                if (!string.IsNullOrWhiteSpace(responseContent))
                {
                    return(JsonConvert.DeserializeObject <TResponse>(responseContent, settings));
                }

                return(default(TResponse));
            }
            else
            {
                if (response.Content != null && response.Content.Headers.ContentType.MediaType.Contains(JsonContentTypeHeader))
                {
                    var errorObjectString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString);
                    if (errorCollection != null)
                    {
                        throw new ClientException(errorCollection, response.StatusCode);
                    }
                }

                response.EnsureSuccessStatusCode();
            }

            return(default(TResponse));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientException"/> class.
 /// </summary>
 /// <param name="error">The error entity.</param>
 /// <param name="httpStatus">The http status.</param>
 public ClientException(ClientError error, HttpStatusCode httpStatus)
 {
     this.Error = error;
     this.HttpStatus = httpStatus;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientException"/> class.
 /// </summary>
 /// <param name="error">The error entity.</param>
 /// <param name="httpStatus">The http status.</param>
 public ClientException(ClientError error, HttpStatusCode httpStatus)
 {
     this.Error      = error;
     this.HttpStatus = httpStatus;
 }