Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: HttpClientFactory custom http message handlers #360

Open
Naxaliav opened this issue Aug 28, 2023 · 1 comment
Open

Question: HttpClientFactory custom http message handlers #360

Naxaliav opened this issue Aug 28, 2023 · 1 comment

Comments

@Naxaliav
Copy link

Naxaliav commented Aug 28, 2023

Hello,
do you have an example, how we can extend http client factory to add multiple http message handlers and keep current implementation of google http client factory? In essence, I have multiple handlers covering cross cutting concerns (traces, metrics, logs) which I am using with other typed http clients, I would like them to be reused. Ideally it would the best to resolve them from DI.

I have found similar issue in other library which uses google http client factory ref: googleapis/google-api-dotnet-client#1756, however some of the implementation is missing in current.

For now I have implemented as such (since I could not find any other possible way)

// configure handlers, so I could resolve through IHttpMessageHandlerFactory
builder.Services.AddHttpClient("Firebase")
    .ConfigurePrimaryHttpMessageHandler(_ => // some proxy configuration)
    .AddHttpMessageHandler<CorrelationIdHandler>()
    .AddHttpMessageHandler<LoggingHandler>()
    .AddHttpMessageHandler<TracingHandler>();

// override google http client factory
public class FirebaseHttpClientFactory : HttpClientFactory
{
      private readonly IHttpMessageHandlerFactory _httpMessageHandlerFactory;

      public FirebaseHttpClientFactory(IHttpMessageHandlerFactory httpMessageHandlerFactory)
      {
             _httpMessageHandlerFactory = httpMessageHandlerFactory
      }

      protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
      {
             return _httpMessageHandlerFactory.CreateHandler("Firebase");
      }
}

// register firebase messaging 
builder.Services.AddSingleton(sp =>  return FirebaseMessaging.GetMessaging(FirebaseApp.Create(new AppOptions{
     Credential = // load from file
     HttpClientFactory = new FirebaseHttpClientFactory(sp.GetRequiredService<IHttpMessageHandlerFactory>())
}));

By doing this, it kinda works, however I loose a lot of functionality from SDK itself, like gzip, user agent name setter and other initializers.

Can you please confirm that this is the only way currently how to achieve this and whether this will work?

I can see that other google library ref: googleapis/google-api-dotnet-client#1756 already decoupled itself from accepting implementation of google.HttpClientFactory to interface (what let's to use DI and pass multiple handlers).

Are you planning to make similar update to this library? If yes and you need help, please let me know :)

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants