Skip to content

Commit 4d9bea2

Browse files
committed
refactor: optimize regex and update type hints in client_helpers
1 parent 4463094 commit 4d9bea2

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

packages/google-api-core/google/api_core/gapic_v1/client_helpers.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2424
from google.auth.transport import mtls # type: ignore
2525

26+
_MTLS_ENDPOINT_RE = re.compile(
27+
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
28+
)
29+
30+
2631

2732

2833
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
@@ -38,11 +43,7 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3843
if not api_endpoint:
3944
return api_endpoint
4045

41-
mtls_endpoint_re = re.compile(
42-
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
43-
)
44-
45-
m = mtls_endpoint_re.match(api_endpoint)
46+
m = _MTLS_ENDPOINT_RE.match(api_endpoint)
4647
if m is None:
4748
# Could not parse api_endpoint; return as-is.
4849
return api_endpoint
@@ -89,26 +90,26 @@ def use_client_cert_effective() -> bool:
8990

9091
def get_api_endpoint(
9192
api_override: Optional[str],
92-
client_cert_source: Optional[bytes],
93+
client_cert_source: Optional[Any],
9394
universe_domain: str,
9495
use_mtls_endpoint: str,
9596
default_universe: str,
9697
default_mtls_endpoint: Optional[str],
9798
default_endpoint_template: str,
98-
) -> str:
99+
) -> Optional[str]:
99100
"""Return the API endpoint used by the client.
100101
101102
Args:
102103
api_override (Optional[str]): The API endpoint override.
103-
client_cert_source (Optional[bytes]): The client certificate source.
104+
client_cert_source (Optional[Any]): The client certificate source.
104105
universe_domain (str): The universe domain.
105106
use_mtls_endpoint (str): How to use the mTLS endpoint.
106107
default_universe (str): The default universe.
107108
default_mtls_endpoint (Optional[str]): The default mTLS endpoint.
108109
default_endpoint_template (str): The default endpoint template.
109110
110111
Returns:
111-
str: The API endpoint to be used by the client.
112+
Optional[str]: The API endpoint to be used by the client.
112113
"""
113114
if api_override is not None:
114115
api_endpoint = api_override

0 commit comments

Comments
 (0)