public virtual async Task <IHttpCallResultCGHT <T> > MakeWebApiFromBodyCall <T>( Enums.HttpVerb httpVerb, ILoggingService log, HttpClient client, string requestUri, T item) where T : class { T retValData = default(T); System.Net.Http.HttpResponseMessage response = null; HttpCallResult <T> retVal = new HttpCallResult <T>(); try { string serializedItem = JsonConvert.SerializeObject(item); var inputMessage = new System.Net.Http.HttpRequestMessage { Content = new System.Net.Http.StringContent(serializedItem, System.Text.Encoding.UTF8, "application/json") }; inputMessage.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); if (httpVerb == Enums.HttpVerb.Post) { response = await client.PostAsync(requestUri, inputMessage.Content); } else if (httpVerb == Enums.HttpVerb.Put) { response = await client.PutAsync(requestUri, inputMessage.Content); } else { throw new NotImplementedException(); } if (response.IsSuccessStatusCode) { var responseContent = response.Content.ReadAsStringAsync(); retValData = JsonConvert.DeserializeObject <T>(responseContent.Result); } else { log.Warn($"Failure during WebApiClient to Web API {Enum.GetName(typeof(Enums.HttpVerb), httpVerb)} operation with {retValData?.GetType().Name}.", LogMessageType.Instance.Warn_WebApiClient, httpResponseStatusCode: (int)response.StatusCode, url: requestUri); } retVal = new HttpCallResult <T>( retValData, requestUri, response.IsSuccessStatusCode, response.StatusCode, response.ReasonPhrase); } catch (Exception ex) { log.Error(message: $"HttpClient {nameof(MakeWebApiFromBodyCall)} call resulted in error - status code: {response?.StatusCode} " + $"reason: {response?.ReasonPhrase}.", logMessageType: LogMessageType.Instance.Exception_WebApiClient, ex: ex, httpResponseStatusCode: (int)response?.StatusCode, url: requestUri); retVal = new HttpCallResult <T>(data: null, requestUri: requestUri, isSuccessStatusCode: response != null ? response.IsSuccessStatusCode : false, statusCode: response != null ? response.StatusCode : System.Net.HttpStatusCode.InternalServerError, reasonPhrase: response != null ? response.ReasonPhrase : ex.Message, exception: ex); } return(retVal); }
public virtual async Task <IHttpCallResultCGHT <T> > MakeWebApiBSONCall <T>( Enums.HttpVerb httpVerb, ILogger log, HttpClient client, string requestUri, T item) where T : class { T retValData = default(T); HttpCallResult <T> retVal = new HttpCallResult <T>(); try { HttpResponseMessage response = null; if (httpVerb == Enums.HttpVerb.Post) { response = await PostBsonAsync <T>(client, requestUri, item); } else { throw new NotImplementedException(); } if (response.IsSuccessStatusCode) { using (BsonDataReader reader = new BsonDataReader(await response.Content.ReadAsStreamAsync())) { JsonSerializer serializer = new JsonSerializer(); retValData = serializer.Deserialize <T>(reader); } } else { log.LogWarning(eventId: (int)coreEnums.EventId.Warn_WebApiClient, message: "Failure during WebApiClient to Web API {HttpVerb} operation with {ReturnTypeName}. HttpResponseStatusCode: {HttpResponseStatusCode} from RequestUri {RequestUri}", Enum.GetName(typeof(Enums.HttpVerb), httpVerb), retVal?.GetType().Name, (int)response.StatusCode, requestUri); } retVal = new HttpCallResult <T>( retValData, requestUri, response.IsSuccessStatusCode, response.StatusCode, response.ReasonPhrase); } catch (Exception ex) { retVal.Exception = ex; } return(retVal); }