HttpRestClientExtensions Class
- Namespace
- Kampute.HttpClient
- Assembly
- Kampute.HttpClient.dll
Definition
public static class HttpRestClientExtensions- Inheritance
- object
- HttpRestClientExtensions
Remarks
Methods
DeleteAsync(this HttpRestClient, string, CancellationToken)
public static Task DeleteAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
DeleteAsync<T>(this HttpRestClient, string, CancellationToken)
public static Task<T> DeleteAsync<T>(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Type Parameters
T- The type of the response object.
Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
DownloadAsync(this HttpRestClient, HttpMethod, string, HttpContent, Func<HttpContentHeaders, Stream>, CancellationToken)
public static Task<Stream> DownloadAsync(this
HttpRestClient client,
HttpMethod method,
string uri,
HttpContent payload,
Func<HttpContentHeaders, Stream> streamProvider,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
streamProviderFunc<HttpContentHeaders, Stream>- A function that returns a Stream based on the HTTP content headers.
cancellationTokenCancellationToken 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, orstreamProviderisnull. - InvalidOperationException
- Thrown if
streamProviderreturnsnull. - 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, or server certificate validation.
- IOException
- Thrown if transferring the response body fails, or writing to the stream from
streamProviderfails. - OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
Remarks
The response body is not buffered: it is copied from the network into the stream returned by streamProvider as it arrives, and the cancellation token can stop the copy. If the copy fails or is canceled, that stream is disposed before the exception is rethrown.
HttpClient.Timeout covers only the time until the response headers arrive. Because the body is not buffered, an HttpRestClient.AfterReceivingResponse handler that reads the response content consumes it, and nothing is copied.
GetAsByteArrayAsync(this HttpRestClient, string, CancellationToken)
public static Task<byte[]> GetAsByteArrayAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
GetAsStreamAsync(this HttpRestClient, string, CancellationToken)
public static Task<Stream> GetAsStreamAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
Remarks
The task completes as soon as the response headers arrive. The response body is not buffered: the returned stream reads it from the network, so the caller must dispose the stream to release the connection. Reading the stream can fail with an IOException if the transfer fails.
HttpClient.Timeout covers only the time until the response headers arrive. Because the body is not buffered, an HttpRestClient.AfterReceivingResponse handler that reads the response content consumes the stream.
GetAsStringAsync(this HttpRestClient, string, CancellationToken)
public static Task<string> GetAsStringAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
GetAsync<T>(this HttpRestClient, string, CancellationToken)
public static Task<T> GetAsync<T>(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Type Parameters
T- The type of the response object.
Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
GetToStreamAsync(this HttpRestClient, string, Stream, CancellationToken)
public static Task GetToStreamAsync(this
HttpRestClient client,
string uri,
Stream stream,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
streamStream- The Stream where the response body is written.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
uriorstreamisnull. - 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, or server certificate validation.
- IOException
- Thrown if transferring the response body fails, or writing to
streamfails. - OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
Remarks
The response body is not buffered: it is copied from the network into stream as it arrives, and the cancellation token can stop the copy.
HttpClient.Timeout covers only the time until the response headers arrive. Because the body is not buffered, an HttpRestClient.AfterReceivingResponse handler that reads the response content consumes it, and nothing is copied.
HeadAsync(this HttpRestClient, string, CancellationToken)
public static Task<HttpResponseHeaders> HeadAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
OptionsAsync(this HttpRestClient, string, CancellationToken)
public static Task<HttpResponseHeaders> OptionsAsync(this HttpRestClient client, string uri, CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
PatchAsync(this HttpRestClient, string, HttpContent, CancellationToken)
public static Task PatchAsync(this
HttpRestClient client,
string uri,
HttpContent payload,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
PatchAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)
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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
PostAsync(this HttpRestClient, string, HttpContent, CancellationToken)
public static Task PostAsync(this
HttpRestClient client,
string uri,
HttpContent payload,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
PostAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)
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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
PutAsync(this HttpRestClient, string, HttpContent, CancellationToken)
public static Task PutAsync(this
HttpRestClient client,
string uri,
HttpContent payload,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
uriisnull. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
PutAsync<T>(this HttpRestClient, string, HttpContent, CancellationToken)
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
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
uristring- The URI to which the request is sent.
payloadHttpContent- The HTTP request payload content.
cancellationTokenCancellationToken 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
uriisnull. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
SendObjectAsync(this HttpRestClient, HttpMethod, string, object, string, CancellationToken)
public static Task SendObjectAsync(this
HttpRestClient client,
HttpMethod method,
string uri,
object payload,
string mediaType,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to send as the request payload. An HttpContent is sent as it is.
mediaTypestring- The media type in which to write
payload. cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,method,uri,payloadormediaTypeisnull. - InvalidOperationException
- Thrown if no formatter in HttpRestClient.ContentFormatters can write
payloadinmediaType. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
Remarks
mediaType. The argument exceptions and the InvalidOperationException are thrown when the method is called, before anything is sent.SendObjectAsync(this HttpRestClient, HttpMethod, string, object, IHttpContentFormatter, CancellationToken)
public static Task SendObjectAsync(this
HttpRestClient client,
HttpMethod method,
string uri,
object payload,
IHttpContentFormatter formatter,
CancellationToken cancellationToken = default)Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to send as the request payload. An HttpContent is sent as it is.
formatterIHttpContentFormatter- The formatter that writes
payload, in the first media type it can write for the type of the payload. cancellationTokenCancellationToken optional- A token for canceling the request (optional).
Returns
- Task
- A task that represents the asynchronous operation.
Exceptions
- ArgumentNullException
- Thrown if
client,method,uri,payloadorformatterisnull. - InvalidOperationException
- Thrown if
formattercannot write the type ofpayload. - 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, or server certificate validation.
- OperationCanceledException
- Thrown if the operation is canceled via the cancellation token, or if the timeout of the underlying HttpClient elapses.
Remarks
formatter even when another formatter is registered for the same media type. The argument exceptions and the InvalidOperationException are thrown when the method is called, before anything is sent.SendObjectAsync<T>(this HttpRestClient, HttpMethod, string, object, string, CancellationToken)
public static Task<T> SendObjectAsync<T>(this
HttpRestClient client,
HttpMethod method,
string uri,
object payload,
string mediaType,
CancellationToken cancellationToken = default)Type Parameters
T- The type of the response object.
Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to send as the request payload. An HttpContent is sent as it is.
mediaTypestring- The media type in which to write
payload. cancellationTokenCancellationToken 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
client,method,uri,payloadormediaTypeisnull. - InvalidOperationException
- Thrown if no formatter in HttpRestClient.ContentFormatters can write
payloadinmediaType. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
Remarks
mediaType. The argument exceptions and the InvalidOperationException are thrown when the method is called, before anything is sent.SendObjectAsync<T>(this HttpRestClient, HttpMethod, string, object, IHttpContentFormatter, CancellationToken)
public static Task<T> SendObjectAsync<T>(this
HttpRestClient client,
HttpMethod method,
string uri,
object payload,
IHttpContentFormatter formatter,
CancellationToken cancellationToken = default)Type Parameters
T- The type of the response object.
Parameters
clientHttpRestClient- The HttpRestClient instance to be used for sending the request.
methodHttpMethod- The HTTP method to use for the request.
uristring- The URI to which the request is sent.
payloadobject- The object to send as the request payload. An HttpContent is sent as it is.
formatterIHttpContentFormatter- The formatter that writes
payload, in the first media type it can write for the type of the payload. cancellationTokenCancellationToken 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
client,method,uri,payloadorformatterisnull. - InvalidOperationException
- Thrown if
formattercannot write the type ofpayload. - 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, or server certificate validation.
- 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, or if the timeout of the underlying HttpClient elapses.
Remarks
formatter even when another formatter is registered for the same media type. The response is still read by the registered formatters. The argument exceptions and the InvalidOperationException are thrown when the method is called, before anything is sent.WithScope(this HttpRestClient)
public static HttpRequestScope WithScope(this HttpRestClient client)Parameters
clientHttpRestClient- The HttpRestClient instance for which the scope is created.
Returns
- HttpRequestScope
- A new HttpRequestScope for
client.
Exceptions
- ArgumentNullException
- Thrown if the
clientargument isnull.
