Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ def _get_http_options():
payload = json.loads(content.decode('utf-8'))
request_url = "{host}{uri}".format(host=self._host, uri=transcoded_request['uri'])
method = transcoded_request['method']
raise core_exceptions.format_http_response_error(response, method, request_url, payload)
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2137): Remove `type: ignore` once version check is added for google-api-core. #}
raise core_exceptions.format_http_response_error(response, method, request_url, payload) # type: ignore
{% else %}
raise core_exceptions.from_http_response(response)
{% endif %}{# is_async #}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ from google.auth.aio import credentials as ga_credentials_async # type: ignore
from google.api_core import exceptions as core_exceptions
from google.api_core import gapic_v1
from google.api_core import retry_async as retries
from google.api_core import rest_helpers

from google.protobuf import json_format

import json # type: ignore
import dataclasses
from typing import Any, Callable, Tuple, Optional, Sequence, Union

Expand Down Expand Up @@ -109,16 +113,73 @@ class Async{{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
def __hash__(self):
return hash("Async{{service.name}}RestTransport.{{method.name}}")

{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2168): Implement server streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2169): Implement client streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2170): Implement long running operation method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2171): Implement pager method. #}
{% if method.http_options and not method.client_streaming and not method.server_streaming and not method.lro and not method.extended_lro and not method.paged_result_field %}
{% set body_spec = method.http_options[0].body %}
{{ shared_macros.response_method(body_spec, is_async=True)|indent(8) }}

{% endif %}{# method.http_options and not method.client_streaming and not method.server_streaming and not method.lro and not method.extended_lro and not method.paged_result_field #}
async def __call__(self,
request: {{method.input.ident}}, *,
retry: OptionalRetry=gapic_v1.method.DEFAULT,
timeout: Optional[float]=None,
metadata: Sequence[Tuple[str, str]]=(),
{# TODO(b/362949446): Update the return type as we implement this for different method types. #}
) -> None:
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2168): Update return type for server streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2169): Update return type for client streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2170): Update the return type for long running operation method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2171): Update the return type for pager method. #}
){% if not method.void %} -> {% if not method.server_streaming %}{{method.output.ident}}{% else %}None{% endif %}{% endif %}:
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2168): Implement server streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2169): Implement client streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2170): Implement long running operation method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2171): Implement pager method. #}
{% if method.http_options and not method.client_streaming and not method.server_streaming and not method.lro and not method.extended_lro and not method.paged_result_field %}
r"""Call the {{- ' ' -}}
{{ (method.name|snake_case).replace('_',' ')|wrap(
width=70, offset=45, indent=8) }}
{{- ' ' -}} method over HTTP.

Args:
request (~.{{ method.input.ident }}):
The request object.{{ ' ' }}
{{- method.input.meta.doc|rst(width=72, indent=16, nl=False) }}
retry (google.api_core.retry_async.AsyncRetry): 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 not method.void %}

Returns:
~.{{ method.output.ident }}:
{{ method.output.meta.doc|rst(width=72, indent=16) }}
{% endif %}
"""

{{ shared_macros.rest_call_method_common(body_spec, method.name, service.name, is_async=True)|indent(8) }}

{% if not method.void %}
# Return the response
resp = {{method.output.ident}}()
{% if method.output.ident.is_proto_plus_type %}
pb_resp = {{method.output.ident}}.pb(resp)
{% else %}
pb_resp = resp
{% endif %}
content = await response.read()
json_format.Parse(content, pb_resp, ignore_unknown_fields=True)
return resp

{% endif %}{# method.void #}

{% else %}
raise NotImplementedError(
"Method {{ method.name }} is not available over REST transport"
)
{% endif %}{# method.http_options and not method.client_streaming and not method.server_streaming and not method.lro and not method.extended_lro and not method.paged_result_field #}

{% endfor %}
{% for method in service.methods.values()|sort(attribute="name") %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,9 @@ def test_{{ method_name }}_raw_page_lro():
{% with method_name = method.safe_name|snake_case + "_unary" if method.extended_lro and not full_extended_lro else method.name|snake_case, method_output = method.extended_lro.operation_type if method.extended_lro and not full_extended_lro else method.output %}{% if method.http_options %}
{# TODO(kbandes): remove this if condition when lro and client streaming are supported. #}
{% if not method.client_streaming %}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2168): Remove unit test for server streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2170): Remove unit test for long running operation method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2171): Remove unit test for pager method. #}
{# NOTE: This guard is added to avoid generating duplicate tests for methods which are tested elsewhere. As we implement each of the api methods
# in the `macro::call_success_test`, the case will be removed from this condition below.
# TODO(https://github.com/googleapis/gapic-generator-python/issues/2143): Remove the test `test_{{ method_name }}_rest` from here once the linked issue is resolved.
Expand Down Expand Up @@ -1940,7 +1943,7 @@ def test_unsupported_parameter_rest_asyncio():
# There are no plans to add support for client streaming.
#}
{% macro is_rest_unsupported_method(method, is_async) %}
{%- if method.client_streaming or is_async -%}
{%- if method.client_streaming or (is_async and (method.server_streaming or method.lro or method.extended_lro or method.paged_result_field)) -%}
{{'True'}}
{%- else -%}
{{'False'}}
Expand Down Expand Up @@ -2083,6 +2086,9 @@ def test_initialize_client_w_{{transport_name}}():
# (method.extended_lro and not full_extended_lro)
#}
{% set method_output = method.output %}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2168): Add unit test for server streaming method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2170): Add unit test for long running operation method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2171): Add unit test for pager method. #}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2143): Update the guard below as we add support for each method, and keep it in sync with the guard in
# `rest_required_tests`, which should be the exact opposite. Remove it once we have all the methods supported in async rest transport that are supported in sync rest transport.
#}
Expand Down Expand Up @@ -2230,10 +2236,10 @@ def test_initialize_client_w_{{transport_name}}():
json_return_value = json_format.MessageToJson(return_value)
{% endif %}{# method.void #}
{% if is_async %}
response_value.read = mock.AsyncMock(return_value=b'{}')
response_value.read = mock.AsyncMock(return_value=json_return_value.encode('UTF-8'))
{% else %}{# is_async #}
response_value.content = json_return_value.encode('UTF-8')
{% endif %}
{% endif %}{# is_async #}
req.return_value = response_value
response = {{ await_prefix }}client.{{ method_name }}(request)

Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def showcase_unit_w_rest_async(
# TODO(https://github.com/googleapis/python-api-core/pull/694): Update the version of google-api-core once the linked PR is merged.
session.install('--no-cache-dir', '--force-reinstall', "google-api-core[grpc]@git+https://github.com/googleapis/python-api-core.git@7dea20d73878eca93b61bb82ae6ddf335fb3a8ca")
# TODO(https://github.com/googleapis/google-auth-library-python/pull/1577): Update the version of google-auth once the linked PR is merged.
session.install('--no-cache-dir', '--force-reinstall', "google-auth@git+https://github.com/googleapis/google-auth-library-python.git@add-support-for-async-authorized-session-api")
session.install('--no-cache-dir', '--force-reinstall', "google-auth@git+https://github.com/googleapis/google-auth-library-python.git")
session.install("aiohttp")
run_showcase_unit_tests(session)

Expand Down
Loading