1616
1717"""Helpers for routing and endpoint resolution."""
1818
19- import re
2019from typing import Any , Optional
2120
2221from google .auth .exceptions import MutualTLSChannelError # type: ignore
2322
24- _MTLS_ENDPOINT_RE = re .compile (
25- r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?"
26- r"(?P<googledomain>\.googleapis\.com)?"
27- )
28-
2923
3024def get_default_mtls_endpoint (api_endpoint : Optional [str ]) -> Optional [str ]:
3125 """Converts api endpoint to mTLS endpoint.
@@ -37,24 +31,18 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3731 Returns:
3832 Optional[str]: converted mTLS api endpoint.
3933 """
40- if not api_endpoint :
34+ if not api_endpoint or ".mtls." in api_endpoint :
4135 return api_endpoint
4236
43- m = _MTLS_ENDPOINT_RE .match (api_endpoint )
44- if m is None :
45- # Could not parse api_endpoint; return as-is.
46- return api_endpoint
37+ if api_endpoint .endswith (".sandbox.googleapis.com" ):
38+ # len(".sandbox.googleapis.com") == 23
39+ return api_endpoint [:- 23 ] + ".mtls.sandbox.googleapis.com"
4740
48- name , mtls_group , sandbox , googledomain = m .groups ()
49- if mtls_group or not googledomain :
50- return api_endpoint
51-
52- if sandbox :
53- return api_endpoint .replace (
54- "sandbox.googleapis.com" , "mtls.sandbox.googleapis.com"
55- )
41+ if api_endpoint .endswith (".googleapis.com" ):
42+ # len(".googleapis.com") == 15
43+ return api_endpoint [:- 15 ] + ".mtls.googleapis.com"
5644
57- return api_endpoint . replace ( ".googleapis.com" , ".mtls.googleapis.com" )
45+ return api_endpoint
5846
5947
6048def get_api_endpoint (
0 commit comments