Class HttpClientExtensions
Helper methods for IHttpClient.
Inherited Members
Namespace: Unity.Cloud.Common
Syntax
public static class HttpClientExtensions
Methods
CreateHttpRequestMessage(HttpMethod, Uri)
Creates an HttpRequestMessage from an HttpMethod and a Uri.
Declaration
public static HttpRequestMessage CreateHttpRequestMessage(HttpMethod httpMethod, Uri uri)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpMethod | httpMethod | The HTTP method. |
| Uri | uri | The Uri to request. |
Returns
| Type | Description |
|---|---|
| HttpRequestMessage | The created HttpRequestMessage. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the URI is null. |
CreateUri(String)
Declaration
public static Uri CreateUri(string uri)
Parameters
| Type | Name | Description |
|---|---|---|
| String | uri | The String to convert. |
Returns
| Type | Description |
|---|---|
| Uri | The created Uri. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when an unhandled exception is thrown constructing the URI. |
DeleteAsync(IHttpClient, String, HttpContent, CancellationToken)
Sends an asynchronous DELETE request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> DeleteAsync(this IHttpClient httpClient, string requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
DeleteAsync(IHttpClient, String, CancellationToken)
Sends an asynchronous DELETE request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> DeleteAsync(this IHttpClient httpClient, string requestUri, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
DeleteAsync(IHttpClient, Uri, HttpContent, CancellationToken)
Sends an asynchronous DELETE request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> DeleteAsync(this IHttpClient httpClient, Uri requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
DeleteAsync(IHttpClient, Uri, CancellationToken)
Sends an asynchronous DELETE request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> DeleteAsync(this IHttpClient httpClient, Uri requestUri, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
GetAsync(IHttpClient, String, CancellationToken)
Sends an asynchronous GET request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> GetAsync(this IHttpClient httpClient, string requestUri, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
GetAsync(IHttpClient, Uri, CancellationToken)
Sends an asynchronous GET request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> GetAsync(this IHttpClient httpClient, Uri requestUri, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PatchAsync(IHttpClient, String, HttpContent, CancellationToken)
Sends an asynchronous PATCH request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PatchAsync(this IHttpClient httpClient, string requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PatchAsync(IHttpClient, Uri, HttpContent, CancellationToken)
Sends an asynchronous PATCH request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PatchAsync(this IHttpClient httpClient, Uri requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PostAsync(IHttpClient, String, HttpContent, String, CancellationToken)
Sends an asynchronous POST request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PostAsync(this IHttpClient httpClient, string requestUri, HttpContent content, string downloadFilePath = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| String | downloadFilePath | The path for the downloaded file. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PostAsync(IHttpClient, Uri, HttpContent, String, CancellationToken)
Sends an asynchronous POST request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PostAsync(this IHttpClient httpClient, Uri requestUri, HttpContent content, string downloadFilePath = null, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| String | downloadFilePath | The path for the downloaded file. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PutAsync(IHttpClient, String, HttpContent, CancellationToken)
Sends an asynchronous PUT request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PutAsync(this IHttpClient httpClient, string requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| String | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the requestUri is invalid. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
PutAsync(IHttpClient, Uri, HttpContent, CancellationToken)
Sends an asynchronous PUT request to the specified Uri.
Declaration
public static Task<HttpResponseMessage> PutAsync(this IHttpClient httpClient, Uri requestUri, HttpContent content, CancellationToken cancellationToken = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | httpClient | The HTTP client. |
| Uri | requestUri | The uri for the request. |
| HttpContent | content | The HTTP content for the request. |
| CancellationToken | cancellationToken | The cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<HttpResponseMessage> |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when the requestUri is null. |
| HttpRequestException | Thrown when an HTTP response can't be obtained from the server. |
| OperationCanceledException | Thrown when the request is cancelled by a cancellation token. |
| TimeoutException | Thrown when the request failed due to timeout. |
WithApiSourceHeaders(IHttpClient, String, String)
Creates an instance of Unity.Cloud.Common.HttpClientHeaderModifier which adds the API source headers to each request.
Declaration
public static IHttpClient WithApiSourceHeaders(this IHttpClient baseHttpClient, string name, string version)
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | baseHttpClient | The client for which to modify the request headers. |
| String | name | The API source name. |
| String | version | The API source version. |
Returns
| Type | Description |
|---|---|
| IHttpClient | The created Unity.Cloud.Common.HttpClientHeaderModifier. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown is |
WithApiSourceHeadersFromAssembly(IHttpClient, Assembly)
Creates an instance of Unity.Cloud.Common.HttpClientHeaderModifier which adds the API source headers to each request. The source values are retrieved from the ApiSourceVersionAttribute which must be defined in the calling Assembly.
Declaration
public static IHttpClient WithApiSourceHeadersFromAssembly(this IHttpClient baseHttpClient, Assembly assembly)
Parameters
| Type | Name | Description |
|---|---|---|
| IHttpClient | baseHttpClient | The client for which to modify the request headers. |
| Assembly | assembly | The target assembly. |
Returns
| Type | Description |
|---|---|
| IHttpClient | The created Unity.Cloud.Common.HttpClientHeaderModifier. |
Remarks
An instance of the ApiSourceVersionAttribute must be defined at the assembly-level in the calling Assembly in order for the correct API source values to be added as a header.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
| InvalidOperationException | Thrown if ApiSourceVersionAttribute does not exist or is not initialized in the calling assembly. |
| InvalidArgumentException | Thrown if ApiSourceVersionAttribute is initialized with null or empty values in the calling assembly. |