firebase-admin-dotnet icon indicating copy to clipboard operation
firebase-admin-dotnet copied to clipboard

Replace HttpClientFactory as interface

Open gsino opened this issue 3 years ago • 7 comments

Hello.

In the GoogleApi repository, an implementation of Google.Apis.Http.IHttpClientFactory using Microsoft.Extensions.Http.IHttpMessageHandlerFactory is presented.

https://github.com/googleapis/google-api-dotnet-client/issues/1756

https://github.com/googleapis/google-api-dotnet-client/blob/bfc9090dc2fae5b06a654328c4da7bc6b05b66f1/Src/Support/IntegrationTests/HttpClientFromMessageHandlerFactoryTests.cs#L38

To apply this method to Firebase as well, change the type of the AppOptions.HttpClientFactory property from Google.Apis.Http.HttpClientFactory to Google.Apis.Http.IHttpClientFactory?

gsino avatar Mar 03 '21 05:03 gsino

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.

google-oss-bot avatar Mar 03 '21 05:03 google-oss-bot

@gsino can you add some example use cases or code samples to this issue to elaborate how this can be used with Firebase? Wouldn't you be able to achieve the same result with something like https://github.com/firebase/firebase-admin-dotnet/blob/master/FirebaseAdmin/FirebaseAdmin.Tests/MockHttpClientFactory.cs?

hiranya911 avatar Mar 03 '21 19:03 hiranya911

Wouldn't you be able to achieve the same result with something like https://github.com/firebase/firebase-admin-dotnet/blob/master/FirebaseAdmin/FirebaseAdmin.Tests/MockHttpClientFactory.cs?

Half Yes. In fact, I can get any HttpMessageHandler by injecting System.Net.Http.IHttpMessageHandlerFactory.

public class MyHttpClientFactory : Google.Apis.Http.HttpClientFactory
{
    private readonly System.Net.Http.IHttpMessageHandlerFactory _httpMessageHandlerFactory;

    public MyHttpClientFactory (System.Net.Http.IHttpMessageHandlerFactory httpMessageHandlerFactory)
    {
        _httpMessageHandlerFactory = httpMessageHandlerFactory;
    }

    protected override HttpMessageHandler CreateHandler(Google.Apis.Http.CreateHttpClientArgs args)
    {
        return _httpMessageHandlerFactory.CreateHandler("MyHttpClientFactory");
    }
}

However, compared to Google.Apis.Http.HttpClientFactory, it lacks the following two features:

  1. Support for CreateHttpClientArgs
  2. Support for StreamInterceptionHandler

For Google.Apis.Http.HttpClientFromMessageHandlerFactory, both features are supported.

If the type of the AppOptions.HttpClientFactory property is Google.Apis.Http.IHttpClientFactory, you can set Google.Apis.HttpHttpClientFromMessageHandlerFactory in addition to Google.Apis.Http.HttpClientFactory.

public sealed class AppOptions
{
    public IHttpClientFactory HttpClientFactory { get; set; }
}
var defaultHttpOption = new AppOptions
{
   HttpClientFactory = new HttpClientFactory(),
}
var customHttpOption1 = new AppOptions
{
   HttpClientFactory = new HttpHttpClientFromMessageHandlerFactory(...),
}
//or use DI Container
var customHttpOption2 = new AppOptions
{
   HttpClientFactory = serviceProvider.GetService<IHttpClientFactory>(),
}

gsino avatar Mar 05 '21 05:03 gsino

Fair enough. We can look into making this change in a future release (shouldn't be a breaking change as far as I can tell).

hiranya911 avatar Mar 05 '21 22:03 hiranya911

Thank you! :)

gsino avatar Mar 08 '21 03:03 gsino

@hiranya911 Any updates on this? We'd really like to use .NET's IHttpClientFactory to manage the underlying http connections for efficiency. Since this is already supported in other Google APIs, and the pattern is now the universal best-practice for all modern .NET applications, it would be great for the Firebase SDK to support it as well. Thanks!

mikequ-taggysoft avatar Nov 23 '21 04:11 mikequ-taggysoft

Are there any updated on this. or any other methods of optimizing the HTTP client usage on FireBase Messaging?

tiaan-lg avatar Jul 18 '23 10:07 tiaan-lg