Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit c85fdd4

Browse files
committed
fix: use default_host even with custom endpoints
Following guidance in https://docs.google.com/document/d/1FoJPYQu8ZHdGBFY9TBRZCDrWJJnxw3fAz5CG5mdZR9E/edit
1 parent b3409cf commit c85fdd4

2 files changed

Lines changed: 12 additions & 80 deletions

File tree

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class {{ service.name }}Transport(abc.ABC):
6767
scopes: Optional[Sequence[str]] = None,
6868
quota_project_id: Optional[str] = None,
6969
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
70-
**kwargs,
70+
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
7373

@@ -85,10 +85,10 @@ class {{ service.name }}Transport(abc.ABC):
8585
scopes (Optional[Sequence[str]]): A list of scopes.
8686
quota_project_id (Optional[str]): An optional project to use for billing
8787
and quota.
88-
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
89-
The client info used to send a user-agent string along with
90-
API requests. If ``None``, then default info will be used.
91-
Generally, you only need to set this if you're developing
88+
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
89+
The client info used to send a user-agent string along with
90+
API requests. If ``None``, then default info will be used.
91+
Generally, you only need to set this if you're developing
9292
your own client library.
9393
"""
9494
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
@@ -99,13 +99,13 @@ class {{ service.name }}Transport(abc.ABC):
9999
scopes_kwargs = self._get_scopes_kwargs(self._host, scopes)
100100

101101
# Save the scopes.
102-
self._scopes = scopes_kwargs["scopes"]
102+
self._scopes = scopes or self.AUTH_SCOPES
103103

104104
# If no credentials are provided, then determine the appropriate
105105
# defaults.
106106
if credentials and credentials_file:
107107
raise exceptions.DuplicateCredentialArgs("'credentials_file' and 'credentials' are mutually exclusive")
108-
108+
109109
if credentials_file is not None:
110110
credentials, _ = auth.load_credentials_from_file(
111111
credentials_file,
@@ -120,19 +120,7 @@ class {{ service.name }}Transport(abc.ABC):
120120
self._credentials = credentials
121121

122122

123-
@classmethod
124-
def _get_user_scopes(cls, host: str, scopes: Optional[Sequence[str]]) -> Optional[Sequence[str]]:
125-
if scopes is None:
126-
# If a custom API endpoint is set, set user scopes to ensure the auth
127-
# library does not used the self-signed JWT flow for service
128-
# accounts.
129-
if host.split(":")[0] != cls.DEFAULT_HOST.split(":")[0]:
130-
scopes = cls.AUTH_SCOPES
131-
132-
return scopes
133-
134-
135-
# TODO(busunkim): These two class methods are in the base transport
123+
# TODO(busunkim): These two class methods are in the base transport
136124
# to avoid duplicating code across the transport classes. These functions
137125
# should be deleted once the minimum required versions of google-api-core
138126
# and google-auth are increased.
@@ -142,7 +130,6 @@ class {{ service.name }}Transport(abc.ABC):
142130
def _get_scopes_kwargs(cls, host: str, scopes: Optional[Sequence[str]]) -> Dict[str, Optional[Sequence[str]]]:
143131
"""Returns scopes kwargs to pass to google-auth methods depending on the google-auth version"""
144132

145-
scopes = cls._get_user_scopes(host, scopes)
146133
scopes_kwargs = {}
147134

148135
if _GOOGLE_AUTH_VERSION and (
@@ -152,17 +139,16 @@ class {{ service.name }}Transport(abc.ABC):
152139
scopes_kwargs = {"scopes": scopes, "default_scopes": cls.AUTH_SCOPES}
153140
else:
154141
scopes_kwargs = {"scopes": scopes or cls.AUTH_SCOPES}
155-
142+
156143
return scopes_kwargs
157144

158145
# TODO: Remove this function once google-api-core >= 1.26.0 is required
159146
@classmethod
160147
def _get_self_signed_jwt_kwargs(cls, host: str, scopes: Optional[Sequence[str]]) -> Dict[str, Union[Optional[Sequence[str]], str]]:
161148
"""Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""
162-
scopes = cls._get_user_scopes(host, scopes)
163149

