private async Task <IRestResponse <T> > ExecuteAsync <T>(IRestRequest request) where T : class { HttpResponseMessage response; using (var client = new HttpClient()) { var webRequest = (WebApiRestRequest)request; switch (request.Method) { case RestMethod.Get: response = await client.GetAsync(GetFullResource(webRequest)); break; case RestMethod.Put: response = await client.PutAsync(GetFullResource(webRequest), GetContent(webRequest)); break; case RestMethod.Post: response = await client.PostAsync(GetFullResource(webRequest), GetContent(webRequest)); break; case RestMethod.Delete: response = await client.DeleteAsync(GetFullResource(webRequest)); break; default: throw new ArgumentOutOfRangeException(); } } if (!response.IsSuccessStatusCode) { throw new HttpException("Received a failure from Trello.\n" + $"Status Code: {response.StatusCode} ({(int)response.StatusCode})\n" + $"Content: {response.Content.ReadAsStringAsync().Result}"); } var restResponse = new WebApiRestResponse <T>(); if (response.Content != null) { restResponse.Content = await response.Content.ReadAsStringAsync(); TrelloConfiguration.Log.Debug(restResponse.Content); restResponse.Data = await response.Content.ReadAsAsync <T>(new MediaTypeFormatter[] { _formatter, _errorLogger }); } return(restResponse); }
private async Task <IRestResponse> ExecuteAsync(IRestRequest request) { HttpResponseMessage response; using (var client = new HttpClient()) { var webRequest = (WebApiRestRequest)request; switch (request.Method) { case RestMethod.Get: response = await client.GetAsync(GetFullResource(webRequest)); break; case RestMethod.Put: response = await client.PutAsync(GetFullResource(webRequest), GetContent(webRequest)); break; case RestMethod.Post: response = await client.PostAsync(GetFullResource(webRequest), GetContent(webRequest)); break; case RestMethod.Delete: response = await client.DeleteAsync(GetFullResource(webRequest)); break; default: throw new ArgumentOutOfRangeException(); } } var restResponse = new WebApiRestResponse(); if (response.Content != null) { restResponse.Content = await response.Content.ReadAsStringAsync(); //TrelloConfiguration.Log.Debug(restResponse.Content); } return(restResponse); }
private async Task <IRestResponse <T> > MapResponse <T>(HttpResponseMessage response) where T : class { var restResponse = new WebApiRestResponse <T> { Content = await response.Content.ReadAsStringAsync(), StatusCode = response.StatusCode }; TrelloConfiguration.Log.Debug($"Status Code: {response.StatusCode} ({(int) response.StatusCode})\n" + $"Content: {restResponse.Content}"); try { restResponse.Data = await response.Content.ReadAsAsync <T>(new MediaTypeFormatter[] { _formatter, _errorLogger }); } catch (Exception e) { restResponse.Exception = e; } return(restResponse); }