canton7 / RestEase

Easy-to-use typesafe REST API client library for .NET Standard 1.1 and .NET Framework 4.5 and higher, which is simple and customisable. Inspired by Refit
MIT License
1.08k stars 109 forks source link

Specifying a RequestModifier when using RestEase.HttpClientFactory #164

Closed wizhi closed 3 years ago

wizhi commented 4 years ago

I'm in need of a way of setting the Authorization header for requests, which is handled quite neatly using a RequestModifier. This doesn't seem to be possible, however, if I'm registering my service using the RestEase.HttpClientFactory abstraction, since this doesn't expose the same arguments as RestClient.For<T>.

I'm not seeing any way of specifying a RequestModifier for clients, registered through any of the IServiceCollection.AddRestEaseClient<T>() overloads.

Am I missing some way of using the RestEase.HttpClientFactory abstraction with a custom RequestModifier?

If not, what would be the recommended approach to doing something similar?

canton7 commented 4 years ago

That's a fair request. That will be in the next version, but I can't say when that will be released.

In the meantime, you should be able to do:

services.AddRestEaseClient<T>(...).ConfigurePrimaryHttpMessageHandler(() => new ModifyingClientHttpHandler(requestModifier));

(Let me know if that doesn't work, as I'll need to change how I've implemented it!)