protected ServiceResponse(HttpStatusCode statusCode, bool success, string errorMessage, Exception ex) { StatusCode = statusCode; RequestSuccess = success; ErrorMessage = errorMessage; Exception = (ServiceResponseException)ex; if (!success || (int)statusCode >= 500 || statusCode == HttpStatusCode.BadRequest || (int)statusCode == 0 || HasException) { HasError = true; } }
public async Task <ServiceResponse <T> > ExecuteRequest <T>(string baseUrl, string path, Method method, object body = null, List <RequestParameter> parameters = null, List <RequestHeader> headers = null, AsyncPolicyWrap <IRestResponse <T> > policy = null) where T : new() { var client = new RestClient(baseUrl) { Timeout = RequestTimeout * 1000 }; var request = ConfigureRequest(path, method, body, parameters, headers); var fullUrl = client.BuildUri(request); IRestResponse <T> response = null; if (policy == null) { response = await client.ExecuteTaskAsync <T>(request); } else { Context context = new Context().WithLogger(_logger); response = await policy.ExecuteAsync(ctx => client.ExecuteTaskAsync <T>(request), context); } //var cb = policy.GetPolicies(); string responseErr = null; ServiceResponseException responseEx = null; HttpStatusCode responseCode = response.StatusCode; if (!response.IsSuccessful) { response.Data = default; responseErr = !string.IsNullOrWhiteSpace(response.ErrorMessage) ? response.ErrorMessage : response.Content; if (response.ResponseStatus == ResponseStatus.TimedOut) { responseCode = HttpStatusCode.GatewayTimeout; } LogFailedRequest(response.ErrorException, responseCode, responseErr, fullUrl.ToString()); if (!string.IsNullOrWhiteSpace(responseErr) || response.ErrorException != null) { responseEx = new ServiceResponseException(response.StatusCode, responseErr, response.ErrorException); } } return(new ServiceResponse <T>(response.Data, response, responseCode, response.IsSuccessful, responseErr, responseEx)); }