TLDR; I need to configure HttpClients in .NET 8 with an API Key that differs for each/most requests, and the API key is found in the database based on some input id given from the caller. What is the best/ideomatic approach to this?
Some background:
We have a background service application (console app basically) that receives messages from a message bus. These messages contains a TenantId and a ConfigurationId.
Based on those two options, we need to find a set of configurations for an API. Specifically we need to find the right database (tenantId) and find the right configuration record (configurationId), which in turns gives us 2 things:
- An URL
- An API Key
Then we need to call the API and do some stuff with that.
I am strugling with the best approach for doing something like this in .NET 8.
Currently I have the following type of setup, but it seems “wrong” somehow, that I need to wrap my ApiClient as a factory class in this manner.
Factory Class
public class ApiClientFactory {
public ApiClientFactory(IServiceProvider provider, HttpClient client)
{
this.provider = provider;
this.client = client;
}
public IApiClient Create(Configuration config)
{
client.BaseAddress = config.Url;
client.DefaultRequestHeaders.Authorization = "Bearer {config.ApiKey}";
return ActivatorUtilities.CreateInstance<ApiClient>(this.provider, client);
}
}
And the DI setup looks something like:
services.AddHttpClient<ApiClientFactory>();
Then in our message handler class we do something like the following:
public class MessageBusHandler
{
public MessageBusHandler(ApiClientFactory factory)
{
this.factory = factory;
}
public void HandleMessage(CallApiMessage message)
{
// Find tenant/config from DB omitted...
var config = GetConfiguration(message.TenantId, message.ConfigurationId);
this.factory.Create(config).CallRemoteApi(message.SomeParameters);
}
}
All of this works…I just keep thinking that it would be better to refactor this into a message handler, so that I could somehow discard the ApiClientFactory since registering it as an HttpClient just seems wrong.
So are there other good options for creating multiple HttpClients for the same API but with different configurations (discovered runtime)?