Skip to content

Commit 26e7820

Browse files
committed
refactor(api-core): update get_universe_domain to accept multiple potential universes
1 parent e0af12e commit 26e7820

2 files changed

Lines changed: 29 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ def get_api_endpoint(
109109

110110

111111
def get_universe_domain(
112-
universe_domain: Optional[str],
112+
*potential_universes: Optional[str],
113113
default_universe: str = "googleapis.com",
114114
) -> str:
115115
"""Return the universe domain used by the client.
116116
117117
Args:
118-
universe_domain (Optional[str]): The configured universe domain.
118+
*potential_universes (Optional[str]): Potential universe domains in order of preference.
119119
default_universe (str): The default universe domain.
120120
121121
Returns:
@@ -124,8 +124,9 @@ def get_universe_domain(
124124
Raises:
125125
ValueError: If the resolved universe domain is an empty string.
126126
"""
127-
resolved = (
128-
universe_domain.strip() if universe_domain is not None else default_universe
127+
resolved = next(
128+
(x.strip() for x in potential_universes if x is not None),
129+
default_universe,
129130
)
130131

131132
if not resolved:

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,36 @@ def test_get_api_endpoint(
164164

165165
def test_get_universe_domain():
166166
# When universe_domain is provided
167-
assert get_universe_domain("foo.com", "default.com") == "foo.com"
168-
assert get_universe_domain(" foo.com ", "default.com") == "foo.com"
167+
assert get_universe_domain("foo.com", default_universe="default.com") == "foo.com"
168+
assert (
169+
get_universe_domain(" foo.com ", default_universe="default.com") == "foo.com"
170+
)
169171

170172
# When universe_domain is None, falls back to default_universe
171-
assert get_universe_domain(None, "default.com") == "default.com"
173+
assert get_universe_domain(None, default_universe="default.com") == "default.com"
174+
175+
# When multiple potential universes are provided, resolves in order of preference
176+
assert (
177+
get_universe_domain("foo.com", "bar.com", default_universe="default.com")
178+
== "foo.com"
179+
)
180+
assert (
181+
get_universe_domain(None, "bar.com", default_universe="default.com")
182+
== "bar.com"
183+
)
184+
assert (
185+
get_universe_domain(None, None, default_universe="default.com") == "default.com"
186+
)
172187

173188
# ValueError raised when resolved value is empty string
174189
with pytest.raises(ValueError) as excinfo:
175-
get_universe_domain("", "default.com")
190+
get_universe_domain("", default_universe="default.com")
191+
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
192+
193+
with pytest.raises(ValueError) as excinfo:
194+
get_universe_domain(" ", default_universe="default.com")
176195
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
177196

178197
with pytest.raises(ValueError) as excinfo:
179-
get_universe_domain(" ", "default.com")
198+
get_universe_domain(None, "", default_universe="default.com")
180199
assert str(excinfo.value) == "Universe Domain cannot be an empty string."

0 commit comments

Comments
 (0)