/// <summary> /// Executes a request and returns a JToken for querying. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> /// <remarks> /// This method will automatically dispose the <paramref name="baseClient"/> and <paramref name="content" /> when finished. /// </remarks> protected async Task<RequestResult<JToken>> ExecuteRequestAsync(RequestUri uri, HttpMethod method, CancellationToken cancellationToken, HttpContent content = null) { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _ExecutionPolicy.Run(baseRequestMessage, async (requestMessage) => { var request = _Client.SendAsync(requestMessage, cancellationToken); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); JToken jtoken = null; // Don't parse the result when the request was Delete. if (baseRequestMessage.Method != HttpMethod.Delete) { // Make sure that dates are not stripped of any timezone information if tokens are de-serialised into strings/DateTime/DateTimeZoneOffset using (var reader = new JsonTextReader(new StringReader(rawResult)) { DateParseHandling = DateParseHandling.None }) { jtoken = await JObject.LoadAsync(reader, cancellationToken); } } return new RequestResult<JToken>(response, jtoken, rawResult, ReadLinkHeader(response)); } }, cancellationToken); return policyResult; } }
/// <summary> /// Executes a request and returns a JToken for querying. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> /// <remarks> /// This method will automatically dispose the <paramref name="baseClient"/> and <paramref name="content" /> when finished. /// </remarks> protected async Task <JToken> ExecuteRequestAsync(RequestUri uri, HttpMethod method, HttpContent content = null) { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _ExecutionPolicy.Run(baseRequestMessage, async (requestMessage) => { var request = _Client.SendAsync(requestMessage); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); JToken jtoken = null; // Don't parse the result when the request was Delete. if (baseRequestMessage.Method != HttpMethod.Delete) { jtoken = JToken.Parse(rawResult); } return(new RequestResult <JToken>(response, jtoken, rawResult)); } }); return(policyResult); } }
/// <summary> /// Executes a request and returns a JToken for querying. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> /// <remarks> /// This method will automatically dispose the <paramref name="baseClient"/> and <paramref name="baseContent" /> when finished. /// </remarks> protected async Task <JToken> ExecuteRequestAsync(IFlurlClient baseClient, HttpMethod method, JsonContent baseContent = null) { var policyResult = await _ExecutionPolicy.Run(baseClient, baseContent, async (client, content) => { var request = client.SendAsync(method, content); var response = await request; var rawResult = await request.ReceiveString(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); JToken jtoken = null; // Don't parse the result when the request was Delete. if (method != HttpMethod.Delete) { jtoken = JToken.Parse(rawResult); } return(new RequestResult <JToken>(response, jtoken, rawResult)); }); baseClient.Dispose(); baseContent?.Dispose(); return(policyResult); }
/// <summary> /// Executes a request and returns a JToken for querying. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> /// <remarks> /// This method will automatically dispose the /// <paramref> /// <name>baseClient</name> /// </paramref> /// and <paramref name="content" /> when finished. /// </remarks> protected async Task <JToken> ExecuteRequestAsync(RequestUri uri, HttpMethod method, HttpContent content = null) { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _executionPolicy.Run(baseRequestMessage, async requestMessage => { Client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); var request = Client.SendAsync(requestMessage); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); JToken jtoken = null; // Don't parse the result when the request was Delete. if (baseRequestMessage != null && baseRequestMessage.Method != HttpMethod.Delete) { jtoken = JToken.Parse(rawResult); } return(new RequestResult <JToken>(response, jtoken, rawResult)); } }); return(policyResult); } }
/// <summary> /// Executes a request and returns the given type. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> protected async Task <T> ExecuteRequestAsync <T>(RequestUri uri, HttpMethod method, HttpContent content = null, string rootElement = null) where T : new() { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _ExecutionPolicy.Run(baseRequestMessage, async requestMessage => { var request = _Client.SendAsync(requestMessage); var result = new T(); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); // This method may fail when the method was Delete, which is intendend. // Delete methods should not be parsing the response JSON and should instead // be using the non-generic ExecuteRequestAsync. var reader = new JsonTextReader(new StringReader(rawResult)); var data = _Serializer.Deserialize <JObject>(reader); if (data != null) { result = data.ToObject <T>(); } return(new RequestResult <T>(response, result, rawResult)); } }); return(policyResult); } }
/// <summary> /// Executes a request and returns the JToken result. Throws an exception when the response is invalid. /// </summary> public static async Task <JToken> ExecuteRequestAsync(BizwebRequestMessage requestMsg, IRequestExecutionPolicy execPolicy) { if (requestMsg.Content != null) { //necessary to buffer content for multiple reads await requestMsg.Content.LoadIntoBufferAsync(); } return(await execPolicy.Run(requestMsg, async (reqMsg) => { //Need to create a RequestInfo before send RequestMessage //because after that, HttpClient will dispose RequestMessage var requestInfo = await CreateRequestSimpleInfoAsync(reqMsg); using (var response = await HttpUtils.SendHttpRequestAsync(reqMsg)) { //Check for and throw exception when necessary. await CheckResponseExceptionsAsync(response, requestInfo); // When using JToken make sure that dates are not stripped of any timezone information // if tokens are de-serialised into strings/DateTime/DateTimeZoneOffset using (var reader = new JsonTextReader(new StreamReader(await response.Content.ReadAsStreamAsync())) { DateParseHandling = DateParseHandling.None }) { //Notice: deserialize can fails when response body null or empty var result = Deserialize(reader, reqMsg.RootElement); return new RequestResult <JToken>(response, result); } } })); }
/// <summary> /// Executes a request and returns the raw result string. Throws an exception when the response is invalid. /// </summary> public static async Task <string> ExecuteRequestToStringAsync(BizwebRequestMessage requestMsg, IRequestExecutionPolicy execPolicy) { if (requestMsg.Content != null) { //necessary to buffer content for multiple reads await requestMsg.Content.LoadIntoBufferAsync(); } return(await execPolicy.Run(requestMsg, async (reqMsg) => { //Need to create a RequestInfo before send RequestMessage //because after that, HttpClient will dispose RequestMessage var requestInfo = await CreateRequestSimpleInfoAsync(reqMsg); using (var response = await HttpUtils.SendHttpRequestAsync(reqMsg)) { //Check for and throw exception when necessary. await CheckResponseExceptionsAsync(response, requestInfo); var rawResponse = await response.Content.ReadAsStringAsync(); return new RequestResult <string>(response, rawResponse); } })); }
/// <summary> /// Executes a request and returns the given type. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> protected async Task <T> ExecuteRequestAsync <T>(RequestUri uri, HttpMethod method, HttpContent content = null, string rootElement = null) where T : new() { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _ExecutionPolicy.Run(baseRequestMessage, async requestMessage => { var request = _Client.SendAsync(requestMessage); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. CheckResponseExceptions(response, rawResult); // This method may fail when the method was Delete, which is intendend. // Delete methods should not be parsing the response JSON and should instead // be using the non-generic ExecuteRequestAsync. try { var result = JsonConvert.DeserializeObject <T>(rawResult); return(new RequestResult <T>(response, result, rawResult)); } catch (Exception e) { Console.WriteLine(e); throw; } } }); return(policyResult); } }
/// <summary> /// Executes a request and returns the given type. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> protected async Task <T> ExecuteRequestAsync <T>(RequestUri uri, HttpMethod method, HttpContent content = null, string rootElement = null) { using (var baseRequestMessage = PrepareRequestMessage(uri, method, content)) { var policyResult = await _ExecutionPolicy.Run(baseRequestMessage, async requestMessage => { // update client for basic authentication var byteArray = Encoding.ASCII.GetBytes($"{_userName}:{_apiKey}"); _Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var request = _Client.SendAsync(requestMessage); using (var response = await request) { var rawResult = await response.Content.ReadAsStringAsync(); //Check for and throw exception when necessary. if (!string.IsNullOrEmpty(rawResult)) { CheckResponseExceptions(response, rawResult); } // This method may fail when the method was Delete, which is intendend. // Delete methods should not be parsing the response JSON and should instead // be using the non-generic ExecuteRequestAsync. var result = JsonConvert.DeserializeObject <T>(rawResult); return(new RequestResult <T>(response, result, rawResult)); } }); return(policyResult); } }
/// <summary> /// Executes a <see cref="IRestRequest"/> and returns a JToken for querying, or throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant creating its own class. /// </summary> /// <param name="client">A <see cref="RestClient"/>.</param> /// <param name="request">An <see cref="IRestRequest"/>.</param> /// <returns>The <see cref="JToken"/> to be queried.</returns> public static async Task <JToken> ExecuteRequestAsync(RestClient client, IRestRequest request) { return(await _executionPolicy.Run(client, request, async() => { //Make request IRestResponse response = await client.ExecuteTaskAsync(request); //Check for and throw exception when necessary. CheckResponseExceptions(response); //Get the raw response string string respString = Encoding.UTF8.GetString(response.RawBytes); //Parse the string if it exists, else parse an empty object. The empty object is expected when //Shopify returns a 0-byte body in it's response (e.g. when deleting a charge). var result = JToken.Parse(string.IsNullOrEmpty(respString) ? "{}" : respString); return new RequestResult <JToken>(response, result); })); }