Repository navigation
fix: retrieve the live discovery document from the client's universe domain - #2862
Conversation
There was a problem hiding this comment.
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.
chalmerlowe
left a comment
There was a problem hiding this comment.
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.
| ): | ||
| 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) |
There was a problem hiding this comment.
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.
| ): | |
| 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, | |
| ) |
| 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. |
There was a problem hiding this comment.
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.
| 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`. |
|
|
||
| # 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) |
There was a problem hiding this comment.
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.
| rootDesc, | ||
| schema, | ||
| universe_domain=universe.DEFAULT_UNIVERSE if HAS_UNIVERSE else "", | ||
| universe_domain=DEFAULT_UNIVERSE if HAS_UNIVERSE else "", |
There was a problem hiding this comment.
nit: The rest of discovery.py uses _universe.HAS_UNIVERSE instead of a separate HAS_UNIVERSE import—switching this line too keeps it consistent.
| universe_domain=DEFAULT_UNIVERSE if HAS_UNIVERSE else "", | |
| universe_domain=DEFAULT_UNIVERSE if _universe.HAS_UNIVERSE else "", |
When
discovery.build()is called withstatic_discovery=False, fetch the discovery document from the configureduniverse_domaininstead of the default universe, and validate any credentials attached to a caller-suppliedhttptransport before making the request.Mismatched credentials or invalid universe configuration now raise
UniverseMismatchError,EmptyUniverseError, orAPICoreVersionError(ongoogle-api-core < 2.18.0) before any discovery HTTP request is sent.Shared universe helpers were moved to
googleapiclient._universe, preferringgoogle.api_core.universe(>= 2.45.0) with fallbacks for oldergoogle-api-coreversions.