Skip to content

Commit 5e1eb20

Browse files
committed
update: generator templates to delegate to api_core helpers
1 parent a4513c4 commit 5e1eb20

1 file changed

Lines changed: 14 additions & 77 deletions

File tree

  • packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service

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

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -145,37 +145,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
145145

146146
@staticmethod
147147
def _get_default_mtls_endpoint(api_endpoint) -> Optional[str]:
148-
"""Converts api endpoint to mTLS endpoint.
149-
150-
Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
151-
"*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
152-
Args:
153-
api_endpoint (Optional[str]): the api endpoint to convert.
154-
Returns:
155-
Optional[str]: converted mTLS api endpoint.
156-
"""
157-
if not api_endpoint:
158-
return api_endpoint
159-
160-
mtls_endpoint_re = re.compile(
161-
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
162-
)
163-
164-
m = mtls_endpoint_re.match(api_endpoint)
165-
if m is None:
166-
# Could not parse api_endpoint; return as-is.
167-
return api_endpoint
168-
169-
name, mtls, sandbox, googledomain = m.groups()
170-
if mtls or not googledomain:
171-
return api_endpoint
172-
173-
if sandbox:
174-
return api_endpoint.replace(
175-
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
176-
)
177-
178-
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
148+
"""Converts api endpoint to mTLS endpoint."""
149+
return gapic_v1.client_helpers._get_default_mtls_endpoint(api_endpoint)
179150

180151
# Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead.
181152
DEFAULT_ENDPOINT = {% if service.host %}"{{ service.host }}"{% else %}None{% endif %}
@@ -190,29 +161,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
190161

191162
@staticmethod
192163
def _use_client_cert_effective():
193-
"""Returns whether client certificate should be used for mTLS if the
194-
google-auth version supports should_use_client_cert automatic mTLS enablement.
195-
196-
Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var.
197-
198-
Returns:
199-
bool: whether client certificate should be used for mTLS
200-
Raises:
201-
ValueError: (If using a version of google-auth without should_use_client_cert and
202-
GOOGLE_API_USE_CLIENT_CERTIFICATE is set to an unexpected value.)
203-
"""
204-
# check if google-auth version supports should_use_client_cert for automatic mTLS enablement
205-
if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER
206-
return mtls.should_use_client_cert()
207-
else: # pragma: NO COVER
208-
# if unsupported, fallback to reading from env var
209-
use_client_cert_str = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false").lower()
210-
if use_client_cert_str not in ("true", "false"):
211-
raise ValueError(
212-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be"
213-
" either `true` or `false`"
214-
)
215-
return use_client_cert_str == "true"
164+
"""Returns whether client certificate should be used for mTLS."""
165+
return gapic_v1.client_helpers._use_client_cert_effective()
216166

217167
@classmethod
218168
def from_service_account_info(cls, info: dict, *args, **kwargs):
@@ -393,29 +343,16 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
393343

394344
@staticmethod
395345
def _get_api_endpoint(api_override, client_cert_source, universe_domain, use_mtls_endpoint) -> str:
396-
"""Return the API endpoint used by the client.
397-
398-
Args:
399-
api_override (str): The API endpoint override. If specified, this is always
400-
the return value of this function and the other arguments are not used.
401-
client_cert_source (bytes): The client certificate source used by the client.
402-
universe_domain (str): The universe domain used by the client.
403-
use_mtls_endpoint (str): How to use the mTLS endpoint, which depends also on the other parameters.
404-
Possible values are "always", "auto", or "never".
405-
406-
Returns:
407-
str: The API endpoint to be used by the client.
408-
"""
409-
if api_override is not None:
410-
api_endpoint = api_override
411-
elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source):
412-
_default_universe = {{ service.client_name }}._DEFAULT_UNIVERSE
413-
if universe_domain != _default_universe:
414-
raise MutualTLSChannelError(f"mTLS is not supported in any universe other than {_default_universe}.")
415-
api_endpoint = {{ service.client_name }}.DEFAULT_MTLS_ENDPOINT
416-
else:
417-
api_endpoint = {{ service.client_name }}._DEFAULT_ENDPOINT_TEMPLATE.format(UNIVERSE_DOMAIN=universe_domain)
418-
return api_endpoint
346+
"""Return the API endpoint used by the client."""
347+
return gapic_v1.client_helpers._get_api_endpoint(
348+
api_override,
349+
client_cert_source,
350+
universe_domain,
351+
use_mtls_endpoint,
352+
{{ service.client_name }}._DEFAULT_UNIVERSE,
353+
{{ service.client_name }}.DEFAULT_MTLS_ENDPOINT,
354+
{{ service.client_name }}._DEFAULT_ENDPOINT_TEMPLATE,
355+
)
419356

420357
@staticmethod
421358
def _get_universe_domain(client_universe_domain: Optional[str], universe_domain_env: Optional[str]) -> str:

0 commit comments

Comments
 (0)