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
- object
- HttpRequestScope
Remarks
Create an instance with WithScope(this HttpRestClient). The headers and properties apply only to the requests of the action that PerformAsync(Func<HttpRestClient, Task>) runs, as with HttpRestClient.BeginHeaderScope(IEnumerable<KeyValuePair<string, string>>) and HttpRestClient.BeginPropertyScope(IEnumerable<KeyValuePair<string, object>>).
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
clientHttpRestClient- The client whose requests the scope applies to.
Exceptions
- ArgumentNullException
- Thrown if
clientisnull.
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
nullvalue, 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
nullvalue, 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
scopedActionFunc<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
scopedActionisnull.
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
scopedFunctionFunc<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
scopedFunctionisnull.
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
Returns
- HttpRequestScope
- The same HttpRequestScope instance for fluent chaining.
Exceptions
- ArgumentNullException
- Thrown if the
nameargument isnull.
SetProperty(string, object)
Sets a request property of the requests sent within this scope.
public HttpRequestScope SetProperty(string name, object value)Parameters
Returns
- HttpRequestScope
- The same HttpRequestScope instance for fluent chaining.
Exceptions
- ArgumentNullException
- Thrown if the
nameargument isnull.
UnsetHeader(string)
Removes a header from the requests sent within this scope, including a default one.
public HttpRequestScope UnsetHeader(string name)Parameters
namestring- The header name to remove.
Returns
- HttpRequestScope
- The same HttpRequestScope instance for fluent chaining.
Exceptions
- ArgumentNullException
- Thrown if the
nameargument isnull.
UnsetProperty(string)
Removes a request property from the requests sent within this scope.
public HttpRequestScope UnsetProperty(string name)Parameters
namestring- The name of the property.
Returns
- HttpRequestScope
- The same HttpRequestScope instance for fluent chaining.
Exceptions
- ArgumentNullException
- Thrown if the
nameargument isnull.
