-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(api_core): move universe and endpoint routing logic to gapic_v1 public helpers #17745
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
Open
hebaalazzeh
wants to merge
8
commits into
main
Choose a base branch
from
feat/gapic-centralization-api-core-routing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+368
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8808918
feat(api-core): move universe and endpoint routing logic to gapic_v1 …
hebaalazzeh 3c47f56
refactor(api-core): revert unrelated changes in noxfile and test_bidi
hebaalazzeh f953c80
refactor(api-core): update routing tests to match generator's test te…
hebaalazzeh 0fe63b5
fix(api-core): update get_default_mtls_endpoint to use robust string …
hebaalazzeh 2d0808b
docs(api-core): address routing review feedback on docstrings, types,…
hebaalazzeh 6596ac5
refactor(api-core): rename routing.py to client_utils.py as consolida…
hebaalazzeh 093ac26
fix(api-core): make mTLS endpoint conversion case-insensitive
hebaalazzeh 91d39e9
fix(api-core): satisfy lint and formatting for conftest and test_clie…
hebaalazzeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/google-api-core/google/api_core/gapic_v1/client_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| """Helpers for client setup and configuration.""" | ||
|
|
||
| from typing import Any, Optional | ||
|
|
||
| from google.auth.exceptions import MutualTLSChannelError # type: ignore | ||
|
|
||
|
|
||
| def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]: | ||
| """Converts api endpoint to mTLS endpoint. | ||
|
|
||
| Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to | ||
| "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively. | ||
|
|
||
| Args: | ||
| api_endpoint (Optional[str]): the api endpoint to convert. | ||
|
|
||
| Returns: | ||
| Optional[str]: converted mTLS api endpoint. | ||
| """ | ||
| if not api_endpoint or ".mtls." in api_endpoint.lower(): | ||
| return api_endpoint | ||
|
|
||
| lowered_endpoint = api_endpoint.lower() | ||
| if lowered_endpoint.endswith(".sandbox.googleapis.com"): | ||
| # len(".sandbox.googleapis.com") == 23 | ||
| return api_endpoint[:-23] + ".mtls.sandbox.googleapis.com" | ||
|
|
||
| if lowered_endpoint.endswith(".googleapis.com"): | ||
| # len(".googleapis.com") == 15 | ||
| return api_endpoint[:-15] + ".mtls.googleapis.com" | ||
|
|
||
| return api_endpoint | ||
|
|
||
|
|
||
| def get_api_endpoint( | ||
| api_override: Optional[str], | ||
| client_cert_source: Optional[Any], | ||
| universe_domain: str, | ||
| use_mtls_endpoint: str, | ||
| default_universe: str, | ||
| default_mtls_endpoint: Optional[str], | ||
| default_endpoint_template: str, | ||
| ) -> str: | ||
| """Return the API endpoint used by the client. | ||
|
|
||
| Args: | ||
| api_override (Optional[str]): The API endpoint override. If specified, | ||
| this is always returned. | ||
| client_cert_source (Optional[Any]): The client certificate source used by the client. | ||
| universe_domain (str): The universe domain used by the client. | ||
| use_mtls_endpoint (str): How to use the mTLS endpoint. Possible values | ||
| are "always", "auto", or "never". | ||
| default_universe (str): The default universe domain. | ||
| default_mtls_endpoint (Optional[str]): The default mTLS endpoint. | ||
| default_endpoint_template (str): The default endpoint template containing | ||
| a placeholder `{UNIVERSE_DOMAIN}`. | ||
|
|
||
| Returns: | ||
| str: The API endpoint to be used by the client. | ||
|
|
||
| Raises: | ||
| google.auth.exceptions.MutualTLSChannelError: If mTLS is requested but | ||
| not supported in the configured universe domain. | ||
| ValueError: If mTLS is requested but no mTLS endpoint is available. | ||
| """ | ||
| if api_override is not None: | ||
| return api_override | ||
| elif use_mtls_endpoint == "always" or ( | ||
| use_mtls_endpoint == "auto" and client_cert_source | ||
| ): | ||
| if universe_domain.lower() != default_universe.lower(): | ||
| raise MutualTLSChannelError( | ||
| f"mTLS is not supported in any universe other than {default_universe}." | ||
| ) | ||
| if not default_mtls_endpoint: | ||
| raise ValueError("mTLS endpoint is not available.") | ||
| return default_mtls_endpoint | ||
| else: | ||
| return default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain) | ||
|
|
||
|
|
||
| def get_universe_domain( | ||
| client_universe_domain: Optional[str], | ||
| universe_domain_env: Optional[str], | ||
| default_universe: str, | ||
| ) -> str: | ||
| """Return the universe domain used by the client. | ||
|
|
||
| Args: | ||
| client_universe_domain (Optional[str]): The universe domain configured | ||
| via client options. | ||
| universe_domain_env (Optional[str]): The universe domain configured | ||
| via environment variable. | ||
| default_universe (str): The default universe domain. | ||
|
|
||
| Returns: | ||
| str: The universe domain to be used by the client. | ||
|
|
||
| Raises: | ||
| ValueError: If the resolved universe domain is an empty string. | ||
| """ | ||
| if client_universe_domain is not None: | ||
| universe_domain = client_universe_domain.strip() | ||
| elif universe_domain_env is not None: | ||
| universe_domain = universe_domain_env.strip() | ||
| else: | ||
| universe_domain = default_universe | ||
|
|
||
| if not universe_domain: | ||
| raise ValueError("Universe Domain cannot be an empty string.") | ||
| return universe_domain | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def mock_mtls_env(): | ||
| """Autouse session-scoped fixture to isolate unit tests from workstation mTLS environments.""" | ||
| with mock.patch.dict( | ||
| os.environ, | ||
| { | ||
| "GOOGLE_API_USE_CLIENT_CERTIFICATE": "false", | ||
| "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false", | ||
| }, | ||
| ): | ||
| yield |
206 changes: 206 additions & 0 deletions
206
packages/google-api-core/tests/unit/gapic/test_client_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from google.api_core.gapic_v1.client_utils import ( | ||
| get_api_endpoint, | ||
| get_default_mtls_endpoint, | ||
| get_universe_domain, | ||
| ) | ||
| from google.auth.exceptions import MutualTLSChannelError | ||
|
|
||
|
|
||
| class MockClient: | ||
| _DEFAULT_UNIVERSE = "googleapis.com" | ||
| DEFAULT_MTLS_ENDPOINT = "foo.mtls.googleapis.com" | ||
| _DEFAULT_ENDPOINT_TEMPLATE = "foo.{UNIVERSE_DOMAIN}" | ||
|
|
||
|
|
||
| def test_get_default_mtls_endpoint(): | ||
| # Test valid API endpoints | ||
| assert get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com" | ||
| assert ( | ||
| get_default_mtls_endpoint("foo.sandbox.googleapis.com") | ||
| == "foo.mtls.sandbox.googleapis.com" | ||
| ) | ||
| # Test case-insensitivity | ||
| assert get_default_mtls_endpoint("foo.GoogleAPIs.com") == "foo.mtls.googleapis.com" | ||
| assert ( | ||
| get_default_mtls_endpoint("foo.Sandbox.GoogleAPIs.com") | ||
| == "foo.mtls.sandbox.googleapis.com" | ||
| ) | ||
|
|
||
| # Test endpoints that shouldn't be converted | ||
| assert ( | ||
| get_default_mtls_endpoint("foo.mtls.googleapis.com") | ||
| == "foo.mtls.googleapis.com" | ||
| ) | ||
| assert get_default_mtls_endpoint("foo.com") == "foo.com" | ||
|
|
||
| # Test empty/None endpoints | ||
| assert get_default_mtls_endpoint("") == "" | ||
| assert get_default_mtls_endpoint(None) is None | ||
|
|
||
|
|
||
| def test__get_api_endpoint(): | ||
| api_override = "foo.com" | ||
| mock_client_cert_source = mock.Mock() | ||
| default_universe = MockClient._DEFAULT_UNIVERSE | ||
| default_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format( | ||
| UNIVERSE_DOMAIN=default_universe | ||
| ) | ||
| mock_universe = "bar.com" | ||
| mock_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format( | ||
| UNIVERSE_DOMAIN=mock_universe | ||
| ) | ||
|
|
||
| assert ( | ||
| get_api_endpoint( | ||
| api_override, | ||
| mock_client_cert_source, | ||
| default_universe, | ||
| "always", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == api_override | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| mock_client_cert_source, | ||
| default_universe, | ||
| "auto", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == MockClient.DEFAULT_MTLS_ENDPOINT | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| None, | ||
| default_universe, | ||
| "auto", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == default_endpoint | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| None, | ||
| default_universe, | ||
| "always", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == MockClient.DEFAULT_MTLS_ENDPOINT | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| mock_client_cert_source, | ||
| default_universe, | ||
| "always", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == MockClient.DEFAULT_MTLS_ENDPOINT | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| None, | ||
| mock_universe, | ||
| "never", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == mock_endpoint | ||
| ) | ||
| assert ( | ||
| get_api_endpoint( | ||
| None, | ||
| None, | ||
| default_universe, | ||
| "never", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| == default_endpoint | ||
| ) | ||
|
|
||
| with pytest.raises(MutualTLSChannelError) as excinfo: | ||
| get_api_endpoint( | ||
| None, | ||
| mock_client_cert_source, | ||
| mock_universe, | ||
| "auto", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| MockClient.DEFAULT_MTLS_ENDPOINT, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| assert ( | ||
| str(excinfo.value) | ||
| == "mTLS is not supported in any universe other than googleapis.com." | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError) as excinfo: | ||
| get_api_endpoint( | ||
| None, | ||
| mock_client_cert_source, | ||
| default_universe, | ||
| "always", | ||
| MockClient._DEFAULT_UNIVERSE, | ||
| None, | ||
| MockClient._DEFAULT_ENDPOINT_TEMPLATE, | ||
| ) | ||
| assert str(excinfo.value) == "mTLS endpoint is not available." | ||
|
|
||
|
|
||
| def test__get_universe_domain(): | ||
| client_universe_domain = "foo.com" | ||
| universe_domain_env = "bar.com" | ||
|
|
||
| assert ( | ||
| get_universe_domain( | ||
| client_universe_domain, universe_domain_env, MockClient._DEFAULT_UNIVERSE | ||
| ) | ||
| == client_universe_domain | ||
| ) | ||
| assert ( | ||
| get_universe_domain(None, universe_domain_env, MockClient._DEFAULT_UNIVERSE) | ||
| == universe_domain_env | ||
| ) | ||
| assert ( | ||
| get_universe_domain(None, None, MockClient._DEFAULT_UNIVERSE) | ||
| == MockClient._DEFAULT_UNIVERSE | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError) as excinfo: | ||
| get_universe_domain("", None, MockClient._DEFAULT_UNIVERSE) | ||
| assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.