public Request On(int code, Action <RestRequest, IRestResponse, Error> action) { if (!StatusCodeActions.ContainsKey(code)) { StatusCodeActions.Add(code, action); } return(this); }
public async Task <T> Execute <T>(RestRequest customRestRequest = null) { var request = customRestRequest ?? new RestRequest(Method); request.Resource = Endpoint; request.Method = Method; if (Body != null) { var json = JsonConvert.SerializeObject(Body); if (IsNoisy) { Console.WriteLine($"|> OUT ==> {json}"); } request.AddParameter("application/json", json, ParameterType.RequestBody); } var response = await RestClient.ExecuteAsync(request); if (IsNoisy) { Console.WriteLine($"<== IN <| {response.Content}"); } var data = JsonConvert.DeserializeObject <T>(response.Content); if (response.StatusCode == HttpStatusCode.OK) { var action = SuccessDelegate as Action <T>; action?.Invoke(data); } else { if (StatusCodeActions.ContainsKey((int)response.StatusCode) && !IsIgnored(response.StatusCode)) { Error err = null; try { err = JsonConvert.DeserializeObject <Error>(response.Content); } catch { /* ignore */ } StatusCodeActions[(int)response.StatusCode]( request, response, err ); } else { throw new HttpRequestException( "Received non-OK status code and no handler was specified for this request.\n"); } } return(data); }