Skip to content

Commit

Permalink
feat: Expose Google.Apis.Http.IHttpClientFactory on Rest.ClientBuilde…
Browse files Browse the repository at this point in the history
…rBase

This is so that client code can customize HTTP clients used.
Closes googleapis/google-api-dotnet-client#1864
  • Loading branch information
smartjoker0117 committed Jun 1, 2021
1 parent 7641114 commit c5fef81
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Google.Api.Gax.Rest.IntegrationTests/ClientBuilderBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ public void InvalidCombination(SampleClientBuilder builder)
Assert.Throws<InvalidOperationException>(builder.ValidateForTest);
}

[Fact]
public async Task HttpClientFactory()
{
var factory = new HttpClientFactory();
var builder = new SampleClientBuilder { HttpClientFactory = factory };
await ValidateResultAsync(builder, initializer => Assert.Same(factory, initializer.HttpClientFactory));
}

private async Task ValidateResultAsync(SampleClientBuilder builder, Action<BaseClientService.Initializer> validator)
{
var initializer = builder.Build();
Expand Down
28 changes: 27 additions & 1 deletion Google.Api.Gax.Rest/ClientBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ public abstract class ClientBuilderBase<TClient>
/// </summary>
public string QuotaProject { get; set; }

/// <summary>
/// An <see cref="IHttpClientFactory"/> that will be used to obtain
/// <see cref="ConfigurableHttpClient"/> for making API Http calls.
/// May be null, in which case an <see cref="Apis.Http.HttpClientFactory"/>
/// will be used.
/// </summary>
/// <remarks>
/// If you want to use custom HTTP clients, for instance, if you need to set a proxy,
/// you may do so by either
/// <list type="bullet">
/// <item>
/// Extending <see cref="Apis.Http.HttpClientFactory"/>. Refer to
/// <see cref="Apis.Http.HttpClientFactory" /> documentation for more information.
/// </item>
/// <item>
/// On .NET Core 2.1 and above, using <see cref="HttpClientFromMessageHandlerFactory"/>
/// in combination with <code>System.Net.Http.IHttpClientFactory</code>. Refer to
/// <see cref="HttpClientFromMessageHandlerFactory"/> documentation and
/// https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
/// for more information.
/// </item>
/// </list>
/// </remarks>
public IHttpClientFactory HttpClientFactory { get; set; }

/// <summary>
/// Creates a new instance with no settings.
/// </summary>
Expand Down Expand Up @@ -118,7 +143,8 @@ private BaseClientService.Initializer CreateServiceInitializerImpl(IConfigurable
HttpClientInitializer = clientInitializer,
ApiKey = ApiKey,
ApplicationName = ApplicationName ?? GetDefaultApplicationName(),
BaseUri = BaseUri
BaseUri = BaseUri,
HttpClientFactory = HttpClientFactory
};
initializer.VersionHeaderBuilder
.AppendAssemblyVersion("gccl", GetType())
Expand Down

0 comments on commit c5fef81

Please sign in to comment.