Skip to content

Commit 79c74a4

Browse files
committed
refactor(api-core): rename gapic internal helper modules and functions to public
Rename _client_cert, _config_helpers, _method_helpers, and _routing submodules and their functions to public names, as they are consumed across package boundaries by generated client libraries. Updates unit tests and top-level gapic_v1 imports accordingly.
1 parent ac1c090 commit 79c74a4

9 files changed

Lines changed: 73 additions & 57 deletions

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@
2525
# Older Python versions safely ignore this variable.
2626
__lazy_modules__: Set[str] = {
2727
"google.api_core.gapic_v1.client_info",
28+
"google.api_core.gapic_v1.client_cert",
29+
"google.api_core.gapic_v1.config_helpers",
30+
"google.api_core.gapic_v1.method_helpers",
31+
"google.api_core.gapic_v1.routing",
2832
"google.api_core.gapic_v1.routing_header",
2933
}
30-
__all__ = ["client_info", "routing_header"]
34+
__all__ = [
35+
"client_info",
36+
"client_cert",
37+
"config_helpers",
38+
"method_helpers",
39+
"routing",
40+
"routing_header",
41+
]
3142

3243
if _has_grpc:
3344
__lazy_modules__.update(
@@ -40,7 +51,11 @@
4051
)
4152

4253
from google.api_core.gapic_v1 import ( # noqa: E402
54+
client_cert,
4355
client_info,
56+
config_helpers,
57+
method_helpers,
58+
routing,
4459
routing_header,
4560
)
4661

packages/google-api-core/google/api_core/gapic_v1/_client_cert.py renamed to packages/google-api-core/google/api_core/gapic_v1/client_cert.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from google.auth.transport import mtls # type: ignore
2323

2424

25-
def _use_client_cert_effective() -> bool:
25+
def use_client_cert_effective() -> bool:
2626
"""Returns whether client certificate should be used for mTLS if the
2727
google-auth version supports should_use_client_cert automatic mTLS
2828
enablement.
@@ -53,7 +53,7 @@ def _use_client_cert_effective() -> bool:
5353
return use_client_cert_str == "true"
5454

5555

56-
def _get_client_cert_source(
56+
def get_client_cert_source(
5757
provided_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]],
5858
use_cert_flag: bool,
5959
) -> Optional[Callable[[], Tuple[bytes, bytes]]]:
@@ -77,3 +77,4 @@ def _get_client_cert_source(
7777
):
7878
client_cert_source = mtls.default_client_cert_source()
7979
return client_cert_source
80+

packages/google-api-core/google/api_core/gapic_v1/_config_helpers.py renamed to packages/google-api-core/google/api_core/gapic_v1/config_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2323

24-
from google.api_core.gapic_v1._client_cert import _use_client_cert_effective
24+
from google.api_core.gapic_v1.client_cert import use_client_cert_effective
2525

2626

27-
def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
27+
def read_environment_variables() -> Tuple[bool, str, Optional[str]]:
2828
"""Returns the environment variables used by the client.
2929
3030
Returns:
@@ -39,7 +39,7 @@ def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
3939
GOOGLE_API_USE_MTLS_ENDPOINT is not any of
4040
["auto", "never", "always"].
4141
"""
42-
use_client_cert = _use_client_cert_effective()
42+
use_client_cert = use_client_cert_effective()
4343
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
4444
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
4545
if use_mtls_endpoint not in ("auto", "never", "always"):

packages/google-api-core/google/api_core/gapic_v1/_method_helpers.py renamed to packages/google-api-core/google/api_core/gapic_v1/method_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Any
2121

2222

23-
def _setup_request_id(request: Any, field_name: str, is_proto3_optional: bool) -> None:
23+
def setup_request_id(request: Any, field_name: str, is_proto3_optional: bool) -> None:
2424
"""Populate a UUID4 field in the request if it is not already set.
2525
2626
Args:

packages/google-api-core/google/api_core/gapic_v1/_routing.py renamed to packages/google-api-core/google/api_core/gapic_v1/routing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828

2929

