HttpRestClientExtensions Class

Namespace
Kampute.HttpClient
Assembly
  • Kampute.HttpClient.dll

Definition

Provides extension methods for HttpRestClient to facilitate sending HTTP requests using various methods, including GET, POST, PUT, PATCH, and DELETE.
public static class HttpRestClientExtensions
Inheritance
  • object
  • HttpRestClientExtensions

Remarks

This static class enriches HttpRestClient by adding convenient extension methods for making HTTP requests. These methods simplify the process of constructing and sending requests for common HTTP methods, enabling more readable and concise client code.

Methods

DeleteAsync<T>(this HttpRestClient, string, CancellationToken)

Sends an asynchronous DELETE request to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> DeleteAsync<T>(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

DeleteAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous DELETE request to the specified URI without processing the response body.
public static Task DeleteAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

DownloadAsync(this HttpRestClient, HttpMethod, string, HttpContent, Func<HttpContentHeaders, Stream>, CancellationToken)

Sends an asynchronous HTTP request with the specified method, URI, and payload, returning the response content as a stream.
public static Task<Stream> DownloadAsync(
	this HttpRestClient client,
	HttpMethod method,
	string uri,
	HttpContent payload,
	Func<HttpContentHeaders, Stream> streamProvider,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
method HttpMethod
The HTTP method to use for the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
streamProvider Func<HttpContentHeaders, Stream>
A function that returns a Stream based on the HTTP content headers.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<Stream>
A task that represents the asynchronous operation. The task result contains a Stream that represents the response content.

Exceptions

ArgumentNullException
Thrown if method, uri, or streamProvider is null.
InvalidOperationException
Thrown if streamProvider returns null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

GetAsByteArrayAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous GET request to the specified URI and returns the response body as an array of bytes.
public static Task<byte[]> GetAsByteArrayAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<byte[]>
A task representing the asynchronous operation, returning an array of bytes.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

GetAsStreamAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous GET request to the specified URI and returns the response body as a Stream.
public static Task<Stream> GetAsStreamAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<Stream>
A task that represents the asynchronous operation, returning a Stream.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

GetAsStringAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous GET request to the specified URI and returns the response body as a string.
public static Task<string> GetAsStringAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<string>
A task representing the asynchronous operation, returning a string.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

GetAsync<T>(this HttpRestClient, string, CancellationToken)

Sends an asynchronous GET request to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> GetAsync<T>(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

GetToStreamAsync(this HttpRestClient, string, Stream, CancellationToken)

Sends an asynchronous GET request to the specified URI and write the response body into the provided Stream.
public static Task GetToStreamAsync(
	this HttpRestClient client,
	string uri,
	Stream stream,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
stream Stream
The Stream where the response body is written.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if uri or stream is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

HeadAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous HEAD request to the specified URI and returns the response headers.
public static Task<HttpResponseHeaders> HeadAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<HttpResponseHeaders>
A task representing the asynchronous operation, returning the response headers.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

OptionsAsync(this HttpRestClient, string, CancellationToken)

Sends an asynchronous OPTIONS request to the specified URI and returns the response headers.
public static Task<HttpResponseHeaders> OptionsAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<HttpResponseHeaders>
A task representing the asynchronous operation, returning the response headers.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PatchAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous PATCH request to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> PatchAsync<T>(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PatchAsync(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous PATCH request to the specified URI without processing the response body.
public static Task PatchAsync(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PostAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous POST request to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> PostAsync<T>(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PostAsync(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous POST request to the specified URI without processing the response body.
public static Task PostAsync(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PutAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous PUT request to the specified URI and returns the response body deserialized as the specified type.
public static Task<T> PutAsync<T>(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Type Parameters

T
The type of the response object.

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task<T>
A task representing the asynchronous operation, returning a deserialized object of type T.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

PutAsync(this HttpRestClient, string, HttpContent, CancellationToken)

Sends an asynchronous PUT request to the specified URI without processing the response body.
public static Task PutAsync(
	this HttpRestClient client,
	string uri,
	HttpContent payload,
	CancellationToken cancellationToken = default)

Parameters

client HttpRestClient
The HttpRestClient instance to be used for sending the request.
uri string
The URI to which the request is sent.
payload HttpContent
The HTTP request payload content.
cancellationToken CancellationToken optional
A token for canceling the request (optional).

Returns

Task
A task that represents the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if uri is null.
HttpResponseException
Thrown if the response status code indicates a failure.
HttpRequestException
Thrown if the request fails due to an underlying issue such as network connectivity, DNS failure, server certificate validation, or timeout.
HttpContentException
Thrown if the response body is empty or its media type is not supported.
OperationCanceledException
Thrown if the operation is canceled via the cancellation token.

WithScope(this HttpRestClient)

Creates a new HttpRequestScope for managing scoped modifications of properties and headers for HTTP requests sent using the HttpRestClient.
public static HttpRequestScope WithScope(this HttpRestClient client)

Parameters

client HttpRestClient
The HttpRestClient instance for which the scope is created.

Returns

HttpRequestScope
An instance of HttpRequestScope that allows properties and headers to be temporarily modified for requests made through the client.

Exceptions

ArgumentNullException
Thrown if the client argument is null>.