From f0ccd6157ac997c61ff6577c2494158bff37dd2e Mon Sep 17 00:00:00 2001 From: ohmayr Date: Fri, 20 Sep 2024 06:34:24 +0000 Subject: [PATCH] chore: refactor mixins call method --- .../%sub/services/%service/_shared_macros.j2 | 58 +++++++++++++++++++ .../%service/transports/_rest_mixins.py.j2 | 48 +-------------- .../services/asset_service/transports/rest.py | 3 +- .../services/eventarc/transports/rest.py | 21 ++++--- .../services/cloud_redis/transports/rest.py | 12 ++-- 5 files changed, 83 insertions(+), 59 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2 index 331fbca6c..e1507eb1d 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2 @@ -371,3 +371,61 @@ class {{ async_method_name_prefix }}{{ service.name }}RestInterceptor: {% endfor %} {% endif %} {% endmacro %} + +{% macro generate_mixin_call_method(service, api, name, sig, is_async) %} +{% set async_prefix = "async " if is_async else "" %} +{% set async_method_name_prefix = "Async" if is_async else "" %} +{% set async_suffix = "_async" if is_async else "" %} +{% set await_prefix = "await " if is_async else "" %} + +@property +def {{ name|snake_case }}(self): + return self._{{ name }}(self._session, self._host, self._interceptor) # type: ignore + +class _{{ name }}(_Base{{ service.name }}RestTransport._Base{{name}}, {{ async_method_name_prefix }}{{service.name}}RestStub): + {% set body_spec = api.mixin_http_options["{}".format(name)][0].body %} + {{ response_method(body_spec) | indent(4) }} + + {{ async_prefix }}def __call__(self, + request: {{ sig.request_type }}, *, + retry: OptionalRetry=gapic_v1.method.DEFAULT, + timeout: Optional[float]=None, + metadata: Sequence[Tuple[str, str]]=(), + ) -> {{ sig.response_type }}: + + r"""Call the {{- ' ' -}} + {{ (name|snake_case).replace('_',' ')|wrap( + width=70, offset=45, indent=8) }} + {{- ' ' -}} method over HTTP. + + Args: + request ({{ sig.request_type }}): + The request object for {{ name }} method. + retry (google.api_core.retry{{ async_suffix }}.{{ async_method_name_prefix }}Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + {% if sig.response_type != 'None' %} + + Returns: + {{ sig.response_type }}: Response from {{ name }} method. + {% endif %} + """ + {{ rest_call_method_common(body_spec, name, service.name, is_async)|indent(4) }} + + {% if sig.response_type == "None" %} + return {{ await_prefix }}self._interceptor.post_{{ name|snake_case }}(None) + {% else %} + {% if is_async %} + content = await response.read() + {% else %} + content = response.content.decode("utf-8") + {% endif %} + resp = {{ sig.response_type }}() + resp = json_format.Parse(content, resp) + resp = {{ await_prefix }}self._interceptor.post_{{ name|snake_case }}(resp) + return resp + {% endif %} + +{% endmacro %} \ No newline at end of file diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/_rest_mixins.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/_rest_mixins.py.j2 index f01102a18..691b98f0e 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/_rest_mixins.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/_rest_mixins.py.j2 @@ -17,53 +17,7 @@ {% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %} {% if "rest" in opts.transport %} - {% for name, sig in api.mixin_api_signatures.items() %} - @property - def {{ name|snake_case }}(self): - return self._{{ name }}(self._session, self._host, self._interceptor) # type: ignore - - class _{{ name }}(_Base{{ service.name }}RestTransport._Base{{name}}, {{service.name}}RestStub): - {% set body_spec = api.mixin_http_options["{}".format(name)][0].body %} - {{ shared_macros.response_method(body_spec)|indent(8) }} - - def __call__(self, - request: {{ sig.request_type }}, *, - retry: OptionalRetry=gapic_v1.method.DEFAULT, - timeout: Optional[float]=None, - metadata: Sequence[Tuple[str, str]]=(), - ) -> {{ sig.response_type }}: - - r"""Call the {{- ' ' -}} - {{ (name|snake_case).replace('_',' ')|wrap( - width=70, offset=45, indent=8) }} - {{- ' ' -}} method over HTTP. - - Args: - request ({{ sig.request_type }}): - The request object for {{ name }} method. - retry (google.api_core.retry.Retry): Designation of what errors, if any, - should be retried. - timeout (float): The timeout for this request. - metadata (Sequence[Tuple[str, str]]): Strings which should be - sent along with the request as metadata. - {% if sig.response_type != 'None' %} - - Returns: - {{ sig.response_type }}: Response from {{ name }} method. - {% endif %} - """ - {{ shared_macros.rest_call_method_common(body_spec, name, service.name)|indent(8) }} - - {% if sig.response_type == "None" %} - return self._interceptor.post_{{ name|snake_case }}(None) - {% else %} - - resp = {{ sig.response_type }}() - resp = json_format.Parse(response.content.decode("utf-8"), resp) - resp = self._interceptor.post_{{ name|snake_case }}(resp) - return resp - {% endif %} - + {{ shared_macros.generate_mixin_call_method(service, api, name, sig, is_async=False) | indent(4) }} {% endfor %} {% endif %} {# rest in opts.transport #} diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/rest.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/rest.py index 4c86709c4..bfa274eae 100755 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/rest.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/rest.py @@ -2677,8 +2677,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = operations_pb2.Operation() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_operation(resp) return resp diff --git a/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/transports/rest.py b/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/transports/rest.py index be3f58cf1..26aec10e1 100755 --- a/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/transports/rest.py +++ b/tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/transports/rest.py @@ -2405,8 +2405,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = locations_pb2.Location() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_location(resp) return resp @@ -2474,8 +2475,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = locations_pb2.ListLocationsResponse() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_list_locations(resp) return resp @@ -2543,8 +2545,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = policy_pb2.Policy() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_iam_policy(resp) return resp @@ -2615,8 +2618,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = policy_pb2.Policy() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_set_iam_policy(resp) return resp @@ -2687,8 +2691,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = iam_policy_pb2.TestIamPermissionsResponse() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_test_iam_permissions(resp) return resp @@ -2885,8 +2890,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = operations_pb2.Operation() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_operation(resp) return resp @@ -2954,8 +2960,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = operations_pb2.ListOperationsResponse() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_list_operations(resp) return resp diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/rest.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/rest.py index 5d99f7e2c..609f18612 100755 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/rest.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/rest.py @@ -1593,8 +1593,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = locations_pb2.Location() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_location(resp) return resp @@ -1662,8 +1663,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = locations_pb2.ListLocationsResponse() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_list_locations(resp) return resp @@ -1857,8 +1859,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = operations_pb2.Operation() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_get_operation(resp) return resp @@ -1926,8 +1929,9 @@ def __call__(self, if response.status_code >= 400: raise core_exceptions.from_http_response(response) + content = response.content.decode("utf-8") resp = operations_pb2.ListOperationsResponse() - resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = json_format.Parse(content, resp) resp = self._interceptor.post_list_operations(resp) return resp