private void GenericUpload(object body, string path, HttpMethod method, int[] allowedStatusCodes, params string[] parameter) { var uri = CreateUri(path, parameter); string upload = JsonConvert.SerializeObject(body, GetSerializerSettings()); HttpResponseMessage resp; try { var request = new HttpRequestMessage { Content = new StringContent(upload, Encoding.UTF8, "application/json"), RequestUri = uri, Method = method, }; _headers.ToList().ForEach(h => request.Headers.Add(h.Key, h.Value)); LoggerSingleton.Inst.Debug("REST", "SendRequest<GenericUpload>", $"RequestUri := {uri}\nMethod := {method}\nHeaders :=\n[\n{string.Join("\n", _headers.Select(h => $" {h.Key} => '{h.Value}'"))}\n]"); resp = _client.SendAsync(request).Result; if (!resp.IsSuccessStatusCode) { if (allowedStatusCodes.Any(sc => sc == (int)resp.StatusCode)) { LoggerSingleton.Inst.Debug("REST", string.Format("REST call to '{0}' [{3}] returned (allowed) statuscode {1} ({2})", uri, (int)resp.StatusCode, resp.StatusCode, method)); _lastResponse = resp; return; } throw new RestStatuscodeException(uri.Host, (int)resp.StatusCode, resp.ReasonPhrase, string.Empty); } } catch (AggregateException e) { if (e.InnerExceptions.Count == 1) { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e0 is HttpRequestException); } else { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e, e0 is HttpRequestException); } } catch (RestException) { throw; } catch (Exception e) { throw new RestException("Could not communicate with server " + uri.Host, e, e is HttpRequestException); } LoggerSingleton.Inst.Debug("REST", string.Format("Calling REST API '{0}' [{1}]", uri, method), string.Format("Send:\r\n{0}\r\n\r\nRecieved: Nothing", CompactJsonFormatter.FormatJSON(upload, LOG_FMT_DEPTH))); _lastResponse = resp; }
private TResult GenericDownload <TResult>(string path, HttpMethod method, int[] allowedStatusCodes, params string[] parameter) { var uri = CreateUri(path, parameter); string download; HttpResponseMessage resp; try { var request = new HttpRequestMessage { RequestUri = uri, Method = method, }; _headers.ToList().ForEach(h => request.Headers.Add(h.Key, h.Value)); LoggerSingleton.Inst.Debug("REST", "SendRequest<GenericDownload>", $"RequestUri := {uri}\nMethod := {method}\nHeaders :=\n[\n{string.Join("\n", _headers.Select(h => $" {h.Key} => '{h.Value}'"))}\n]"); resp = _client.SendAsync(request).Result; if (!resp.IsSuccessStatusCode) { if (allowedStatusCodes.Any(sc => sc == (int)resp.StatusCode)) { LoggerSingleton.Inst.Debug("REST", string.Format("REST call to '{0}' [{3}] returned (allowed) statuscode {1} ({2})", uri, (int)resp.StatusCode, resp.StatusCode, method)); _lastResponse = resp; return(default(TResult)); } string content = string.Empty; try { content = resp.Content.ReadAsStringAsync().Result; } catch (Exception) { // ignore } throw new RestStatuscodeException(uri.Host, (int)resp.StatusCode, resp.ReasonPhrase, content); } download = resp.Content.ReadAsStringAsync().Result; } catch (AggregateException e) { if (e.InnerExceptions.Count == 1) { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e0 is HttpRequestException); } else { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e, e0 is HttpRequestException); } } catch (RestException) { throw; } catch (Exception e) { throw new RestException("Could not communicate with server " + uri.Host, e, e is HttpRequestException); } TResult downloadObject; try { downloadObject = JsonConvert.DeserializeObject <TResult>(download, _converter); } catch (Exception e) { throw new RestException("Rest call to " + uri.Host + " returned unexpected data", "Rest call to " + uri.Host + " returned unexpected data :\r\n" + download, e, false); } LoggerSingleton.Inst.Debug("REST", string.Format("Calling REST API '{0}' [{1}]", uri, method), string.Format("Send: Nothing\r\nRecieved:\r\n{0}", CompactJsonFormatter.FormatJSON(download, LOG_FMT_DEPTH))); _lastResponse = resp; return(downloadObject); }
private TResult GenericTwoWay <TResult>(object body, string path, HttpMethod method, int[] allowedStatusCodes, params string[] parameter) { var uri = CreateUri(path, parameter); var ident = $"[[{Guid.NewGuid().ToString("N").ToUpper().Substring(0,6)}]]"; string upload = JsonConvert.SerializeObject(body, GetSerializerSettings()); string download; HttpResponseMessage resp; try { var request = new HttpRequestMessage { Content = new StringContent(upload, Encoding.UTF8, "application/json"), RequestUri = uri, Method = method, }; _headers.ToList().ForEach(h => request.Headers.Add(h.Key, h.Value)); LoggerSingleton.Inst.Debug("REST", $"{ident} SendRequest<GenericTwoWay> [START] ({uri} :: {method})", $"RequestUri := {uri}\nMethod := {method}\nHeaders :=\n[\n{string.Join("\n", _headers.Select(h => $" {h.Key} => '{h.Value}'"))}\n]"); resp = _client.SendAsync(request).Result; if (!resp.IsSuccessStatusCode) { if (allowedStatusCodes.Any(sc => sc == (int)resp.StatusCode)) { LoggerSingleton.Inst.Warn("REST", $"{ident} SendRequest<GenericTwoWay> [FAILED] ({uri} :: {method})", $"REST call to '{uri}' [{method}] returned (allowed) statuscode {(int) resp.StatusCode} ({resp.StatusCode})"); _lastResponse = resp; return(default(TResult)); } string content = string.Empty; try { content = resp.Content.ReadAsStringAsync().Result; } catch (Exception) { // ignore } if (resp.StatusCode == HttpStatusCode.GatewayTimeout) { throw new RestStatuscodeException(uri.Host, (int)resp.StatusCode, resp.ReasonPhrase, content, true); } throw new RestStatuscodeException(uri.Host, (int)resp.StatusCode, resp.ReasonPhrase, content); } download = resp.Content.ReadAsStringAsync().Result; } catch (AggregateException e) { if (e.InnerExceptions.Count == 1) { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e0 is HttpRequestException); } else { var e0 = e.InnerExceptions.First(); throw new RestException("Could not communicate with server " + uri.Host, e, e0 is HttpRequestException); } } catch (RestException) { throw; } catch (Exception e) { throw new RestException("Could not communicate with server " + uri.Host, e, e is HttpRequestException); } TResult downloadObject; try { downloadObject = JsonConvert.DeserializeObject <TResult>(download, _converter); } catch (Exception e) { throw new RestException("Rest call to " + uri.Host + " returned unexpected data :\r\n" + download, e, false); } LoggerSingleton.Inst.Debug("REST", $"{ident} SendRequest<GenericTwoWay> [FINISH] ({uri} :: {method})", $"Send:\r\n{CompactJsonFormatter.FormatJSON(upload, LOG_FMT_DEPTH)}\n\n" + "---------------------\n\n" + $"Recieved:\n{CompactJsonFormatter.FormatJSON(download, LOG_FMT_DEPTH)}"); _lastResponse = resp; return(downloadObject); }