Skip to content

Commit

Permalink
Provide AsyncIO support for generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
lidizheng committed Apr 9, 2020
1 parent d145c47 commit 76fcc19
Show file tree
Hide file tree
Showing 16 changed files with 936 additions and 5 deletions.
68 changes: 68 additions & 0 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,63 @@ def client_output(self):
# Return the usual output.
return self.output

@utils.cached_property
def client_output_async(self):
"""Return the output from the client layer.
This takes into account transformations made by the outer GAPIC
client to transform the output from the transport.
Returns:
Union[~.MessageType, ~.PythonType]:
A description of the return type.
"""
# Void messages ultimately return None.
if self.void:
return PrimitiveType.build(None)

# If this method is an LRO, return a PythonType instance representing
# that.
if self.lro:
return PythonType(meta=metadata.Metadata(
address=metadata.Address(
name='AsyncOperation',
module='operation_async',
package=('google', 'api_core'),
collisions=self.lro.response_type.ident.collisions,
),
documentation=utils.doc(
'An object representing a long-running operation. \n\n'
'The result type for the operation will be '
':class:`{ident}`: {doc}'.format(
doc=self.lro.response_type.meta.doc,
ident=self.lro.response_type.ident.sphinx,
),
),
))

# # If this method is paginated, return that method's pager class.
if self.paged_result_field:
return PythonType(meta=metadata.Metadata(
address=metadata.Address(
name=f'{self.name}AsyncPager',
package=self.ident.api_naming.module_namespace + (self.ident.api_naming.versioned_module_name,) + self.ident.subpackage + (
'services',
utils.to_snake_case(self.ident.parent[-1]),
),
module='pagers',
collisions=self.input.ident.collisions,
),
documentation=utils.doc(
f'{self.output.meta.doc}\n\n'
'Iterating over this object will yield results and '
'resolve additional pages automatically.',
),
))

# Return the usual output.
return self.output

@property
def field_headers(self) -> Sequence[str]:
"""Return the field headers defined for this method."""
Expand Down Expand Up @@ -706,6 +763,8 @@ def _ref_types(self, recursive: bool) -> Sequence[Union[MessageType, EnumType]]:
if not self.void:
answer.append(self.client_output)
answer.extend(self.client_output.field_types)
answer.append(self.client_output_async)
answer.extend(self.client_output_async.field_types)

# If this method has LRO, it is possible (albeit unlikely) that
# the LRO messages reside in a different module.
Expand Down Expand Up @@ -763,6 +822,11 @@ def client_name(self) -> str:
"""Returns the name of the generated client class"""
return self.name + "Client"

@property
def async_client_name(self) -> str:
"""Returns the name of the generated AsyncIO client class"""
return self.name + "AsyncClient"

@property
def transport_name(self):
return self.name + "Transport"
Expand All @@ -771,6 +835,10 @@ def transport_name(self):
def grpc_transport_name(self):
return self.name + "GrpcTransport"

@property
def grpc_asyncio_transport_name(self):
return self.name + "GrpcAsyncIOTransport"

@property
def has_lro(self) -> bool:
"""Return whether the service has a long-running method."""
Expand Down
4 changes: 4 additions & 0 deletions gapic/templates/%namespace/%name/__init__.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ _lazy_name_to_package_map = {
'types': '{% if api.naming.module_namespace %}{{ api.naming.module_namespace|join(".") }}.{% endif -%}{{ api.naming.versioned_module_name }}.types',
{%- for service in api.services.values()|sort(attribute='name')|unique(attribute='name') if service.meta.address.subpackage == api.subpackage_view %}
'{{ service.client_name|snake_case }}': '{% if api.naming.module_namespace %}{{ api.naming.module_namespace|join(".") }}.{% endif -%}{{ api.naming.versioned_module_name }}.services.{{ service.name|snake_case }}.client',
'{{ service.async_client_name|snake_case }}': '{% if api.naming.module_namespace %}{{ api.naming.module_namespace|join(".") }}.{% endif -%}{{ api.naming.versioned_module_name }}.services.{{ service.name|snake_case }}.async_client',
'{{ service.transport_name|snake_case }}': '{% if api.naming.module_namespace %}{{ api.naming.module_namespace|join(".") }}.{% endif -%}{{ api.naming.versioned_module_name }}.services.transports.base',
'{{ service.grpc_transport_name|snake_case }}': '{% if api.naming.module_namespace %}{{ api.naming.module_namespace|join(".") }}.{% endif -%}{{ api.naming.versioned_module_name }}.services.transports.grpc',
{%- endfor %} {# Need to do types and enums #}
Expand Down Expand Up @@ -105,6 +106,8 @@ from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.'
if service.meta.address.subpackage == api.subpackage_view -%}
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif -%}
{{ api.naming.versioned_module_name }}.services.{{ service.name|snake_case }}.client import {{ service.client_name }}
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif -%}
{{ api.naming.versioned_module_name }}.services.{{ service.name|snake_case }}.async_client import {{ service.async_client_name }}
{% endfor -%}

{# Import messages and enums from each proto.
Expand Down Expand Up @@ -141,6 +144,7 @@ __all__ = (
{% for service in api.services.values()|sort(attribute='name')
if service.meta.address.subpackage == api.subpackage_view -%}
'{{ service.client_name }}',
'{{ service.async_client_name }}',
{% endfor -%}
{% for proto in api.protos.values()|sort(attribute='module_name')
if proto.meta.address.subpackage == api.subpackage_view -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

{% block content %}
from .client import {{ service.client_name }}
from .async_client import {{ service.async_client_name }}

__all__ = (
'{{ service.client_name }}',
'{{ service.async_client_name }}',
)
{% endblock %}
Loading

0 comments on commit 76fcc19

Please sign in to comment.