164150
self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {}
165-
151+
166152
if _API_CORE_VERSION and (
167153
packaging.version.parse(_API_CORE_VERSION)
168154
>= packaging.version.parse("1.26.0")
@@ -172,7 +158,7 @@ class {{ service.name }}Transport(abc.ABC):
172158
self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST
173159
else:
174160
self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES
175-
161+
176162
return self_signed_jwt_kwargs
177163

178164

@@ -251,7 +237,7 @@ class {{ service.name }}Transport(abc.ABC):
251237
],
252238
]:
253239
raise NotImplementedError()
254-
{% endif %}
240+
{% endif %}
255241

256242
__all__ = (
257243
'{{ service.name }}Transport',

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,32 +1505,6 @@ def test_{{ service.name|snake_case }}_base_transport_with_credentials_file_old_
15051505
)
15061506

15071507

1508-
@requires_google_auth_gte_1_25_0
1509-
def test_{{ service.name|snake_case }}_base_transport_custom_host():
1510-
# Test the default credentials are used if credentials and credentials_file are None.
1511-
with mock.patch.object(auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport:
1512-
Transport.return_value = None
1513-
load_creds.return_value = (credentials.AnonymousCredentials(), None)
1514-
transport = transports.{{ service.name }}Transport(
1515-
host="squid.clam.whelk",
1516-
credentials_file="credentials.json",
1517-
quota_project_id="octopus",
1518-
)
1519-
load_creds.assert_called_once_with("credentials.json",
1520-
# scopes should be set since custom endpoint was passed
1521-
scopes=(
1522-
{%- for scope in service.oauth_scopes %}
1523-
'{{ scope }}',
1524-
{%- endfor %}
1525-
),
1526-
default_scopes=(
1527-
{%- for scope in service.oauth_scopes %}
1528-
'{{ scope }}',
1529-
{%- endfor %}
1530-
),
1531-
quota_project_id="octopus",
1532-
)
1533-
15341508
def test_{{ service.name|snake_case }}_base_transport_with_adc():
15351509
# Test the default credentials are used if credentials and credentials_file are None.
15361510
with mock.patch.object(auth, 'default', autospec=True) as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport:
@@ -1596,34 +1570,6 @@ def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class):
15961570
)
15971571

15981572

1599-
@pytest.mark.parametrize(
1600-
"transport_class",
1601-
[
1602-
transports.{{ service.name }}GrpcTransport,
1603-
transports.{{ service.name }}GrpcAsyncIOTransport,
1604-
],
1605-
)
1606-
@requires_google_auth_gte_1_25_0
1607-
def test_{{ service.name|snake_case }}_transport_auth_adc_custom_host(transport_class):
1608-
# If credentials and host are not provided, the transport class should use
1609-
# ADC credentials.
1610-
with mock.patch.object(auth, 'default', autospec=True) as adc:
1611-
adc.return_value = (credentials.AnonymousCredentials(), None)
1612-
transport_class(host="squid.clam.whelk", quota_project_id="octopus")
1613-
adc.assert_called_once_with(
1614-
# scopes should be set since a custom endpoint (host) was set
1615-
scopes=(
1616-
{%- for scope in service.oauth_scopes %}
1617-
'{{ scope }}',
1618-
{%- endfor %}),
1619-
default_scopes=(
1620-
{%- for scope in service.oauth_scopes %}
1621-
'{{ scope }}',
1622-
{%- endfor %}),
1623-
quota_project_id="octopus",
1624-
)
1625-
1626-
16271573
@pytest.mark.parametrize(
16281574
"transport_class",
16291575
[

0 commit comments

Comments
 (0)