Skip to content

fix: retrieve the live discovery document from the client's universe domain - #2862

Merged
daniel-sanche merged 12 commits into
mainfrom
b588887517_python_track_one_google-api-python-client_01_live-discovery-universe
Dec 4, 2026
Merged

daniel-sanche merged 12 commits into
mainfrom
b588887517_python_track_one_google-api-python-client_01_live-discovery-universe

Conversation

@daniel-sanche

Copy link
Copy Markdown
Contributor

When discovery.build() is called with static_discovery=False, fetch the discovery document from the configured universe_domain instead of the default universe, and validate any credentials attached to a caller-supplied http transport before making the request.

Mismatched credentials or invalid universe configuration now raise UniverseMismatchError, EmptyUniverseError, or APICoreVersionError (on google-api-core < 2.18.0) before any discovery HTTP request is sent.

Shared universe helpers were moved to googleapiclient._universe, preferring google.api_core.universe (>= 2.45.0) with fallbacks for older google-api-core versions.

@product-auto-label product-auto-label Bot added the size: l Pull request size is large. label Dec 4, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new helper module _universe.py to handle universe domain compatibility, preferring google.api_core.universe when available and falling back to local implementations for older google-api-core versions. It integrates these helpers into discovery.py to resolve the universe domain before making discovery requests and to validate credentials. Comprehensive unit tests are also added in test__universe.py and test_discovery.py to cover various scenarios with and without universe support. I have no feedback to provide.

@product-auto-label product-auto-label Bot added size: xl Pull request size is extra large. and removed size: l Pull request size is large. labels Dec 4, 2026
@daniel-sanche
daniel-sanche marked this pull request as ready for review December 4, 2026 02:39
@daniel-sanche
daniel-sanche requested a review from a team as a code owner December 4, 2026 02:39
@daniel-sanche daniel-sanche changed the title [DRAFT] fix: retrieve the live discovery document from the client's universe domain fix: retrieve the live discovery document from the client's universe domain Dec 4, 2026

@chalmerlowe chalmerlowe left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this PR! The _universe.py module, pre-request validation (including uncached GCE credentials), and test coverage for older google-api-core versions are super clean. Left a few small suggestions below to align whitespace handling with our shared universe_domain helpers and tidy up a couple of nits.

Comment on lines +85 to +97
):
raise APICoreVersionError
return None
return api_core_universe.determine_domain(
options_universe,
env_universe,
)


def check_api_core_compatible_with_credentials_universe(credentials):
"""Raise APICoreVersionError if credentials require universe support."""
if not HAS_UNIVERSE:
credentials_universe = getattr(credentials, "universe_domain", None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider stripping extra spaces from options_universe and GOOGLE_CLOUD_UNIVERSE_DOMAIN before checking them and calling determine_domain().

That way, values with accidental spaces (like " foo.com " or " ") behave the same way even on older google-api-core releases (2.18.0–2.44.0), and "googleapis.com " won't accidentally trigger APICoreVersionError when HAS_UNIVERSE is False.

Suggested change
):
raise APICoreVersionError
return None
return api_core_universe.determine_domain(
options_universe,
env_universe,
)
def check_api_core_compatible_with_credentials_universe(credentials):
"""Raise APICoreVersionError if credentials require universe support."""
if not HAS_UNIVERSE:
credentials_universe = getattr(credentials, "universe_domain", None)
# Strip before checking/determine_domain so surrounding whitespace is
# removed from non-empty values even on older google-api-core releases.
stripped_options = (
options_universe.strip()
if isinstance(options_universe, str)
else options_universe
)
env_universe = os.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN)
stripped_env = (
env_universe.strip() if isinstance(env_universe, str) else env_universe
)
if not HAS_UNIVERSE:
# Older google-api-core (< 2.18.0) cannot route to non-default
# universes; fail before any request if a universe is configured.
if stripped_options is not None or (
stripped_env is not None and stripped_env != DEFAULT_UNIVERSE
):
raise APICoreVersionError
return None
return api_core_universe.determine_domain(
stripped_options,
stripped_env,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Comment on lines +271 to +273
google.api_core.universe.UniverseMismatchError: if `static_discovery` is
False and the credentials of `http` or `credentials` belong to a
different universe domain than the client.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since build() can also raise EmptyUniverseError and APICoreVersionError before fetching the discovery doc (as noted in the PR description), let's list them here too.

Suggested change
google.api_core.universe.UniverseMismatchError: if `static_discovery` is
False and the credentials of `http` or `credentials` belong to a
different universe domain than the client.
google.api_core.universe.UniverseMismatchError: if `static_discovery` is
False and the credentials of `http` or `credentials` belong to a
different universe domain than the client.
google.api_core.universe.EmptyUniverseError: if the configured universe
domain is empty.
googleapiclient._universe.APICoreVersionError: if universe features
are configured with `google-api-core < 2.18.0`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch


# Resolve the universe domain before any request is made, so that the
# discovery document is retrieved from the same universe as the API.
universe_domain = _universe.resolve_client_universe(client_options)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: When static_discovery=True (the default), build() resolves universe_domain here and then build_from_document() resolves it a second time below. Totally harmless (since it still validates client_options early), just flagging in case you want to note or skip it.

Comment thread googleapiclient/discovery.py Outdated
rootDesc,
schema,
universe_domain=universe.DEFAULT_UNIVERSE if HAS_UNIVERSE else "",
universe_domain=DEFAULT_UNIVERSE if HAS_UNIVERSE else "",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The rest of discovery.py uses _universe.HAS_UNIVERSE instead of a separate HAS_UNIVERSE import—switching this line too keeps it consistent.

Suggested change
universe_domain=DEFAULT_UNIVERSE if HAS_UNIVERSE else "",
universe_domain=DEFAULT_UNIVERSE if _universe.HAS_UNIVERSE else "",

@chalmerlowe chalmerlowe left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments.

@daniel-sanche
daniel-sanche added this pull request to stack #2865 December 4, 2026 20:32
@daniel-sanche
daniel-sanche merged commit 9c50dc5 into main Dec 4, 2026
18 checks passed
@daniel-sanche
daniel-sanche deleted the b588887517_python_track_one_google-api-python-client_01_live-discovery-universe branch December 4, 2026 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: xl Pull request size is extra large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants