Exemplo n.º 1
0
        private async Task <IRestfulResponse> Execute(IRestfulRequest restfulRequest, Func <IRestfulRequest, HttpRequestMessage> getHttpRequestMessage)
        {
            IRestfulResponse restfulResponse = new DefaultRestfulResponse {
                Request = restfulRequest
            };

            try
            {
                HttpClient         httpClient         = this.CreateHttpClient(restfulRequest);
                HttpRequestMessage httpRequestMessage = getHttpRequestMessage(restfulRequest);
                var cancellTokenSource = new CancellationTokenSource(restfulRequest.Timeout);
                HttpResponseMessage httpResponseMessage =
                    await httpClient.SendAsync(httpRequestMessage, cancellTokenSource.Token);

                httpResponseMessage.EnsureSuccessStatusCode();
                restfulResponse.IsSuccessful = httpResponseMessage.IsSuccessStatusCode;
                restfulResponse.StatusCode   = httpResponseMessage.StatusCode;
                httpResponseMessage.Headers.ForEach(h => restfulResponse.Headers.Add(new KeyValuePair <string, string>(h.Key, h.Value.ToString())));
                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    HttpContent content = httpResponseMessage.Content;
                    restfulResponse.RawBytes = await content.ReadAsByteArrayAsync();

                    restfulResponse.Content = await content.ReadAsStringAsync();
                }

                return(restfulResponse);
            }
            catch (WebException webException)
            {
                restfulResponse.ErrorMessage      = webException.Message;
                restfulResponse.ErrorException    = webException;
                restfulResponse.StatusDescription = webException.Status.ToString();
                return(restfulResponse);
            }
            catch (Exception e)
            {
                restfulResponse.ErrorMessage      = e.Message;
                restfulResponse.ErrorException    = e;
                restfulResponse.StatusDescription = $"Send request to {restfulRequest.Method}: {restfulRequest.Url} failed";
                return(restfulResponse);
            }
        }
Exemplo n.º 2
0
        private async Task <IRestfulResponse <TResponse> > DeserializeResponse <TResponse>(Task <IRestfulResponse> asyncResponse)
        {
            IRestfulResponse             response        = await asyncResponse;
            IRestfulResponse <TResponse> restfulResponse = new DefaultRestfulResponse <TResponse>(response);

            try
            {
                if (restfulResponse.IsSuccessful && !string.IsNullOrWhiteSpace(restfulResponse.Content))
                {
                    restfulResponse.ResponseBody = this._serializer.DeserializeObject <TResponse>(response.Content);
                }

                return(restfulResponse);
            }
            catch (Exception e)
            {
                restfulResponse.ErrorMessage      = e.Message;
                restfulResponse.ErrorException    = e;
                restfulResponse.StatusDescription = "Deserialize response failed";
                return(restfulResponse);
            }
        }