From dc7a9a591b1f6e74aa4aaf3b79a3206ba7fdd09f Mon Sep 17 00:00:00 2001 From: Amanda Tarafa Mas Date: Tue, 14 Jan 2025 13:54:51 -0800 Subject: [PATCH] feat: Support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable. Towards b/325308829 --- .../Google.Apis/Services/BaseClientService.cs | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Src/Support/Google.Apis/Services/BaseClientService.cs b/Src/Support/Google.Apis/Services/BaseClientService.cs index fb8121babd3..d38567cf4dd 100644 --- a/Src/Support/Google.Apis/Services/BaseClientService.cs +++ b/Src/Support/Google.Apis/Services/BaseClientService.cs @@ -46,6 +46,7 @@ namespace Google.Apis.Services public abstract class BaseClientService : IClientService { private const string DefaultUniverseDomain = "googleapis.com"; + private const string UniverseDomainEnvironmentVariable = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"; /// The class logger. private static readonly ILogger Logger = ApplicationContext.Logger.ForType(); @@ -212,7 +213,8 @@ protected BaseClientService(Initializer initializer) protected string BaseUriOverride { get; } /// - /// The universe domain to connect to, or null to use the default universe domain . + /// The universe domain to connect to, or null to use the default universe domain, + /// which may be configured via the . /// /// /// @@ -229,6 +231,24 @@ public string UniverseDomain set; } + /// + /// The configured universe domain, which is: + /// + /// + /// if not null. + /// + /// + /// Otherwise, the value of the environment variable with name + /// if set to a non empty or blank-only value. + /// + /// + /// Otherwise, null. + /// + /// + /// + private string EffectiveConfiguredUniverseDomain => + UniverseDomain ?? GetNonWhiteSpaceOrNullEnvironmentVariable(UniverseDomainEnvironmentVariable); + /// /// The timeout to set on instances used by the service. /// May be null, in which case the default timeout values on instances @@ -251,7 +271,7 @@ private ConfigurableHttpClient CreateHttpClient(Initializer initializer, string GZipEnabled = GZipEnabled, ApplicationName = ApplicationName, GoogleApiClientHeader = versionHeader, - UniverseDomain = UniverseDomain ?? DefaultUniverseDomain, + UniverseDomain = EffectiveConfiguredUniverseDomain ?? DefaultUniverseDomain, }; // Add the user's input initializer. @@ -301,7 +321,8 @@ protected virtual BackOffHandler CreateBackOffHandler() } /// - /// Gets the effective URI taking into account the . + /// Gets the effective URI taking into account the and the value of + /// the . /// /// An explicit URI. May be null. /// A default URI. May be null. @@ -309,8 +330,8 @@ protected virtual BackOffHandler CreateBackOffHandler() /// /// if not null. /// - /// Otherwise, if is not null, the result of replacing - /// with + /// Otherwise, if is not null, the result of replacing + /// with /// in . /// /// Otherwise . @@ -321,7 +342,8 @@ protected internal string GetEffectiveUri(string explicitUri, string defaultUri) // it for the batch endpoint as well. The batch endpoint does not have an // override mechanism, so we pass explicitUri as null in that case. explicitUri ?? - (UniverseDomain is null ? defaultUri : defaultUri?.Replace(DefaultUniverseDomain, UniverseDomain)); + (EffectiveConfiguredUniverseDomain is string configureUniverseDomain ? defaultUri?.Replace(DefaultUniverseDomain, UniverseDomain) : + defaultUri); #region IClientService Members @@ -455,5 +477,15 @@ public virtual void Dispose() HttpClient.Dispose(); } } + + /// + /// Retrieves the value of the environment variable with , + /// mapping empty or whitespace-only strings to null. + /// + private static string GetNonWhiteSpaceOrNullEnvironmentVariable(string name) + { + var value = Environment.GetEnvironmentVariable(name); + return string.IsNullOrWhiteSpace(value) ? null : value; + } } }