Skip to content

Commit cdca139

Browse files
committed
refactor(api-core): import get_universe_domain with fallback for PR checks compatibility
1 parent 26e7820 commit cdca139

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@
1818

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

21+
try:
22+
from google.api_core.universe import ( # type: ignore[attr-defined]
23+
get_universe_domain,
24+
)
25+
except ImportError:
26+
27+
def get_universe_domain(
28+
*potential_universes: Optional[str],
29+
default_universe: str = "googleapis.com",
30+
) -> str:
31+
"""Return the universe domain used by the client.
32+
33+
Args:
34+
*potential_universes (Optional[str]): Potential universe domains in order of preference.
35+
default_universe (str): The default universe domain.
36+
37+
Returns:
38+
str: The universe domain to be used by the client.
39+
40+
Raises:
41+
ValueError: If the resolved universe domain is an empty string.
42+
"""
43+
resolved = next(
44+
(x.strip() for x in potential_universes if x is not None),
45+
default_universe,
46+
)
47+
48+
if not resolved:
49+
raise ValueError("Universe Domain cannot be an empty string.")
50+
return resolved
51+
2152

2253
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
2354
"""Converts api endpoint to mTLS endpoint.
@@ -106,29 +137,3 @@ def get_api_endpoint(
106137
return default_mtls_endpoint
107138
else:
108139
return default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain)
109-
110-
111-
def get_universe_domain(
112-
*potential_universes: Optional[str],
113-
default_universe: str = "googleapis.com",
114-
) -> str:
115-
"""Return the universe domain used by the client.
116-
117-
Args:
118-
*potential_universes (Optional[str]): Potential universe domains in order of preference.
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 = next(
128-
(x.strip() for x in potential_universes if x is not None),
129-
default_universe,
130-
)
131-
132-
if not resolved:
133-
raise ValueError("Universe Domain cannot be an empty string.")
134-
return resolved

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
if they are not already set.
2222
"""
2323

24-
from typing import Union
2524
import uuid
25+
from typing import Union
2626

2727
import google.protobuf.message
2828

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from google.api_core.gapic_v1.requests import setup_request_id
2121

22-
2322
# --- Mock Request Helper Classes ---
2423

2524

0 commit comments

Comments
 (0)