protected virtual IResponseConcern Execute(IRestRequest request) { IRestResponse response = default(IRestResponse); IResponseConcern apiResponse = new BaseResponseConcern(); try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; BeforeRequest(restClient, request); Stopwatch watcher = Stopwatch.StartNew(); response = restClient.Execute(request); watcher.Stop(); Logger.Log($"Time Taken For api:{restClient.BaseUrl}{request.Resource} Time: {watcher.Elapsed.TotalSeconds} sec"); AfterResponse(restClient, response); LogRestApi(BaseUrl, request, response); TimeoutCheck(request, response); apiResponse = new BaseResponseConcern(); if (response.IsSuccessful) { apiResponse.IsSuccess = true; } else { apiResponse.IsSuccess = false; if (!string.IsNullOrEmpty(response.Content)) { apiResponse.Errors = new string[] { response.Content.GetErrorMessage() }; } else { apiResponse.Errors = new string[] { response.ErrorMessage }; } } } catch { Log(restClient.BaseUrl, request, response); } return(apiResponse); }
public IResponseConcern <T> Execute <T>(IRestRequest request) { IResponseConcern <T> apiResponse = new BaseResponseConcern <T>(); IRestResponse rawResponse = default(IRestResponse); try { BeforeRequest(restClient, request); Stopwatch watcher = Stopwatch.StartNew(); try { rawResponse = restClient.Execute(request); watcher.Stop(); Logger.Log($"Time Taken For api:{restClient.BaseUrl}{request.Resource} Time: {watcher.Elapsed.TotalSeconds} sec"); } catch (Exception ex) { watcher.Stop(); Logger.Log($"Time Taken For api:{restClient.BaseUrl}{request.Resource} Time: {watcher.Elapsed.TotalSeconds} sec with Errors"); Logger.Log(ex); } AfterResponse(restClient, rawResponse); watcher.Stop(); LogRestApi(BaseUrl, request, rawResponse); TimeoutCheck(request, rawResponse); if (rawResponse.IsSuccessful) { try { apiResponse.Concern = JsonConvert.DeserializeObject <T>(rawResponse.Content); } catch (Exception ex) { Logger.Log("******************Deserialization failed****************"); Logger.LogCritical(ex); //TODO: tosolve the type mismatch issue but need to log message properly apiResponse.Concern = JsonConvert.DeserializeObject <T>(rawResponse.Content, new JsonSerializerSettings() { Converters = new List <JsonConverter>() { new DefaultValueConverter() } }); apiResponse.Errors = new string[] { ex.Message }; } apiResponse.IsSuccess = true; } else { apiResponse.IsSuccess = false; if (!string.IsNullOrEmpty(rawResponse.Content)) { apiResponse.Errors = new string[] { rawResponse.Content.GetErrorMessage() }; } else { apiResponse.Errors = new string[] { rawResponse.ErrorMessage }; } } } catch { Log(restClient.BaseUrl, request, rawResponse); } return(apiResponse); }