// Detects of any exceptions have occured and throws the appropriate exceptions. protected internal virtual void DetectError(HTTPClient client) { ResponseException exception = null; string reason = string.Format("{0} {1}", StatusCode, ReasonPhrase); if (StatusCode >= 500) { exception = new ServerException(this, new Exception(reason)); } else if (StatusCode == 404) { exception = new NotFoundException(this, new Exception(reason)); } else if (StatusCode == 401) { exception = new AuthenticationException(this, new Exception(reason)); } else if (StatusCode >= 400) { exception = new ClientException(this, new Exception(reason)); } else if (!Parsed) { exception = new ParserException(this, new Exception(reason)); } if (exception != null) { exception.Log(client.Configuration, this); throw exception; } }
// Tries to read the body. private string ReadBody(HTTPClient client) { // Get the connection HttpClient connection = Request.Connection; try { ReasonPhrase = ""; Task <HttpResponseMessage> response = connection.SendAsync(client.HttpRequestMessage); if (response.Result.IsSuccessStatusCode) { ParseStatusCode((int)response.Result.StatusCode); return(JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result).ToString()); } else { ParseStatusCode((int)response.Result.StatusCode); ReasonPhrase = response.Result.ReasonPhrase; return(JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result).ToString()); } } catch (Exception ex) { ParseStatusCode(500); throw new ServerException(this, ex); } }
// Ties to parse the response body into a JSON Object private JObject ParseJson(HTTPClient client) { if (Json) { return(JObject.Parse(Body)); } return(null); }
// Tries to parse the data private void ParseData(HTTPClient client) { this.Parsed = false; this.Body = ReadBody(client); this.Result = ParseJson(client); this.Parsed = this.Result != null; this.Data = null; if (Parsed && Result.ContainsKey("data")) { if (Result["data"].Type == JTokenType.Array) { this.Data = (JArray)Result["data"]; } if (Result["data"].Type == JTokenType.Object) { this.Data = (JObject)Result["data"]; } } }
// Tries to parse the raw response from the request. protected internal virtual void Parse(HTTPClient client) { ParseData(client); }
protected internal Request(string verb, string path, Params @params, string bearerToken, HTTPClient client) { Configuration config = client.Configuration; this.Verb = verb; this.Host = config.Host; this.Path = path; this.@Params = @params; this.BearerToken = bearerToken; this.LanguageVersion = "netstandard2.0"; this.ClientVersion = Amadeus.VERSION; this.AppId = config.CustomAppId; this.AppVersion = config.CustomAppVersion; this.Port = config.Port; this.Ssl = config.Ssl; DetermineScheme(); PrepareUrl(); PrepareHeaders(); }