Could we have .WithBaseUrl() option?
Closed this issue · 6 comments
Hi,
I have a number of services that I use which share the same interface but use different base addresses. For instance a Live and Test service. There are reasons for the system to communicate with both systems rather than just one or the other.
Unless I am missing something we either have to set the baseurl at the time of the client creation, or override the entire Url in the call using .WithUrl()
It would seem to be so much simpler to have a .WithBaseUrl() option to just override the base address for this, and looking at the code it seems like it would be simple enough to implement.
At present the options for this appear to be:
- 'destroy' and re-create the apiclient after each call
- create multiple ApiClients in a dictionary to get the correct one for the call
- Override the full Url for each call (many endpoints though - annoying to keep track).
These all seem suboptimal compared to having a .WithBaseUrl() option.
Thoughts?
Hi @AndrewZenith
Thank you for using kiota and for reaching out.
Have you tried setting the base url of the request adapter of your client
Let us know if you have any additional comments or questions.
Hi,
That's what I'm already doing. As I mentioned, you can't change it once the ApiClient is created, leading to the question of having a .WithBaseUrl() option.
Thank you for the additional information.
Have you considered having multiple request adapters and clients? and switching between them instead? If so, what are the challenges it posses?
Generally speaking, the request adapter holds all the configuration for the client. (auth, serialization, base url, etc...)
Hi,
At present I am using my option (2) - create multiple ApiClients in a dictionary to get the correct one for the call
I was rather hoping to just be able to have a single ApiClient rather than multiple, everything else other than the BaseUrl seems configurable via the RequestOptions.
You could achieve that via a custom middleware handler and the associated option.
But I question whether this is desirable: you'll effectively end up mixing the environment switching logic with the application logic.
Instead this could/should be handled by the configuration layer/through dependency injection.
Hi,
I'm building the clients through a factory as I could potentially need to add a new endpoint address and I want to do that dynamically whilst the application is running if needed.
If I didn't do that then I can see I should have all the services in startup to support each client, which would mean having multiple clients anyway.
So, I'm using a dictionary of clients in my single service - and yes, that way I don't have to change the BaseUrl for each usage of the client.
So I'm good with what I have.