Skip to content

Commit d386dfe

Browse files
committed
refactor: remove underscore prefix from gapic client helpers
1 parent be2b472 commit d386dfe

2 files changed

Lines changed: 76 additions & 57 deletions

File tree

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
)
3131

3232

33-
def _get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
33+
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3434
"""Converts api endpoint to mTLS endpoint.
3535
3636
Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
@@ -60,7 +60,7 @@ def _get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
6060
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
6161

6262

63-
def _use_client_cert_effective() -> bool:
63+
def use_client_cert_effective() -> bool:
6464
"""Returns whether client certificate should be used for mTLS if the
6565
google-auth version supports should_use_client_cert automatic mTLS
6666
enablement.
@@ -91,7 +91,7 @@ def _use_client_cert_effective() -> bool:
9191
return use_client_cert_str == "true"
9292

9393

94-
def _get_api_endpoint(
94+
def get_api_endpoint(
9595
api_override: Optional[str],
9696
client_cert_source: Optional[Any],
9797
universe_domain: str,
@@ -126,10 +126,12 @@ def _get_api_endpoint(
126126
)
127127
return default_mtls_endpoint
128128
else:
129-
return default_endpoint_template.format(UNIVERSE_DOMAIN=universe_domain)
129+
return default_endpoint_template.format(
130+
UNIVERSE_DOMAIN=universe_domain
131+
)
130132

131133

132-
def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
134+
def read_environment_variables() -> Tuple[bool, str, Optional[str]]:
133135
"""Returns the environment variables used by the client.
134136
135137
Returns:
@@ -144,8 +146,10 @@ def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
144146
GOOGLE_API_USE_MTLS_ENDPOINT is not any of
145147
["auto", "never", "always"].
146148
"""
147-
use_client_cert = _use_client_cert_effective()
148-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
149+
use_client_cert = use_client_cert_effective()
150+
use_mtls_endpoint = os.getenv(
151+
"GOOGLE_API_USE_MTLS_ENDPOINT", "auto"
152+
).lower()
149153
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
150154
if use_mtls_endpoint not in ("auto", "never", "always"):
151155
raise MutualTLSChannelError(
@@ -155,7 +159,7 @@ def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
155159
return use_client_cert, use_mtls_endpoint, universe_domain_env
156160

157161

158-
def _get_client_cert_source(
162+
def get_client_cert_source(
159163
provided_cert_source: Optional[Any], use_cert_flag: bool
160164
) -> Optional[Any]:
161165
"""Return the client cert source to be used by the client.
@@ -180,7 +184,7 @@ def _get_client_cert_source(
180184
return client_cert_source
181185

182186

183-
def _get_universe_domain(
187+
def get_universe_domain(
184188
client_universe_domain: Optional[str],
185189
universe_domain_env: Optional[str],
186190
default_universe: str,
@@ -210,7 +214,9 @@ def _get_universe_domain(
210214
return universe_domain
211215

212216

213-
def _setup_request_id(request: Any, field_name: str, is_proto3_optional: bool) -> None:
217+
def setup_request_id(
218+
request: Any, field_name: str, is_proto3_optional: bool
219+
) -> None:
214220
"""Populate a UUID4 field in the request if it is not already set.
215221
216222
Args:

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

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,69 @@
2121
from google.auth.exceptions import MutualTLSChannelError
2222

2323

24-
def test__get_default_mtls_endpoint():
24+
def test_get_default_mtls_endpoint():
2525
# Test valid API endpoints
2626
assert (
27-
client_helpers._get_default_mtls_endpoint("foo.googleapis.com")
27+
client_helpers.get_default_mtls_endpoint("foo.googleapis.com")
2828
== "foo.mtls.googleapis.com"
2929
)
3030
assert (
31-
client_helpers._get_default_mtls_endpoint("foo.sandbox.googleapis.com")
31+
client_helpers.get_default_mtls_endpoint("foo.sandbox.googleapis.com")
3232
== "foo.mtls.sandbox.googleapis.com"
3333
)
3434

3535
# Test endpoints that shouldn't be converted
3636
assert (
37-
client_helpers._get_default_mtls_endpoint("foo.mtls.googleapis.com")
37+
client_helpers.get_default_mtls_endpoint("foo.mtls.googleapis.com")
3838
== "foo.mtls.googleapis.com"
3939
)
40-
assert client_helpers._get_default_mtls_endpoint("foo.com") == "foo.com"
40+
assert client_helpers.get_default_mtls_endpoint("foo.com") == "foo.com"
4141

4242
# Test empty/None endpoints
43-
assert client_helpers._get_default_mtls_endpoint("") == ""
44-
assert client_helpers._get_default_mtls_endpoint(None) is None
43+
assert client_helpers.get_default_mtls_endpoint("") == ""
44+
assert client_helpers.get_default_mtls_endpoint(None) is None
4545

4646

4747
@mock.patch("google.auth.transport.mtls.should_use_client_cert", autospec=True)
48-
def test__use_client_cert_effective_with_google_auth(mock_method):
48+
def test_use_client_cert_effective_with_google_auth(mock_method):
4949
# Test when google-auth supports the method
5050
mock_method.return_value = True
51-
assert client_helpers._use_client_cert_effective() is True
51+
assert client_helpers.use_client_cert_effective() is True
5252

5353
mock_method.return_value = False
54-
assert client_helpers._use_client_cert_effective() is False
54+
assert client_helpers.use_client_cert_effective() is False
5555

5656

5757
@mock.patch.dict(os.environ, {}, clear=True)
58-
def test__use_client_cert_effective_fallback():
58+
def test_use_client_cert_effective_fallback():
5959
# We must patch hasattr to simulate google-auth lacking the method
6060
with mock.patch(
6161
"google.api_core.gapic_v1.client_helpers.hasattr", return_value=False
6262
):
6363
# Default is false
64-
assert client_helpers._use_client_cert_effective() is False
64+
assert client_helpers.use_client_cert_effective() is False
6565

6666
env_true = {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}
6767
with mock.patch.dict(os.environ, env_true):
68-
assert client_helpers._use_client_cert_effective() is True
68+
assert client_helpers.use_client_cert_effective() is True
6969

7070
with mock.patch.dict(
7171
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}
7272
):
73-
assert client_helpers._use_client_cert_effective() is False
73+
assert client_helpers.use_client_cert_effective() is False
7474

7575
with mock.patch.dict(
7676
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}
7777
):
7878
match_str = "must be either `true` or `false`"
7979
with pytest.raises(ValueError, match=match_str):
80-
client_helpers._use_client_cert_effective()
80+
client_helpers.use_client_cert_effective()
8181

8282

83-
def test__get_api_endpoint_override():
83+
def test_get_api_endpoint_override():
8484
# If api_override is provided, it should be returned
8585
# regardless of other args
86-
endpoint = client_helpers._get_api_endpoint(
86+
endpoint = client_helpers.get_api_endpoint(
8787
api_override="custom.endpoint.com",
8888
client_cert_source=None,
8989
universe_domain="googleapis.com",
@@ -95,9 +95,9 @@ def test__get_api_endpoint_override():
9595
assert endpoint == "custom.endpoint.com"
9696

9797

98-
def test__get_api_endpoint_mtls_always():
98+
def test_get_api_endpoint_mtls_always():
9999
# use_mtls_endpoint == "always" should use the default mtls endpoint
100-
endpoint = client_helpers._get_api_endpoint(
100+
endpoint = client_helpers.get_api_endpoint(
101101
api_override=None,
102102
client_cert_source=None,
103103
universe_domain="googleapis.com",
@@ -109,9 +109,9 @@ def test__get_api_endpoint_mtls_always():
109109
assert endpoint == "foo.mtls.googleapis.com"
110110

111111

112-
def test__get_api_endpoint_mtls_auto_with_cert():
112+
def test_get_api_endpoint_mtls_auto_with_cert():
113113
# "auto" with client_cert_source should use mtls
114-
endpoint = client_helpers._get_api_endpoint(
114+
endpoint = client_helpers.get_api_endpoint(
115115
api_override=None,
116116
client_cert_source=mock.Mock(),
117117
universe_domain="googleapis.com",
@@ -123,9 +123,9 @@ def test__get_api_endpoint_mtls_auto_with_cert():
123123
assert endpoint == "foo.mtls.googleapis.com"
124124

125125

126-
def test__get_api_endpoint_mtls_auto_no_cert():
126+
def test_get_api_endpoint_mtls_auto_no_cert():
127127
# "auto" without client_cert_source should use the default template
128-
endpoint = client_helpers._get_api_endpoint(
128+
endpoint = client_helpers.get_api_endpoint(
129129
api_override=None,
130130
client_cert_source=None,
131131
universe_domain="googleapis.com",
@@ -137,10 +137,10 @@ def test__get_api_endpoint_mtls_auto_no_cert():
137137
assert endpoint == "foo.googleapis.com"
138138

139139

140-
def test__get_api_endpoint_mtls_universe_mismatch():
140+
def test_get_api_endpoint_mtls_universe_mismatch():
141141
# mTLS is only supported in the default universe
142142
with pytest.raises(MutualTLSChannelError, match="mTLS is not supported"):
143-
client_helpers._get_api_endpoint(
143+
client_helpers.get_api_endpoint(
144144
api_override=None,
145145
client_cert_source=mock.Mock(),
146146
universe_domain="custom-universe.com",
@@ -151,80 +151,93 @@ def test__get_api_endpoint_mtls_universe_mismatch():
151151
)
152152

153153

154-
@mock.patch("google.api_core.gapic_v1.client_helpers._use_client_cert_effective")
154+
@mock.patch(
155+
"google.api_core.gapic_v1.client_helpers.use_client_cert_effective"
156+
)
155157
@mock.patch.dict(os.environ, clear=True)
156-
def test__read_environment_variables(mock_effective):
158+
def test_read_environment_variables(mock_effective):
157159
mock_effective.return_value = True
158160
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "always"
159161
os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "custom.com"
160162

161-
cert, mtls, domain = client_helpers._read_environment_variables()
163+
cert, mtls, domain = client_helpers.read_environment_variables()
162164
assert cert is True
163165
assert mtls == "always"
164166
assert domain == "custom.com"
165167

166168

167169
@mock.patch.dict(os.environ, clear=True)
168-
def test__read_environment_variables_invalid_mtls():
170+
def test_read_environment_variables_invalid_mtls():
169171
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "invalid"
170172
with pytest.raises(
171173
MutualTLSChannelError, match="must be `never`, `auto` or `always`"
172174
):
173-
client_helpers._read_environment_variables()
175+
client_helpers.read_environment_variables()
174176

175177

176-
@mock.patch("google.auth.transport.mtls.has_default_client_cert_source", create=True)
177-
@mock.patch("google.auth.transport.mtls.default_client_cert_source", create=True)
178-
def test__get_client_cert_source(mock_default, mock_has_default):
178+
@mock.patch(
179+
"google.auth.transport.mtls.has_default_client_cert_source", create=True
180+
)
181+
@mock.patch(
182+
"google.auth.transport.mtls.default_client_cert_source", create=True
183+
)
184+
def test_get_client_cert_source(mock_default, mock_has_default):
179185
mock_default.return_value = b"default_cert"
180186
mock_has_default.return_value = True
181187

182188
# When use_cert_flag is False, return None
183-
assert client_helpers._get_client_cert_source(b"provided", False) is None
189+
assert client_helpers.get_client_cert_source(b"provided", False) is None
184190

185191
# When provided_cert_source is given, return provided
186-
assert client_helpers._get_client_cert_source(b"provided", True) == b"provided"
192+
assert (
193+
client_helpers.get_client_cert_source(b"provided", True)
194+
== b"provided"
195+
)
187196

188197
# When no provided cert but default is available
189-
assert client_helpers._get_client_cert_source(None, True) == b"default_cert"
198+
assert client_helpers.get_client_cert_source(None, True) == b"default_cert"
190199

191200

192-
def test__get_universe_domain():
201+
def test_get_universe_domain():
193202
# client_universe_domain takes precedence
194203
assert (
195-
client_helpers._get_universe_domain("client.com", "env.com", "default.com")
204+
client_helpers.get_universe_domain(
205+
"client.com", "env.com", "default.com"
206+
)
196207
== "client.com"
197208
)
198209

199210
# env takes precedence over default
200211
assert (
201-
client_helpers._get_universe_domain(None, "env.com", "default.com") == "env.com"
212+
client_helpers.get_universe_domain(None, "env.com", "default.com")
213+
== "env.com"
202214
)
203215

204216
# fallback to default
205217
assert (
206-
client_helpers._get_universe_domain(None, None, "default.com") == "default.com"
218+
client_helpers.get_universe_domain(None, None, "default.com")
219+
== "default.com"
207220
)
208221

209222

210-
def test__get_universe_domain_empty():
223+
def test_get_universe_domain_empty():
211224
with pytest.raises(ValueError, match="cannot be an empty string"):
212-
client_helpers._get_universe_domain("", None, "default.com")
225+
client_helpers.get_universe_domain("", None, "default.com")
213226

214227

215-
def test__setup_request_id():
228+
def test_setup_request_id():
216229
import uuid
217230

218231
# test dict request
219232
req = {}
220-
client_helpers._setup_request_id(req, "request_id", True)
233+
client_helpers.setup_request_id(req, "request_id", True)
221234
assert "request_id" in req
222235
uuid_str = req["request_id"]
223236
uuid.UUID(uuid_str) # verify it is a valid UUID
224237

225238
# test dict request when already set
226239
req = {"request_id": "existing"}
227-
client_helpers._setup_request_id(req, "request_id", True)
240+
client_helpers.setup_request_id(req, "request_id", True)
228241
assert req["request_id"] == "existing"
229242

230243
class DummyRequest:
@@ -238,12 +251,12 @@ def HasField(self, field_name):
238251

239252
# test object request proto3 optional true
240253
req_obj = DummyRequest()
241-
client_helpers._setup_request_id(req_obj, "request_id", True)
254+
client_helpers.setup_request_id(req_obj, "request_id", True)
242255
assert req_obj.request_id != ""
243256
uuid.UUID(req_obj.request_id)
244257

245258
# test object request proto3 optional false
246259
req_obj2 = DummyRequest()
247-
client_helpers._setup_request_id(req_obj2, "request_id", False)
260+
client_helpers.setup_request_id(req_obj2, "request_id", False)
248261
assert req_obj2.request_id != ""
249262
uuid.UUID(req_obj2.request_id)

0 commit comments

Comments
 (0)