Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable. #2913

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading