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.09k stars 109 forks source link

"AddRestEaseClient" could register multiple interfaces for the same configuration? #178

Closed leonibr closed 3 years ago

leonibr commented 3 years ago

Hi, Is there a way that AddRestEaseClient could register multiple interfaces, something like:

.AddRestEaseClientMultiple()
    .Add<IAdminClient>()
    .Add<IStorePublic>()
   // all having the same configuration
    .Close()  // now returns to IHttpClientBuilder
.ConfigureHttpClient((service, client) => client.BaseAddress = Program.basePath)
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

Thank you for your time

canton7 commented 3 years ago

Which method would the params such as baseAddress, configurer and requestModifier be passed to?

leonibr commented 3 years ago

Sorry for the delay,. I think I didn't quite explain correctly. I usually create several clients like UserClient , ProfileClient and so on, and those clients share a common configuration settings like baseAddress and some common HttpMessageHandlers. So there is no way to pass multiple clients registration that will share some common configuration? I don't think you should create a new API for this, I thought there was something out of docs for this sugar, but thanks.

canton7 commented 3 years ago

There isn't currently. It wouldn't be hard to add something I think: you'd probably want to turn the API around a bit and call:

services.AddHttpClient(...).AddRestEaseClient<T1>().AddRestEaseClient<T2>()...;

You're basically just splitting this code out into its own extension method on IHttpClientBuilder.

You wouldn't be able to pass in a baseAddress or HttpMessageHandler to AddRestEaseClient of course: those need to be registered with the HttpClient, so you'd need to configure them there.

leonibr commented 3 years ago

ok, I see your point. Thank you again.

canton7 commented 3 years ago

Reopening, as I think this is probably something worth implementing.

RichMercer commented 3 years ago

I'm new to this library but I've created a factory class where I pass in the IApi type I need and then return the RestClient.For<T>(...) and handle all of my logic e.g. adding the Bearer token etc. so I can keep my code DRY. The solution above would really help with that, unless there's another way already to globally add adding headers?

canton7 commented 3 years ago

This issue won't be relevant to you if you're configuring a RestEase interface after constructing it, before returning it. It's just about if you're using HttpClientFactory and IServiceProvider.

If you've got other questions, please open another issue. Thanks!