Skip to content

Commit dc7a9a5

Browse files
committed
feat: Support GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable.
Towards b/325308829
1 parent c96abd2 commit dc7a9a5

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

Src/Support/Google.Apis/Services/BaseClientService.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace Google.Apis.Services
4646
public abstract class BaseClientService : IClientService
4747
{
4848
private const string DefaultUniverseDomain = "googleapis.com";
49+
private const string UniverseDomainEnvironmentVariable = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";
4950

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

214215
/// <summary>
215-
/// The universe domain to connect to, or null to use the default universe domain <see cref="DefaultUniverseDomain"/>.
216+
/// The universe domain to connect to, or null to use the default universe domain,
217+
/// which may be configured via the <see cref="UniverseDomainEnvironmentVariable"/> .
216218
/// </summary>
217219
/// <remarks>
218220
/// <para>
@@ -229,6 +231,24 @@ public string UniverseDomain
229231
set;
230232
}
231233

234+
/// <summary>
235+
/// The configured universe domain, which is:
236+
/// <list type="bullet">
237+
/// <item>
238+
/// <see cref="UniverseDomain"/> if not null.
239+
/// </item>
240+
/// <item>
241+
/// Otherwise, the value of the environment variable with name <see cref="UniverseDomainEnvironmentVariable"/>
242+
/// if set to a non empty or blank-only value.
243+
/// </item>
244+
/// <item>
245+
/// Otherwise, null.
246+
/// </item>
247+
/// </list>
248+
/// </summary>
249+
private string EffectiveConfiguredUniverseDomain =>
250+
UniverseDomain ?? GetNonWhiteSpaceOrNullEnvironmentVariable(UniverseDomainEnvironmentVariable);
251+
232252
/// <summary>
233253
/// The timeout to set on <see cref="HttpClient"/> instances used by the service.
234254
/// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
@@ -251,7 +271,7 @@ private ConfigurableHttpClient CreateHttpClient(Initializer initializer, string
251271
GZipEnabled = GZipEnabled,
252272
ApplicationName = ApplicationName,
253273
GoogleApiClientHeader = versionHeader,
254-
UniverseDomain = UniverseDomain ?? DefaultUniverseDomain,
274+
UniverseDomain = EffectiveConfiguredUniverseDomain ?? DefaultUniverseDomain,
255275
};
256276

257277
// Add the user's input initializer.
@@ -301,16 +321,17 @@ protected virtual BackOffHandler CreateBackOffHandler()
301321
}
302322

303323
/// <summary>
304-
/// Gets the effective URI taking into account the <see cref="UniverseDomain"/>.
324+
/// Gets the effective URI taking into account the <see cref="UniverseDomain"/> and the value of
325+
/// the <see cref="UniverseDomainEnvironmentVariable"/>.
305326
/// </summary>
306327
/// <param name="explicitUri">An explicit URI. May be null.</param>
307328
/// <param name="defaultUri">A default URI. May be null.</param>
308329
/// <returns>
309330
/// <list type="bullet">
310331
/// <item><paramref name="explicitUri"/> if not null.</item>
311332
/// <item>
312-
/// Otherwise, if <see cref="UniverseDomain"/> is not null, the result of replacing
313-
/// <see cref="DefaultUniverseDomain"/> with <see cref="UniverseDomain"/>
333+
/// Otherwise, if <see cref="EffectiveConfiguredUniverseDomain"/> is not null, the result of replacing
334+
/// <see cref="DefaultUniverseDomain"/> with <see cref="EffectiveConfiguredUniverseDomain"/>
314335
/// in <paramref name="defaultUri"/>.
315336
/// </item>
316337
/// Otherwise <paramref name="defaultUri"/>.
@@ -321,7 +342,8 @@ protected internal string GetEffectiveUri(string explicitUri, string defaultUri)
321342
// it for the batch endpoint as well. The batch endpoint does not have an
322343
// override mechanism, so we pass explicitUri as null in that case.
323344
explicitUri ??
324-
(UniverseDomain is null ? defaultUri : defaultUri?.Replace(DefaultUniverseDomain, UniverseDomain));
345+
(EffectiveConfiguredUniverseDomain is string configureUniverseDomain ? defaultUri?.Replace(DefaultUniverseDomain, UniverseDomain) :
346+
defaultUri);
325347

326348
#region IClientService Members
327349

@@ -455,5 +477,15 @@ public virtual void Dispose()
455477
HttpClient.Dispose();
456478
}
457479
}
480+
481+
/// <summary>
482+
/// Retrieves the value of the environment variable with <paramref name="name"/>,
483+
/// mapping empty or whitespace-only strings to null.
484+
/// </summary>
485+
private static string GetNonWhiteSpaceOrNullEnvironmentVariable(string name)
486+
{
487+
var value = Environment.GetEnvironmentVariable(name);
488+
return string.IsNullOrWhiteSpace(value) ? null : value;
489+
}
458490
}
459491
}

0 commit comments

Comments
 (0)