Skip to content

Commit 2aeabc7

Browse files
committed
docs: clarify get_default_mtls_endpoint pass-through, fix: support port suffixes in mTLS conversion
1 parent e2227a1 commit 2aeabc7

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
2626
2727
Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
2828
"*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
29+
Other URLs (including those that do not match these domain suffixes or
30+
already contain '.mtls.') are passed through as-is.
2931
3032
Args:
3133
api_endpoint (Optional[str]): the api endpoint to convert.
@@ -36,14 +38,19 @@ def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3638
if not api_endpoint or ".mtls." in api_endpoint.lower():
3739
return api_endpoint
3840

39-
lowered_endpoint = api_endpoint.lower()
40-
if lowered_endpoint.endswith(".sandbox.googleapis.com"):
41+
# Handle optional port suffix (e.g. ":443")
42+
parts = api_endpoint.split(":")
43+
host = parts[0]
44+
port = ":" + parts[1] if len(parts) > 1 else ""
45+
46+
lowered_host = host.lower()
47+
if lowered_host.endswith(".sandbox.googleapis.com"):
4148
# len(".sandbox.googleapis.com") == 23
42-
return api_endpoint[:-23] + ".mtls.sandbox.googleapis.com"
49+
return host[:-23] + ".mtls.sandbox.googleapis.com" + port
4350

44-
if lowered_endpoint.endswith(".googleapis.com"):
51+
if lowered_host.endswith(".googleapis.com"):
4552
# len(".googleapis.com") == 15
46-
return api_endpoint[:-15] + ".mtls.googleapis.com"
53+
return host[:-15] + ".mtls.googleapis.com" + port
4754

4855
return api_endpoint
4956

packages/google-api-core/tests/unit/gapic/test_client_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,32 @@ def test_get_default_mtls_endpoint():
4545
== "foo.mtls.sandbox.googleapis.com"
4646
)
4747

48+
# Test valid API endpoints with ports
49+
assert (
50+
get_default_mtls_endpoint("foo.googleapis.com:443")
51+
== "foo.mtls.googleapis.com:443"
52+
)
53+
assert (
54+
get_default_mtls_endpoint("foo.sandbox.googleapis.com:443")
55+
== "foo.mtls.sandbox.googleapis.com:443"
56+
)
57+
# Test case-insensitivity with ports
58+
assert (
59+
get_default_mtls_endpoint("foo.GoogleAPIs.com:443")
60+
== "foo.mtls.googleapis.com:443"
61+
)
62+
assert (
63+
get_default_mtls_endpoint("foo.Sandbox.GoogleAPIs.com:443")
64+
== "foo.mtls.sandbox.googleapis.com:443"
65+
)
66+
4867
# Test endpoints that shouldn't be converted
4968
assert (
5069
get_default_mtls_endpoint("foo.mtls.googleapis.com")
5170
== "foo.mtls.googleapis.com"
5271
)
5372
assert get_default_mtls_endpoint("foo.com") == "foo.com"
73+
assert get_default_mtls_endpoint("foo.com:8080") == "foo.com:8080"
5474

5575
# Test empty/None endpoints
5676
assert get_default_mtls_endpoint("") == ""

0 commit comments

Comments
 (0)