Skip to content

Commit

Permalink
feat: Support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable.
Browse files Browse the repository at this point in the history
Towards b/325308829
  • Loading branch information
amanda-tarafa committed Jan 14, 2025
1 parent c96abd2 commit dc7a9a5
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions Src/Support/Google.Apis/Services/BaseClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>The class logger.</summary>
private static readonly ILogger Logger = ApplicationContext.Logger.ForType<BaseClientService>();
Expand Down Expand Up @@ -212,7 +213,8 @@ protected BaseClientService(Initializer initializer)
protected string BaseUriOverride { get; }

/// <summary>
/// The universe domain to connect to, or null to use the default universe domain <see cref="DefaultUniverseDomain"/>.
/// The universe domain to connect to, or null to use the default universe domain,
/// which may be configured via the <see cref="UniverseDomainEnvironmentVariable"/> .
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -229,6 +231,24 @@ public string UniverseDomain
set;
}

/// <summary>
/// The configured universe domain, which is:
/// <list type="bullet">
/// <item>
/// <see cref="UniverseDomain"/> if not null.
/// </item>
/// <item>
/// Otherwise, the value of the environment variable with name <see cref="UniverseDomainEnvironmentVariable"/>
/// if set to a non empty or blank-only value.
/// </item>
/// <item>
/// Otherwise, null.
/// </item>
/// </list>
/// </summary>
private string EffectiveConfiguredUniverseDomain =>
UniverseDomain ?? GetNonWhiteSpaceOrNullEnvironmentVariable(UniverseDomainEnvironmentVariable);

/// <summary>
/// The timeout to set on <see cref="HttpClient"/> instances used by the service.
/// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
Expand All @@ -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.
Expand Down Expand Up @@ -301,16 +321,17 @@ protected virtual BackOffHandler CreateBackOffHandler()
}

/// <summary>
/// Gets the effective URI taking into account the <see cref="UniverseDomain"/>.
/// Gets the effective URI taking into account the <see cref="UniverseDomain"/> and the value of
/// the <see cref="UniverseDomainEnvironmentVariable"/>.
/// </summary>
/// <param name="explicitUri">An explicit URI. May be null.</param>
/// <param name="defaultUri">A default URI. May be null.</param>
/// <returns>
/// <list type="bullet">
/// <item><paramref name="explicitUri"/> if not null.</item>
/// <item>
/// Otherwise, if <see cref="UniverseDomain"/> is not null, the result of replacing
/// <see cref="DefaultUniverseDomain"/> with <see cref="UniverseDomain"/>
/// Otherwise, if <see cref="EffectiveConfiguredUniverseDomain"/> is not null, the result of replacing
/// <see cref="DefaultUniverseDomain"/> with <see cref="EffectiveConfiguredUniverseDomain"/>
/// in <paramref name="defaultUri"/>.
/// </item>
/// Otherwise <paramref name="defaultUri"/>.
Expand All @@ -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

Expand Down Expand Up @@ -455,5 +477,15 @@ public virtual void Dispose()
HttpClient.Dispose();
}
}

/// <summary>
/// Retrieves the value of the environment variable with <paramref name="name"/>,
/// mapping empty or whitespace-only strings to null.
/// </summary>
private static string GetNonWhiteSpaceOrNullEnvironmentVariable(string name)
{
var value = Environment.GetEnvironmentVariable(name);
return string.IsNullOrWhiteSpace(value) ? null : value;
}
}
}

0 comments on commit dc7a9a5

Please sign in to comment.