HttpRequestScope Class

Namespace
Kampute.HttpClient
Assembly
  • Kampute.HttpClient.dll

Definition

Collects headers and request properties, and applies them to the requests that a client sends within PerformAsync(Func<HttpRestClient, Task>).
public sealed class HttpRequestScope
Inheritance

Remarks

Examples

var report = await client
    .WithScope()
    .SetHeader("Accept", MediaTypeNames.Text.Csv)
    .PerformAsync(scopedClient => scopedClient.GetAsStringAsync("reports/daily"));

Constructors

HttpRequestScope(HttpRestClient)

Initializes a new instance of the HttpRequestScope class.
public HttpRequestScope(HttpRestClient client)

Parameters

client HttpRestClient
The client whose requests the scope applies to.

Exceptions

ArgumentNullException
Thrown if client is null.

Properties

Client

Gets the client whose requests the scope applies to.
public HttpRestClient Client { get; }

Property Value

HttpRestClient
The HttpRestClient of this scope.

Headers

Gets the headers of this scope.
public IReadOnlyCollection<KeyValuePair<string, string>> Headers { get; }

Property Value

IReadOnlyCollection<KeyValuePair<string, string>>
The headers to set, and, with a null value, the headers to remove.

Properties

Gets the request properties of this scope.
public IReadOnlyCollection<KeyValuePair<string, object>> Properties { get; }

Property Value

IReadOnlyCollection<KeyValuePair<string, object>>
The properties to set, and, with a null value, the properties to remove.

Methods

PerformAsync(Func<HttpRestClient, Task>)

Runs an asynchronous action whose requests get the headers and properties of this scope.
public Task PerformAsync(Func<HttpRestClient, Task> scopedAction)

Parameters

scopedAction Func<HttpRestClient, Task>
The action to run. It receives the client of this scope.

Returns

Task
A task representing the asynchronous operation.

Exceptions

ArgumentNullException
Thrown if scopedAction is null.

PerformAsync<T>(Func<HttpRestClient, Task<T>>)

Runs an asynchronous function whose requests get the headers and properties of this scope, and returns its result.
public Task<T> PerformAsync<T>(Func<HttpRestClient, Task<T>> scopedFunction)

Type Parameters

T
The type of the result of the function.

Parameters

scopedFunction Func<HttpRestClient, Task<T>>
The function to run. It receives the client of this scope.

Returns

Task<T>
A task representing the asynchronous operation with a result of type T.

Exceptions

ArgumentNullException
Thrown if scopedFunction is null.

SetHeader(string, string)

Sets a header of the requests sent within this scope, replacing any default value.
public HttpRequestScope SetHeader(string name, string value)

Parameters

name string
The name of the header.
value string
The value of the header.

Returns

HttpRequestScope
The same HttpRequestScope instance for fluent chaining.

Exceptions

ArgumentNullException
Thrown if the name argument is null.

SetProperty(string, object)

Sets a request property of the requests sent within this scope.
public HttpRequestScope SetProperty(string name, object value)

Parameters

name string
The name of the property.
value object
The value of the property.

Returns

HttpRequestScope
The same HttpRequestScope instance for fluent chaining.

Exceptions

ArgumentNullException
Thrown if the name argument is null.

UnsetHeader(string)

Removes a header from the requests sent within this scope, including a default one.
public HttpRequestScope UnsetHeader(string name)

Parameters

name string
The header name to remove.

Returns

HttpRequestScope
The same HttpRequestScope instance for fluent chaining.

Exceptions

ArgumentNullException
Thrown if the name argument is null.

UnsetProperty(string)

Removes a request property from the requests sent within this scope.
public HttpRequestScope UnsetProperty(string name)

Parameters

name string
The name of the property.

Returns

HttpRequestScope
The same HttpRequestScope instance for fluent chaining.

Exceptions

ArgumentNullException
Thrown if the name argument is null.