/// <summary> /// Performs an HTTP DELETE against a specified URI. /// </summary> /// <typeparam name="T">The request and response body type</typeparam> /// <param name="uri">The URI</param> /// <param name="operationName">The Operation Name</param> /// <param name="headers">Headers to add to the request</param> /// <returns>The response body content</returns> public async Task <T> DeleteAsync <T>(Uri uri, string operationName, NameValueCollection headers = null) { WebApiClientRequest request = this.CreateRequest(HttpMethod.Delete, uri, operationName, headers); WebApiClientResponse <T> response = await this.SendAsync <T>(request).ConfigureAwait(false); return(response.Content); }
/// <summary> /// Performs an HTTP PATCH against a specified URI. /// </summary> /// <typeparam name="TRequest">The request body type</typeparam> /// <typeparam name="TResponse">The response body type</typeparam> /// <param name="uri">The URI</param> /// <param name="content">The request body content</param> /// <param name="operationName">The operation name</param> /// <param name="headers">Headers to add to the request</param> /// <returns>The response body content</returns> public async Task <TResponse> PatchAsync <TRequest, TResponse>(Uri uri, TRequest content, string operationName, NameValueCollection headers = null) { WebApiClientRequest <TRequest> request = this.CreateRequest(new HttpMethod("PATCH"), uri, content, operationName, headers); WebApiClientResponse <TResponse> response = await this.SendAsync <TResponse>(request).ConfigureAwait(false); return(response.Content); }
/// <summary> /// Sends a client request. /// </summary> /// <typeparam name="T">The response body type</typeparam> /// <param name="request">The request to send</param> /// <returns>A client response</returns> public async Task <WebApiClientResponse <T> > SendAsync <T>(WebApiClientRequest request) { this.SetConnectionLeaseTimeout(request.Uri); using (var httpRequestMessage = request.CreateHttpRequestMessage()) { // Record the expected timeout so we can improve the exception thrown / logging DateTimeOffset timeoutExpectedAt = DateTimeOffset.UtcNow.Add(this.httpClient.Timeout); // Capture request body in case we need it for error logging purposes (as calling httpClient.SendAsync disposes it) string requestBody = httpRequestMessage.Content == null ? string.Empty : await httpRequestMessage.Content.ReadAsStringAsync(); try { using (var httpResponseMessage = await this.httpClient.SendAsync(httpRequestMessage).ConfigureAwait(false)) { if (!httpResponseMessage.IsSuccessStatusCode) { return(await ErrorResponseHandler <T>(httpResponseMessage, httpRequestMessage, this.clientParameters.MediaType, this.clientParameters.Formatter).ConfigureAwait(false)); } return(await HandleResponseAsync <T>(httpResponseMessage, this.clientParameters.MediaType, this.clientParameters.Formatter).ConfigureAwait(false)); } } catch (TaskCanceledException exception) { // .NET throws TaskCanceledExceptions for certain types of commonly seen timeouts. Throw a WebException if we think this is the case here. this.ThrowWebExceptionIfTimeout(exception, timeoutExpectedAt, httpRequestMessage.RequestUri, requestBody); // Since ThrowIfTimeout didn't throw, we have a genuine TaskCanceledException. Add request detail for error logging purposes & rethrow exception.Data.Add(HttpExceptionDataRequestMessageContent, requestBody); throw; } catch (OperationCanceledException exception) { // .NET throws OperationCanceledException for certain types of commonly seen timeouts. Throw a WebException if we think this is the case here. this.ThrowWebExceptionIfTimeout(exception, timeoutExpectedAt, httpRequestMessage.RequestUri, requestBody); // Since ThrowIfTimeout didn't throw, we have a genuine OperationCanceledException. Add request detail for error logging purposes & rethrow exception.Data.Add(HttpExceptionDataRequestMessageContent, requestBody); throw; } catch (Exception exception) { exception.Data.Add(HttpExceptionDataRequestMessageContent, requestBody); throw; } } }
/// <summary> /// Sends a client request. /// </summary> /// <param name="request">The request to send</param> /// <returns>A client response</returns> public async Task <WebApiClientResponse> SendAsync(WebApiClientRequest request) { return(await this.SendAsync <object>(request).ConfigureAwait(false)); }
/// <summary> /// Performs an HTTP DELETE against a specified URI. /// </summary> /// <param name="uri">The URI</param> /// <param name="operationName">The Operation Name</param> /// <param name="headers">Headers to add to the request</param> /// <returns>Void task</returns> public async Task DeleteAsync(Uri uri, string operationName, NameValueCollection headers = null) { WebApiClientRequest request = this.CreateRequest(HttpMethod.Delete, uri, operationName, headers); await this.SendAsync(request).ConfigureAwait(false); }