Skip to content

Commit e237507

Browse files
committed
refactor(api-core): move universe routing helpers to universe.py
1 parent 0314206 commit e237507

4 files changed

Lines changed: 86 additions & 50 deletions

File tree

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818

1919
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2020

21+
from google.api_core.universe import get_universe_domain
22+
23+
__all__ = [
24+
"get_default_mtls_endpoint",
25+
"get_api_endpoint",
26+
"get_universe_domain",
27+
]
28+
2129

2230
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
2331
"""Converts api endpoint to mTLS endpoint.
@@ -107,27 +115,3 @@ def get_api_endpoint(
107115
else:
108116
return default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain)
109117

110-
111-
def get_universe_domain(
112-
universe_domain: Optional[str],
113-
default_universe: str = "googleapis.com",
114-
) -> str:
115-
"""Return the universe domain used by the client.
116-
117-
Args:
118-
universe_domain (Optional[str]): The configured universe domain.
119-
default_universe (str): The default universe domain.
120-
121-
Returns:
122-
str: The universe domain to be used by the client.
123-
124-
Raises:
125-
ValueError: If the resolved universe domain is an empty string.
126-
"""
127-
resolved = (
128-
universe_domain.strip() if universe_domain is not None else default_universe
129-
)
130-
131-
if not resolved:
132-
raise ValueError("Universe Domain cannot be an empty string.")
133-
return resolved

packages/google-api-core/google/api_core/universe.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ def __init__(self, client_universe, credentials_universe):
3636
super().__init__(message)
3737

3838

39+
def get_universe_domain(
40+
*potential_universes: Optional[str],
41+
default_universe: str,
42+
) -> str:
43+
"""Return the universe domain used by the client.
44+
45+
Args:
46+
*potential_universes (Optional[str]): Potential universe domains in order of preference.
47+
default_universe (str): The default universe domain.
48+
49+
Returns:
50+
str: The universe domain to be used by the client.
51+
52+
Raises:
53+
EmptyUniverseError: If the resolved universe domain is an empty string.
54+
"""
55+
resolved = next(
56+
(x.strip() for x in potential_universes if x is not None),
57+
default_universe,
58+
)
59+
60+
if not resolved:
61+
raise EmptyUniverseError()
62+
return resolved
63+
64+
3965
def determine_domain(
4066
client_universe_domain: Optional[str], universe_domain_env: Optional[str]
4167
) -> str:
@@ -52,14 +78,11 @@ def determine_domain(
5278
Raises:
5379
ValueError: If the universe domain is an empty string.
5480
"""
55-
universe_domain = DEFAULT_UNIVERSE
56-
if client_universe_domain is not None:
57-
universe_domain = client_universe_domain
58-
elif universe_domain_env is not None:
59-
universe_domain = universe_domain_env
60-
if len(universe_domain.strip()) == 0:
61-
raise EmptyUniverseError
62-
return universe_domain
81+
return get_universe_domain(
82+
client_universe_domain,
83+
universe_domain_env,
84+
default_universe=DEFAULT_UNIVERSE,
85+
)
6386

6487

6588
def compare_domains(client_universe: str, credentials: Any) -> bool:

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from google.api_core.gapic_v1.client_utils import (
1919
get_api_endpoint,
2020
get_default_mtls_endpoint,
21-
get_universe_domain,
2221
)
2322

2423

@@ -160,20 +159,3 @@ def test_get_api_endpoint(
160159
== expected
161160
)
162161

163-
164-
def test_get_universe_domain():
165-
# When universe_domain is provided
166-
assert get_universe_domain("foo.com", "default.com") == "foo.com"
167-
assert get_universe_domain(" foo.com ", "default.com") == "foo.com"
168-
169-
# When universe_domain is None, falls back to default_universe
170-
assert get_universe_domain(None, "default.com") == "default.com"
171-
172-
# ValueError raised when resolved value is empty string
173-
with pytest.raises(ValueError) as excinfo:
174-
get_universe_domain("", "default.com")
175-
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
176-
177-
with pytest.raises(ValueError) as excinfo:
178-
get_universe_domain(" ", "default.com")
179-
assert str(excinfo.value) == "Universe Domain cannot be an empty string."

packages/google-api-core/tests/unit/test_universe.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,50 @@ def test_compare_domains():
6262
universe.compare_domains(fake_domain, _Fake_Credentials(another_fake_domain))
6363
assert str(excinfo.value).find(fake_domain) >= 0
6464
assert str(excinfo.value).find(another_fake_domain) >= 0
65+
66+
67+
def test_get_universe_domain():
68+
# When universe_domain is provided
69+
assert (
70+
universe.get_universe_domain("foo.com", default_universe="default.com")
71+
== "foo.com"
72+
)
73+
assert (
74+
universe.get_universe_domain(" foo.com ", default_universe="default.com")
75+
== "foo.com"
76+
)
77+
78+
# When universe_domain is None, falls back to default_universe
79+
assert (
80+
universe.get_universe_domain(None, default_universe="default.com")
81+
== "default.com"
82+
)
83+
84+
# When multiple potential universes are provided, resolves in order of preference
85+
assert (
86+
universe.get_universe_domain(
87+
"foo.com", "bar.com", default_universe="default.com"
88+
)
89+
== "foo.com"
90+
)
91+
assert (
92+
universe.get_universe_domain(None, "bar.com", default_universe="default.com")
93+
== "bar.com"
94+
)
95+
assert (
96+
universe.get_universe_domain(None, None, default_universe="default.com")
97+
== "default.com"
98+
)
99+
100+
# EmptyUniverseError raised when resolved value is empty string
101+
with pytest.raises(universe.EmptyUniverseError) as excinfo:
102+
universe.get_universe_domain("", default_universe="default.com")
103+
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
104+
105+
with pytest.raises(universe.EmptyUniverseError) as excinfo:
106+
universe.get_universe_domain(" ", default_universe="default.com")
107+
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
108+
109+
with pytest.raises(universe.EmptyUniverseError) as excinfo:
110+
universe.get_universe_domain(None, "", default_universe="default.com")
111+
assert str(excinfo.value) == "Universe Domain cannot be an empty string."

0 commit comments

Comments
 (0)