Skip to content

Commit d4272d2

Browse files
vchudnov-gpartheagcf-owl-bot[bot]
committed
feat: Allow representing enums with their unqualified symbolic names in headers (#465)
* feat: Allow non-fully-qualified enums in routing headers * Rename s/fully_qualified_enums/qualified_enums/g for correctness * chore: minor tweaks * chore: Temporary workaround for pytest in noxfile. * Fix import order * bring coverage to 100% * lint * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove replacement in owlbot.py causing lint failure Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent e933e0f commit d4272d2

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

google/api_core/gapic_v1/routing_header.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,52 @@
2020
Generally, these headers are specified as gRPC metadata.
2121
"""
2222

23+
from enum import Enum
2324
from urllib.parse import urlencode
2425

2526
ROUTING_METADATA_KEY = "x-goog-request-params"
2627

2728

28-
def to_routing_header(params):
29+
def to_routing_header(params, qualified_enums=True):
2930
"""Returns a routing header string for the given request parameters.
3031
3132
Args:
3233
params (Mapping[str, Any]): A dictionary containing the request
3334
parameters used for routing.
35+
qualified_enums (bool): Whether to represent enum values
36+
as their type-qualified symbol names instead of as their
37+
unqualified symbol names.
3438
3539
Returns:
3640
str: The routing header string.
41+
3742
"""
43+
if not qualified_enums:
44+
if isinstance(params, dict):
45+
tuples = params.items()
46+
else:
47+
tuples = params
48+
params = [(x[0], x[1].name) if isinstance(x[1], Enum) else x for x in tuples]
3849
return urlencode(
3950
params,
4051
# Per Google API policy (go/api-url-encoding), / is not encoded.
4152
safe="/",
4253
)
4354

4455

45-
def to_grpc_metadata(params):
56+
def to_grpc_metadata(params, qualified_enums=True):
4657
"""Returns the gRPC metadata containing the routing headers for the given
4758
request parameters.
4859
4960
Args:
5061
params (Mapping[str, Any]): A dictionary containing the request
5162
parameters used for routing.
63+
qualified_enums (bool): Whether to represent enum values
64+
as their type-qualified symbol names instead of as their
65+
unqualified symbol names.
5266
5367
Returns:
5468
Tuple(str, str): The gRPC metadata containing the routing header key
5569
and value.
5670
"""
57-
return (ROUTING_METADATA_KEY, to_routing_header(params))
71+
return (ROUTING_METADATA_KEY, to_routing_header(params, qualified_enums))

noxfile.py

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def default(session, install_grpc=True):
9494
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
9595
)
9696

97-
# Install all test dependencies, then install this package in-place.
9897
session.install(
9998
"dataclasses",
10099
"mock",

owlbot.py

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
""",
4848
)
4949

50-
s.replace(".github/workflows/lint.yml", "python-version: \"3.10\"", "python-version: \"3.7\"")
51-
5250
python.configure_previous_major_version_branches()
5351

5452
s.shell.run(["nox", "-s", "blacken"], hide_output=False)

tests/unit/gapic/test_routing_header.py

+31
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from enum import Enum
16+
1517
import pytest
1618

1719
try:
@@ -35,6 +37,35 @@ def test_to_routing_header_with_slashes():
3537
assert value == "name=me/ep&book.read=1%262"
3638

3739

40+
def test_enum_fully_qualified():
41+
class Message:
42+
class Color(Enum):
43+
RED = 1
44+
GREEN = 2
45+
BLUE = 3
46+
47+
params = [("color", Message.Color.RED)]
48+
value = routing_header.to_routing_header(params)
49+
assert value == "color=Color.RED"
50+
value = routing_header.to_routing_header(params, qualified_enums=True)
51+
assert value == "color=Color.RED"
52+
53+
54+
def test_enum_nonqualified():
55+
class Message:
56+
class Color(Enum):
57+
RED = 1
58+
GREEN = 2
59+
BLUE = 3
60+
61+
params = [("color", Message.Color.RED), ("num", 5)]
62+
value = routing_header.to_routing_header(params, qualified_enums=False)
63+
assert value == "color=RED&num=5"
64+
params = {"color": Message.Color.RED, "num": 5}
65+
value = routing_header.to_routing_header(params, qualified_enums=False)
66+
assert value == "color=RED&num=5"
67+
68+
3869
def test_to_grpc_metadata():
3970
params = [("name", "meep"), ("book.read", "1")]
4071
metadata = routing_header.to_grpc_metadata(params)

0 commit comments

Comments
 (0)