Skip to content

Commit 331a643

Browse files
committed
refactor: address review comments on centralized routing helpers
1 parent 9f58958 commit 331a643

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

  • packages/gapic-generator/gapic/templates

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,10 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
144144
"""{{ service.meta.doc|rst(width=72, indent=4) }}{% if service.version|length %}
145145
This class implements API version {{ service.version }}.{% endif %}"""
146146

147-
@staticmethod
148-
def _get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
149-
"""Converts api endpoint to mTLS endpoint."""
150-
return routing.get_default_mtls_endpoint(api_endpoint)
151-
152147
# Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead.
153148
DEFAULT_ENDPOINT = {% if service.host %}"{{ service.host }}"{% else %}None{% endif %}
154149

155-
DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore
150+
DEFAULT_MTLS_ENDPOINT = routing.get_default_mtls_endpoint(
156151
DEFAULT_ENDPOINT
157152
)
158153

@@ -370,7 +365,19 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
370365
universe_domain: str,
371366
use_mtls_endpoint: str,
372367
) -> str:
373-
"""Return the API endpoint used by the client."""
368+
"""Return the API endpoint used by the client.
369+
370+
Args:
371+
api_override (Optional[str]): The API endpoint override. If specified, this is always
372+
the return value of this function and the other arguments are not used.
373+
client_cert_source (Union[Callable[[], Tuple[bytes, bytes]], None]): The client certificate source used by the client.
374+
universe_domain (str): The universe domain used by the client.
375+
use_mtls_endpoint (str): How to use the mTLS endpoint, which depends also on the other parameters.
376+
Possible values are "always", "auto", or "never".
377+
378+
Returns:
379+
str: The API endpoint to be used by the client.
380+
"""
374381
return routing.get_api_endpoint(
375382
api_override,
376383
client_cert_source,
@@ -383,7 +390,18 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
383390

384391
@staticmethod
385392
def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_env: Optional[str]) -> str:
386-
"""Return the universe domain used by the client."""
393+
"""Return the universe domain used by the client.
394+
395+
Args:
396+
client_universe_domain (Optional[str]): The universe domain configured via the client options.
397+
universe_domain_env (Optional[str]): The universe domain configured via the "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable.
398+
399+
Returns:
400+
str: The universe domain to be used by the client.
401+
402+
Raises:
403+
ValueError: If the universe domain is an empty string.
404+
"""
387405
return routing.get_universe_domain(
388406
client_universe_domain,
389407
universe_domain_env,

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,6 @@ def set_event_loop():
156156
asyncio.set_event_loop(None)
157157

158158

159-
def test__get_default_mtls_endpoint():
160-
api_endpoint = "example.googleapis.com"
161-
api_mtls_endpoint = "example.mtls.googleapis.com"
162-
sandbox_endpoint = "example.sandbox.googleapis.com"
163-
sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com"
164-
non_googleapi = "api.example.com"
165-
custom_endpoint = ".custom"
166-
167-
assert {{ service.client_name }}._get_default_mtls_endpoint(None) is None
168-
assert {{ service.client_name }}._get_default_mtls_endpoint(api_endpoint) == api_mtls_endpoint
169-
assert {{ service.client_name }}._get_default_mtls_endpoint(api_mtls_endpoint) == api_mtls_endpoint
170-
assert {{ service.client_name }}._get_default_mtls_endpoint(sandbox_endpoint) == sandbox_mtls_endpoint
171-
assert {{ service.client_name }}._get_default_mtls_endpoint(sandbox_mtls_endpoint) == sandbox_mtls_endpoint
172-
assert {{ service.client_name }}._get_default_mtls_endpoint(non_googleapi) == non_googleapi
173-
assert {{ service.client_name }}._get_default_mtls_endpoint(custom_endpoint) == custom_endpoint
174-
175159
def test__read_environment_variables():
176160
assert {{ service.client_name }}._read_environment_variables() == (False, "auto", None)
177161

0 commit comments

Comments
 (0)