Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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.

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.