private async Task <TEntity> ReadResponseAsync <TEntity>(HttpResponseMessage responseMessage, bool checkResponseCode = false) where TEntity : class { var content = await responseMessage.Content.ReadAsStringAsync(); var globallyPaidResponse = new HttpServiceClientResponse(responseMessage.StatusCode, responseMessage.Headers, content); if (globallyPaidResponse.StatusCode == HttpStatusCode.OK) { if (checkResponseCode) { CheckResponseCode(content, globallyPaidResponse); } try { return(JsonConvert.DeserializeObject <TEntity>(content)); } catch (JsonException jex) { System.Diagnostics.Debug.Write($"Invalid response object {typeof(TEntity)} from API: \"{content}\", message: \"{jex.Message}\""); throw new GloballyPaidException(responseMessage.StatusCode, $"Invalid response object from API: \"{content}\", message: \"{jex.Message}\"", globallyPaidResponse); } } else { throw new GloballyPaidException(responseMessage.StatusCode, content, globallyPaidResponse); } }
private void CheckResponseCode(string content, HttpServiceClientResponse globallyPaidResponse) { var response = new { response_code = string.Empty, message = string.Empty }; try { response = JsonConvert.DeserializeAnonymousType(content, new { response_code = string.Empty, message = string.Empty }); } catch (JsonException jex) { throw new GloballyPaidException(globallyPaidResponse.StatusCode, $"Invalid response object from API: \"{content}\", message: \"{jex.Message}\"", globallyPaidResponse); } if (response != null && !string.IsNullOrEmpty(response.response_code) && !string.IsNullOrEmpty(response.message) && response.response_code != "00") { throw new GloballyPaidException(HttpStatusCode.BadRequest, response.message, globallyPaidResponse); } }
public GloballyPaidException(HttpStatusCode httpStatusCode, string errorMessage, HttpServiceClientResponse globallyPaidResponse) { HttpStatusCode = httpStatusCode; ErrorMessage = !string.IsNullOrWhiteSpace(errorMessage) ? errorMessage : GetErrorMessageFromStatusCode(httpStatusCode); GloballyPaidResponse = globallyPaidResponse; }