Skip to content

feat: add HttpClientFactory option to HttpOptions for custom HttpClient configuration#234

Open
niimima wants to merge 3 commits intogoogleapis:mainfrom
niimima:add-http-client-factory
Open

feat: add HttpClientFactory option to HttpOptions for custom HttpClient configuration#234
niimima wants to merge 3 commits intogoogleapis:mainfrom
niimima:add-http-client-factory

Conversation

@niimima
Copy link

@niimima niimima commented Mar 9, 2026

Summary

Add HttpClientFactory to HttpOptions to allow callers to provide a custom HttpClient instance.

This enables advanced configuration scenarios such as:

  • custom message handlers
  • default headers
  • proxy settings
  • custom timeout policies
  • integration with IHttpClientFactory

Changes

  • Add Func<HttpClient>? HttpClientFactory property to HttpOptions
  • Mark the property with [JsonIgnore] to prevent serialization
  • Provide XML documentation for the new option

Motivation

In some environments, users need to control how HttpClient instances are created
(e.g., using IHttpClientFactory, custom handlers, or proxy settings).

This change allows consumers of the library to inject their own HttpClient
without modifying the internal HTTP pipeline.

Example

var options = new HttpOptions
{
    HttpClientFactory = () =>
    {
        var handler = new HttpClientHandler
        {
            Proxy = new WebProxy("http://proxy.example.com:8080"),
            UseProxy = true
        };

        return new HttpClient(handler);
    }
};

Compatibility

This change is backward compatible since the new property is optional.

@shivvaam0001
Copy link

Hi @niimima

Thank you for your contribution. Please check the failing checks and update the same.
Thanks!

# Conflicts:
#	Google.GenAI.Tests/HttpApiClientTest.cs
#	Google.GenAI/ApiClient.cs
@niimima niimima changed the title Add HttpClientFactory option to HttpOptions for custom HttpClient configuration feat: add HttpClientFactory option to HttpOptions for custom HttpClient configuration Mar 13, 2026
@niimima
Copy link
Author

niimima commented Mar 13, 2026

@shivvaam0001
Thanks for the feedback!

I've fixed the failing checks and updated the PR.
I noticed this change touches an auto-generated file, but I couldn't find the source/template. Please let me know if you'd prefer the change in a different location.

Please let me know if anything else needs to be addressed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants