Skip to content

Commit 93fcb6d

Browse files
committed
feat: delegate REST transcoding to google-api-core
1 parent 2e4481a commit 93fcb6d

6 files changed

Lines changed: 23 additions & 76 deletions

File tree

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,23 @@ def _get_http_options():
198198
service: The service.
199199
is_async (bool): Used to determine the code path i.e. whether for sync or async call.
200200
is_request_message_proto_plus_type (bool): Used to determine whether the request message is a proto-plus type. #}
201-
{% macro rest_call_method_common(body_spec, method_name, service, is_async=False, is_request_message_proto_plus_type=False) %}
201+
{% macro rest_call_method_common(body_spec, method_name, service, is_async=False, is_request_message_proto_plus_type=False, rest_numeric_enums=False) %}
202202
{% set service_name = service.name %}
203203
{% set await_prefix = "await " if is_async else "" %}
204204
{% set async_class_prefix = "Async" if is_async else "" %}
205205

206206
http_options = _Base{{ service_name }}RestTransport._Base{{method_name}}._get_http_options()
207-
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2274): Add debug log before intercepting a request #}
208207
request, metadata = {{ await_prefix }}self._interceptor.pre_{{ method_name|snake_case }}(request, metadata)
209-
transcoded_request = _Base{{ service_name }}RestTransport._Base{{method_name}}._get_transcoded_request(http_options, request)
210-
211-
{% if body_spec %}
212-
body = _Base{{ service_name }}RestTransport._Base{{method_name}}._get_request_body_json(transcoded_request)
213-
{% endif %}{# body_spec #}
214-
215-
# Jsonify the query params
216-
query_params = _Base{{ service_name }}RestTransport._Base{{method_name}}._get_query_params_json(transcoded_request)
208+
transcoded_request, body, query_params = gapic_v1.rest_helpers.transcode(
209+
http_options,
210+
request,
211+
required_fields_default_values=getattr(
212+
_Base{{ service_name }}RestTransport._Base{{method_name}},
213+
"__REQUIRED_FIELDS_DEFAULT_VALUES",
214+
None,
215+
),
216+
rest_numeric_enums={{ rest_numeric_enums }},
217+
)
217218

218219
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(logging.DEBUG): # pragma: NO COVER
219220
request_url = "{host}{uri}".format(host=self._host, uri=transcoded_request['uri'])

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/_rest_mixins_base.py.j2

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,7 @@
2525

2626
{{ shared_macros.http_options_method(api.mixin_http_options["{}".format(name)])|indent(8)}}
2727

28-
@staticmethod
29-
def _get_transcoded_request(http_options, request):
30-
request_kwargs = json_format.MessageToDict(request)
31-
transcoded_request = path_template.transcode(
32-
http_options, **request_kwargs)
33-
return transcoded_request
34-
35-
{% set body_spec = api.mixin_http_options["{}".format(name)][0].body %}
36-
{%- if body_spec %}
37-
38-
@staticmethod
39-
def _get_request_body_json(transcoded_request):
40-
body = json.dumps(transcoded_request['body'])
41-
return body
42-
43-
{%- endif %} {# body_spec #}
44-
45-
@staticmethod
46-
def _get_query_params_json(transcoded_request):
47-
query_params = json.loads(json.dumps(transcoded_request['query_params']))
48-
return query_params
28+
pass
4929

5030
{% endfor %}
5131
{% endif %} {# rest in opts.transport #}

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class {{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
245245
{% endif %}
246246
"""
247247

248-
{{ shared_macros.rest_call_method_common(body_spec, method.name, service, False, method.input.ident.is_proto_plus_type)|indent(8) }}
248+
{{ shared_macros.rest_call_method_common(body_spec, method.name, service, False, method.input.ident.is_proto_plus_type, opts.rest_numeric_enums)|indent(8) }}
249249

250250
{% if not method.void %}
251251
# Return the response

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest_asyncio.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Async{{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
203203
{% endif %}
204204
"""
205205

206-
{{ shared_macros.rest_call_method_common(body_spec, method.name, service, True, method.input.ident.is_proto_plus_type)|indent(8) }}
206+
{{ shared_macros.rest_call_method_common(body_spec, method.name, service, True, method.input.ident.is_proto_plus_type, opts.rest_numeric_enums)|indent(8) }}
207207

208208
{% if not method.void %}
209209
# Return the response

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest_base.py.j2

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,51 +120,8 @@ class _Base{{ service.name }}RestTransport({{service.name}}Transport):
120120
def _get_unset_required_fields(cls, message_dict):
121121
return {k: v for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() if k not in message_dict}
122122
{% endif %}{# required fields #}
123-
124123
{% set method_http_options = method.http_options %}
125-
126124
{{ shared_macros.http_options_method(method_http_options)|indent(8) }}
127-
128-
@staticmethod
129-
def _get_transcoded_request(http_options, request):
130-
{% if method.input.ident.is_proto_plus_type %}
131-
pb_request = {{method.input.ident}}.pb(request)
132-
{% else %}
133-
pb_request = request
134-
{% endif %}
135-
transcoded_request = path_template.transcode(http_options, pb_request)
136-
return transcoded_request
137-
138-
{% set body_spec = method.http_options[0].body %}
139-
{%- if body_spec %}
140-
141-
@staticmethod
142-
def _get_request_body_json(transcoded_request):
143-
# Jsonify the request body
144-
145-
body = json_format.MessageToJson(
146-
transcoded_request['body'],
147-
use_integers_for_enums={{ opts.rest_numeric_enums }}
148-
)
149-
return body
150-
151-
{%- endif %}{# body_spec #}
152-
153-
@staticmethod
154-
def _get_query_params_json(transcoded_request):
155-
query_params = json.loads(json_format.MessageToJson(
156-
transcoded_request['query_params'],
157-
use_integers_for_enums={{ opts.rest_numeric_enums }},
158-
))
159-
{% if method.input.required_fields %}
160-
query_params.update(_Base{{ service.name }}RestTransport._Base{{method.name}}._get_unset_required_fields(query_params))
161-
{% endif %}{# required fields #}
162-
163-
{% if opts.rest_numeric_enums %}
164-
query_params["$alt"] = "json;enum-encoding=int"
165-
{% endif %}
166-
return query_params
167-
168125
{% endif %}{# method.http_options and not method.client_streaming #}
169126
{% endfor %}
170127

packages/gapic-generator/noxfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ def showcase_library(
350350
)
351351

352352
# Install the generated showcase library.
353+
session.install("-e", "../google-api-core")
353354
if templates == "DEFAULT":
354355
# Use the constraints file for the specific python runtime version.
355356
# We do this to make sure that we're testing against the lowest
@@ -380,6 +381,9 @@ def showcase_library(
380381
# Install the library without a constraints file.
381382
session.install("-e", tmp_dir)
382383

384+
import shutil
385+
shutil.rmtree("generated_showcase", ignore_errors=True)
386+
shutil.copytree(tmp_dir, "generated_showcase")
383387
yield tmp_dir
384388

385389

@@ -504,6 +508,10 @@ def run_showcase_unit_tests(session, fail_under=100, rest_async_io_enabled=False
504508
session.run("python", "-m", "pip", "freeze")
505509

506510
# Run the tests.
511+
import os
512+
env = os.environ.copy()
513+
env["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = "false"
514+
env["CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE"] = ""
507515
session.run(
508516
"py.test",
509517
*(
@@ -517,6 +525,7 @@ def run_showcase_unit_tests(session, fail_under=100, rest_async_io_enabled=False
517525
path.join("tests", "unit"),
518526
]
519527
),
528+
env=env,
520529
)
521530

522531

0 commit comments

Comments
 (0)