Skip to content

Commit 393fd85

Browse files
committed
fix: address code review feedback for GAPIC centralization
1 parent ed6438e commit 393fd85

9 files changed

Lines changed: 102 additions & 70 deletions

File tree

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.api_core.gapic_v1 import client_info
16-
from google.api_core.gapic_v1 import routing_header
15+
from google.api_core.gapic_v1 import (
16+
client_info,
17+
config,
18+
config_async,
19+
method,
20+
method_async,
21+
routing_header,
22+
)
1723

1824
__all__ = [
1925
"client_info",
26+
"config",
27+
"config_async",
28+
"method",
29+
"method_async",
2030
"routing_header",
2131
]
22-
23-
try:
24-
import grpc # noqa: F401
25-
26-
from google.api_core.gapic_v1 import config # noqa: F401
27-
from google.api_core.gapic_v1 import config_async # noqa: F401
28-
from google.api_core.gapic_v1 import method # noqa: F401
29-
from google.api_core.gapic_v1 import method_async # noqa: F401
30-
31-
__all__.extend(["config", "config_async", "method", "method_async"])
32-
except ImportError: # pragma: NO COVER
33-
pass

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"""Helpers for client certificate handling and mTLS authentication."""
1818

1919
import os
20-
from typing import Any, Optional
20+
from typing import Callable, Optional, Tuple
2121

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,9 +53,10 @@ def use_client_cert_effective() -> bool:
5353
return use_client_cert_str == "true"
5454

5555

56-
def get_client_cert_source(
57-
provided_cert_source: Optional[Any], use_cert_flag: bool
58-
) -> Optional[Any]:
56+
def _get_client_cert_source(
57+
provided_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]],
58+
use_cert_flag: bool,
59+
) -> Optional[Callable[[], Tuple[bytes, bytes]]]:
5960
"""Return the client cert source to be used by the client.
6061
6162
Args:

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import os
2020
from typing import Optional, Tuple
2121

22-
from google.api_core.gapic_v1._client_cert import use_client_cert_effective
2322
from google.auth.exceptions import MutualTLSChannelError # type: ignore
2423

24+
from google.api_core.gapic_v1._client_cert import _use_client_cert_effective
2525

26-
def read_environment_variables() -> Tuple[bool, str, Optional[str]]:
26+
27+
def _read_environment_variables() -> Tuple[bool, str, Optional[str]]:
2728
"""Returns the environment variables used by the client.
2829
2930
Returns:
@@ -38,7 +39,7 @@ def read_environment_variables() -> Tuple[bool, str, Optional[str]]:
3839
GOOGLE_API_USE_MTLS_ENDPOINT is not any of
3940
["auto", "never", "always"].
4041
"""
41-
use_client_cert = use_client_cert_effective()
42+
use_client_cert = _use_client_cert_effective()
4243
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto").lower()
4344
universe_domain_env = os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN")
4445
if use_mtls_endpoint not in ("auto", "never", "always"):

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

Lines changed: 4 additions & 5 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,
@@ -74,8 +74,7 @@ def get_api_endpoint(
7474
):
7575
if universe_domain.lower() != default_universe.lower():
7676
raise MutualTLSChannelError(
77-
f"mTLS is not supported in any universe other than "
78-
f"{default_universe}."
77+
f"mTLS is not supported in any universe other than {default_universe}."
7978
)
8079
return default_mtls_endpoint
8180
else:
@@ -86,7 +85,7 @@ def get_api_endpoint(
8685
)
8786

8887

89-
def get_universe_domain(
88+
def _get_universe_domain(
9089
client_universe_domain: Optional[str],
9190
universe_domain_env: Optional[str],
9291
default_universe: str,

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@
1818

1919
import pytest
2020

21-
2221
from google.api_core.gapic_v1._client_cert import (
23-
get_client_cert_source,
24-
use_client_cert_effective,
22+
_get_client_cert_source,
23+
_use_client_cert_effective,
2524
)
2625

2726

2827
@mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True)
2928
def test_use_client_cert_effective_with_google_auth(mock_method):
3029
# Test when google-auth supports the method
3130
mock_method.return_value = True
32-
assert use_client_cert_effective() is True
31+
assert _use_client_cert_effective() is True
3332

3433
mock_method.return_value = False
35-
assert use_client_cert_effective() is False
34+
assert _use_client_cert_effective() is False
3635

3736

3837
@mock.patch.dict(os.environ, {}, clear=True)
@@ -42,23 +41,23 @@ def test_use_client_cert_effective_fallback():
4241
"google.api_core.gapic_v1._client_cert.hasattr", return_value=False
4342
):
4443
# Default is false
45-
assert use_client_cert_effective() is False
44+
assert _use_client_cert_effective() is False
4645

4746
env_true = {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}
4847
with mock.patch.dict(os.environ, env_true):
49-
assert use_client_cert_effective() is True
48+
assert _use_client_cert_effective() is True
5049

5150
with mock.patch.dict(
5251
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}
5352
):
54-
assert use_client_cert_effective() is False
53+
assert _use_client_cert_effective() is False
5554

5655
with mock.patch.dict(
5756
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}
5857
):
5958
match_str = "must be either `true` or `false`"
6059
with pytest.raises(ValueError, match=match_str):
61-
use_client_cert_effective()
60+
_use_client_cert_effective()
6261

6362

6463
@mock.patch(
@@ -72,10 +71,10 @@ def test_get_client_cert_source(mock_default, mock_has_default):
7271
mock_has_default.return_value = True
7372

7473
# When use_cert_flag is False, return None
75-
assert get_client_cert_source(b"provided", False) is None
74+
assert _get_client_cert_source(b"provided", False) is None
7675

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

8079
# When no provided cert but default is available
81-
assert get_client_cert_source(None, True) == b"default_cert"
80+
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818

1919
import pytest
2020

21-
2221
from google.auth.exceptions import MutualTLSChannelError
2322

24-
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
2524

2625

2726
@mock.patch(
28-
"google.api_core.gapic_v1._config_helpers.use_client_cert_effective"
27+
"google.api_core.gapic_v1._config_helpers._use_client_cert_effective"
2928
) # noqa: E501
3029
@mock.patch.dict(os.environ, clear=True)
3130
def test_read_environment_variables(mock_effective):
3231
mock_effective.return_value = True
3332
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "always"
3433
os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "custom.com"
3534

36-
cert, mtls, domain = read_environment_variables()
35+
cert, mtls, domain = _read_environment_variables()
3736
assert cert is True
3837
assert mtls == "always"
3938
assert domain == "custom.com"
@@ -45,4 +44,4 @@ def test_read_environment_variables_invalid_mtls():
4544
with pytest.raises(
4645
MutualTLSChannelError, match="must be `never`, `auto` or `always`"
4746
):
48-
read_environment_variables()
47+
_read_environment_variables()

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

Lines changed: 15 additions & 5 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,12 +43,22 @@ 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)
55+
56+
class CustomRequestWrapper:
57+
def __init__(self):
58+
self.request_id = ""
59+
60+
# test custom non-iterable object wrapper
61+
req_obj3 = CustomRequestWrapper()
62+
_setup_request_id(req_obj3, "request_id", True)
63+
assert req_obj3.request_id != ""
64+
uuid.UUID(req_obj3.request_id)

0 commit comments

Comments
 (0)