-
Notifications
You must be signed in to change notification settings - Fork 11
Handling
Stanislav Molchanovskiy edited this page Jul 17, 2022
·
2 revisions
To add your own logic for executing requests, create your own implementation of the IClientHandler
interface and pass implementation to the WithHandling
method. For example you can implement authentication using handlers:
public class AuthHandler : IClientHandler
{
private readonly IConfiguration _configuration;
public AuthHandler(IConfiguration configuration)
=> _configuration = configuration;
public Task<HttpRequest> HandleRequestAsync(HttpRequest httpRequest, MethodInvocation methodInvocation)
{
httpRequest.AddHeader(name: "Authorization", value: _configuration["token"]);
return Task.FromResult(httpRequest);
}
public Task<IHttpResponse> HandleResponseAsync(IHttpResponse httpResponse, MethodInvocation methodInvocation)
=> Task.FromResult(httpResponse);
}
...
IConfiguration configuration = ...;
IMyClient client = NClientGallery.Clients.GetRest()
.For<IMyClient>(host: "http://localhost:8080")
.WithHandling(new IClientHandler[] { new AuthHandler(configuration) })
.Build();
The client can apply handlers in a strictly specified order, for this use IOrderedClientHandler<TRequest, TResponse>
interface.