Skip to content

Commit

Permalink
feat: implement async rest mixin methods (#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr authored Sep 25, 2024
1 parent bedbab8 commit ed04b26
Show file tree
Hide file tree
Showing 26 changed files with 1,247 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _get_http_options():
{% endmacro %}


{% macro prep_wrapped_messages_async_method(service) %}
{% macro prep_wrapped_messages_async_method(api, service) %}
def _prep_wrapped_messages(self, client_info):
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
self._wrapped_methods = {
Expand Down Expand Up @@ -265,6 +265,13 @@ def _prep_wrapped_messages(self, client_info):
client_info=client_info,
),
{% endfor %}{# service.methods.values() #}
{% for method_name in api.mixin_api_methods.keys() %}
self.{{ method_name|snake_case }}: self._wrap_method(
self.{{ method_name|snake_case }},
default_timeout=None,
client_info=client_info,
),
{% endfor %} {# method_name in api.mixin_api_methods.keys() #}
}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.list_operations,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.list_operations]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -91,11 +87,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.get_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.get_operation]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -151,11 +143,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.delete_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.delete_operation]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -206,11 +194,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.cancel_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.cancel_operation]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -264,11 +248,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.wait_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.wait_operation]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -391,11 +371,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.set_iam_policy,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.set_iam_policy]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -514,11 +490,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.get_iam_policy,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.get_iam_policy]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -575,11 +547,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.test_iam_permissions,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.test_iam_permissions]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -635,11 +603,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.get_location,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.get_location]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -691,11 +655,7 @@

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.list_locations,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.list_locations]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def _get_http_options():
{% endmacro %}


{% macro prep_wrapped_messages_async_method(service) %}
{% macro prep_wrapped_messages_async_method(api, service) %}
def _prep_wrapped_messages(self, client_info):
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
self._wrapped_methods = {
Expand Down Expand Up @@ -258,6 +258,16 @@ def _prep_wrapped_messages(self, client_info):
client_info=client_info,
),
{% endfor %}{# service.methods.values() #}
{% for method_name in api.mixin_api_methods.keys() %}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2197): Use `transport_safe_name` similar
# to what we do for non-mixin methods above.
#}
self.{{ method_name|snake_case }}: self._wrap_method(
self.{{ method_name|snake_case }},
default_timeout=None,
client_info=client_info,
),
{% endfor %}{# method_name in api.mixin_api_methods.keys() #}
}
{% endmacro %}

Expand Down Expand Up @@ -310,6 +320,7 @@ class {{ async_method_name_prefix }}{{ service.name }}RestInterceptor:
{{ async_prefix }}def post_{{ method.name|snake_case }}(self, response):
logging.log(f"Received response: {response}")
return response

{% endif %}

{% endfor %}
Expand Down Expand Up @@ -342,12 +353,11 @@ class {{ async_method_name_prefix }}{{ service.name }}RestInterceptor:
it is returned to user code.
"""
return response

{% endif %}
{% endif %}{# if (not is_async) or (is_async and not method.lro and not method.extended_lro and not method.paged_result_field) #}
{% endfor %}

{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2148): Remove the condition below once mixins are supported for async rest transport. #}
{% if not is_async %}
{% for name, signature in api.mixin_api_signatures.items() %}
{{ async_prefix }}def pre_{{ name|snake_case }}(
self, request: {{signature.request_type}}, metadata: Sequence[Tuple[str, str]]
Expand All @@ -369,11 +379,15 @@ class {{ async_method_name_prefix }}{{ service.name }}RestInterceptor:
it is returned to user code.
"""
return response

{% endfor %}
{% endif %}
{% endmacro %}

{% macro generate_mixin_call_method(service, api, name, sig, is_async) %}
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2198): generate _Mixin classes
# and @property methods into separate macros so that _Method and _Mixin classes can be defined all
# together and the @property methods for each can be defined after the class definitions.
#}
{% 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 "" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,7 @@ class {{ service.async_client_name }}:

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.set_iam_policy,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.set_iam_policy]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -632,11 +628,7 @@ class {{ service.async_client_name }}:

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.get_iam_policy,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.get_iam_policy]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down Expand Up @@ -689,11 +681,7 @@ class {{ service.async_client_name }}:

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.test_iam_permissions,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.test_iam_permissions]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
return self._stubs["test_iam_permissions"]
{% endif %}

{{ shared_macros.prep_wrapped_messages_async_method(service)|indent(4) }}
{{ shared_macros.prep_wrapped_messages_async_method(api, service)|indent(4) }}

{{ shared_macros.wrap_async_method_macro()|indent(4) }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Async{{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
self._wrap_with_kind = True
self._prep_wrapped_messages(client_info)

{{ shared_macros.prep_wrapped_messages_async_method(service)|indent(4) }}
{{ shared_macros.prep_wrapped_messages_async_method(api, service)|indent(4) }}

{{ shared_macros.wrap_async_method_macro()|indent(4) }}

Expand Down Expand Up @@ -225,6 +225,9 @@ class Async{{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
return self._{{method.name}}(self._session, self._host, self._interceptor) # type: ignore

{% endfor %}
{% for name, sig in api.mixin_api_signatures.items() %}
{{ shared_macros.generate_mixin_call_method(service, api, name, sig, is_async=True) | indent(4) }}
{% endfor %}

@property
def kind(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1884,11 +1884,8 @@ def test_unsupported_parameter_rest_asyncio():
{% endfor %}{# for method in service.methods.values() #}
{% for name, sig in api.mixin_api_signatures.items() %}
{% if 'rest' in transport %}
{# TODO: Remove the following condition as we implement async mixins #}
{% if not is_async %}
{{ bad_request_mixins_test(service, api, name, sig, transport, is_async) }}
{{ call_success_mixins_test(service, api, name, sig, transport, is_async) }}
{% endif %}{# if not is_async #}
{% endif %}{# if 'rest' in transport #}
{% endfor %}
{{ initialize_client_with_transport_test(service, transport, is_async) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3100,11 +3100,7 @@ async def get_operation(

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.get_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)
rpc = self.transport._wrapped_methods[self._client._transport.get_operation]

# Certain fields should be provided within the metadata header;
# add these here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,11 @@ def _prep_wrapped_messages(self, client_info):
default_timeout=None,
client_info=client_info,
),
self.get_operation: self._wrap_method(
self.get_operation,
default_timeout=None,
client_info=client_info,
),
}

def _wrap_method(self, func, *args, **kwargs):
Expand Down
Loading

0 comments on commit ed04b26

Please sign in to comment.