From c5fef81749b3cb59073d009e2f547ea5dad67525 Mon Sep 17 00:00:00 2001 From: brysky9654 Date: Tue, 1 Jun 2021 16:08:51 +0100 Subject: [PATCH] feat: Expose Google.Apis.Http.IHttpClientFactory on Rest.ClientBuilderBase This is so that client code can customize HTTP clients used. Closes googleapis/google-api-dotnet-client#1864 --- .../ClientBuilderBaseTest.cs | 8 ++++++ Google.Api.Gax.Rest/ClientBuilderBase.cs | 28 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Google.Api.Gax.Rest.IntegrationTests/ClientBuilderBaseTest.cs b/Google.Api.Gax.Rest.IntegrationTests/ClientBuilderBaseTest.cs index 2ffab48..08ec540 100644 --- a/Google.Api.Gax.Rest.IntegrationTests/ClientBuilderBaseTest.cs +++ b/Google.Api.Gax.Rest.IntegrationTests/ClientBuilderBaseTest.cs @@ -240,6 +240,14 @@ public void InvalidCombination(SampleClientBuilder builder) Assert.Throws(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 validator) { var initializer = builder.Build(); diff --git a/Google.Api.Gax.Rest/ClientBuilderBase.cs b/Google.Api.Gax.Rest/ClientBuilderBase.cs index 1f0d75d..edb14c4 100644 --- a/Google.Api.Gax.Rest/ClientBuilderBase.cs +++ b/Google.Api.Gax.Rest/ClientBuilderBase.cs @@ -64,6 +64,31 @@ public abstract class ClientBuilderBase /// public string QuotaProject { get; set; } + /// + /// An that will be used to obtain + /// for making API Http calls. + /// May be null, in which case an + /// will be used. + /// + /// + /// If you want to use custom HTTP clients, for instance, if you need to set a proxy, + /// you may do so by either + /// + /// + /// Extending . Refer to + /// documentation for more information. + /// + /// + /// On .NET Core 2.1 and above, using + /// in combination with System.Net.Http.IHttpClientFactory. Refer to + /// documentation and + /// https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests + /// for more information. + /// + /// + /// + public IHttpClientFactory HttpClientFactory { get; set; } + /// /// Creates a new instance with no settings. /// @@ -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())