Skip to content

Commit 7e47c49

Browse files
committed
fix(api-core): add transcode alias, input validation, and gapic_v1 re-export
1 parent 2c3a593 commit 7e47c49

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.api_core.rest_helpers import (
16+
transcode,
17+
transcode_request,
18+
)
19+
20+
__all__ = ["transcode", "transcode_request"]

packages/google-api-core/google/api_core/rest_helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def transcode_request(
135135
- The serialized request body JSON string, or None if no body.
136136
- The query parameters dictionary.
137137
"""
138+
if request is None:
139+
raise TypeError("request cannot be None")
140+
138141
# Convert proto-plus message to its underlying protobuf message if needed
139142
pb_request = getattr(request, "_pb", request)
140143

@@ -163,3 +166,7 @@ def transcode_request(
163166
query_params_json["$alt"] = "json;enum-encoding=int"
164167

165168
return transcoded_request, body_json, query_params_json
169+
170+
171+
transcode = transcode_request
172+

packages/google-api-core/tests/unit/test_rest_helpers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,34 @@ def test_transcode_with_required_fields_existing_key():
252252
)
253253

254254
assert query_params["name"] == "custom-name"
255+
256+
257+
def test_transcode_alias_and_gapic_v1_import():
258+
from google.api_core.gapic_v1.rest_helpers import (
259+
transcode as tr_gapic,
260+
transcode_request as tr_req_gapic,
261+
)
262+
from google.api_core.rest_helpers import transcode as tr_top
263+
264+
assert tr_gapic is transcode_request
265+
assert tr_req_gapic is transcode_request
266+
assert tr_top is transcode_request
267+
268+
269+
def test_transcode_request_invalid_request():
270+
http_options = [{"method": "get", "uri": "/v1/test"}]
271+
with pytest.raises(TypeError, match="request cannot be None"):
272+
transcode_request(http_options, None)
273+
274+
275+
def test_transcode_request_proto_plus_wrapper():
276+
http_options = [{"method": "get", "uri": "/v1/test/{name}"}]
277+
mock_pb = descriptor_pb2.FieldDescriptorProto()
278+
mock_pb.name = "proto-plus-field"
279+
280+
mock_proto_plus = mock.Mock()
281+
mock_proto_plus._pb = mock_pb
282+
283+
transcoded, _, _ = transcode_request(http_options, mock_proto_plus)
284+
assert transcoded["uri"] == "/v1/test/proto-plus-field"
285+

0 commit comments

Comments
 (0)