private static async Task <HttpFragileOperation> ConvertHttpResponseMessageToHttpResult(HttpResponseMessage httpResponseMessage) { var stringResult = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); var statusCode = httpResponseMessage.StatusCode; if (httpResponseMessage.IsSuccessStatusCode) { return(HttpFragileOperation.CreateSuccessfulResult(statusCode, stringResult)); } if ((int)statusCode >= 500) { return(HttpFragileOperation.CreateErrorResult(statusCode, stringResult, $"500+ error result. StatusCode: {statusCode}. Response: {stringResult}")); } return(HttpFragileOperation.CreateFailedResult(statusCode, stringResult, $"Non-500+ error result. StatusCode: {statusCode}. Response: {stringResult}")); }
private static async Task <HttpFragileOperation> HttpClientExceptionHandlerAsync(Func <HttpClient, Task <HttpFragileOperation> > function, IEnumerable <KeyValuePair <string, string> > headers = null) { using (var client = new HttpClient()) { if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } try { return(await function(client).ConfigureAwait(false)); } catch (Exception e) { return(HttpFragileOperation.CreateErrorResult(HttpStatusCode.InternalServerError, "", $"An exception occured while making the request. Exception: {e}")); } } }