30-
def _get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
30+
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
3131
"""Converts api endpoint to mTLS endpoint.
3232
3333
Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
@@ -57,7 +57,7 @@ def _get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
5757
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
5858

5959

60-
def _get_api_endpoint(
60+
def get_api_endpoint(
6161
api_override: Optional[str],
6262
client_cert_source: Optional[Any],
6363
universe_domain: str,
@@ -85,7 +85,7 @@ def _get_api_endpoint(
8585
)
8686

8787

88-
def _get_universe_domain(
88+
def get_universe_domain(
8989
client_universe_domain: Optional[str],
9090
universe_domain_env: Optional[str],
9191
default_universe: str,

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,46 @@
1919
import pytest
2020

2121

22-
from google.api_core.gapic_v1._client_cert import (
23-
_get_client_cert_source,
24-
_use_client_cert_effective,
22+
from google.api_core.gapic_v1.client_cert import (
23+
get_client_cert_source,
24+
use_client_cert_effective,
2525
)
2626

2727

2828
@mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True)
2929
def test_use_client_cert_effective_with_google_auth(mock_method):
3030
# Test when google-auth supports the method
3131
mock_method.return_value = True
32-
assert _use_client_cert_effective() is True
32+
assert use_client_cert_effective() is True
3333

3434
mock_method.return_value = False
35-
assert _use_client_cert_effective() is False
35+
assert use_client_cert_effective() is False
3636

3737

3838
@mock.patch.dict(os.environ, {}, clear=True)
3939
def test_use_client_cert_effective_fallback():
4040
# We must patch hasattr to simulate google-auth lacking the method
4141
with mock.patch(
42-
"google.api_core.gapic_v1._client_cert.hasattr", return_value=False
42+
"google.api_core.gapic_v1.client_cert.hasattr", return_value=False
4343
):
4444
# Default is false
45-
assert _use_client_cert_effective() is False
45+
assert use_client_cert_effective() is False
4646

4747
env_true = {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}
4848
with mock.patch.dict(os.environ, env_true):
49-
assert _use_client_cert_effective() is True
49+
assert use_client_cert_effective() is True
5050

5151
with mock.patch.dict(
5252
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}
5353
):
54-
assert _use_client_cert_effective() is False
54+
assert use_client_cert_effective() is False
5555

5656
with mock.patch.dict(
5757
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}
5858
):
5959
match_str = "must be either `true` or `false`"
6060
with pytest.raises(ValueError, match=match_str):
61-
_use_client_cert_effective()
61+
use_client_cert_effective()
6262

6363

6464
@mock.patch(
@@ -72,10 +72,10 @@ def test_get_client_cert_source(mock_default, mock_has_default):
7272
mock_has_default.return_value = True
7373

7474
# When use_cert_flag is False, return None
75-
assert _get_client_cert_source(b"provided", False) is None
75+
assert get_client_cert_source(b"provided", False) is None
7676

7777
# When provided_cert_source is given, return provided
78-
assert _get_client_cert_source(b"provided", True) == b"provided" # noqa: E501
78+
assert get_client_cert_source(b"provided", True) == b"provided" # noqa: E501
7979

8080
# When no provided cert but default is available
81-
assert _get_client_cert_source(None, True) == b"default_cert"
81+
assert get_client_cert_source(None, True) == b"default_cert"

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020

2121
from google.auth.exceptions import MutualTLSChannelError
2222

23-
from google.api_core.gapic_v1._config_helpers import _read_environment_variables
23+
from google.api_core.gapic_v1.config_helpers import read_environment_variables
2424

2525

2626
@mock.patch(
27-
"google.api_core.gapic_v1._config_helpers._use_client_cert_effective"
27+
"google.api_core.gapic_v1.config_helpers.use_client_cert_effective"
2828
) # noqa: E501
2929
@mock.patch.dict(os.environ, clear=True)
3030
def test_read_environment_variables(mock_effective):
3131
mock_effective.return_value = True
3232
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "always"
3333
os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "custom.com"
3434

35-
cert, mtls, domain = _read_environment_variables()
35+
cert, mtls, domain = read_environment_variables()
3636
assert cert is True
3737
assert mtls == "always"
3838
assert domain == "custom.com"
@@ -44,4 +44,4 @@ def test_read_environment_variables_invalid_mtls():
4444
with pytest.raises(
4545
MutualTLSChannelError, match="must be `never`, `auto` or `always`"
4646
):
47-
_read_environment_variables()
47+
read_environment_variables()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
# limitations under the License.
1515

1616

17-
from google.api_core.gapic_v1._method_helpers import _setup_request_id
17+
from google.api_core.gapic_v1.method_helpers import setup_request_id
1818

1919

2020
def test_setup_request_id():
2121
import uuid
2222

2323
# test dict request
2424
req = {}
25-
_setup_request_id(req, "request_id", True)
25+
setup_request_id(req, "request_id", True)
2626
assert "request_id" in req
2727
uuid_str = req["request_id"]
2828
uuid.UUID(uuid_str) # verify it is a valid UUID
2929

3030
# test dict request when already set
3131
req = {"request_id": "existing"}
32-
_setup_request_id(req, "request_id", True)
32+
setup_request_id(req, "request_id", True)
3333
assert req["request_id"] == "existing"
3434

3535
class DummyRequest:
@@ -43,13 +43,13 @@ def HasField(self, field_name):
4343

4444
# test object request proto3 optional true
4545
req_obj = DummyRequest()
46-
_setup_request_id(req_obj, "request_id", True)
46+
setup_request_id(req_obj, "request_id", True)
4747
assert req_obj.request_id != ""
4848
uuid.UUID(req_obj.request_id)
4949

5050
# test object request proto3 optional false
5151
req_obj2 = DummyRequest()
52-
_setup_request_id(req_obj2, "request_id", False)
52+
setup_request_id(req_obj2, "request_id", False)
5353
assert req_obj2.request_id != ""
5454
uuid.UUID(req_obj2.request_id)
5555

@@ -59,6 +59,6 @@ def __init__(self):
5959

6060
# test custom non-iterable object wrapper
6161
req_obj3 = CustomRequestWrapper()
62-
_setup_request_id(req_obj3, "request_id", True)
62+
setup_request_id(req_obj3, "request_id", True)
6363
assert req_obj3.request_id != ""
6464
uuid.UUID(req_obj3.request_id)

0 commit comments

Comments
 (0)