Skip to content

Commit

Permalink
feat: add async samples
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed May 4, 2021
1 parent b08fa71 commit 22ecced
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 24 deletions.
15 changes: 10 additions & 5 deletions gapic/samplegen/samplegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ def preprocess_sample(sample, api_schema: api.API, rpc: wrappers.Method):
sample["module_name"] = api_schema.naming.versioned_module_name
sample["module_namespace"] = api_schema.naming.module_namespace

sample["client_name"] = api_schema.services[sample["service"]].client_name
if sample["transport"] == "grpc-async":
sample["client_name"] = api_schema.services[sample["service"]].async_client_name
else:
sample["client_name"] = api_schema.services[sample["service"]].client_name
# the type of the request object passed to the rpc e.g, `ListRequest`
sample["request_type"] = rpc.input.ident.name

Expand Down Expand Up @@ -944,17 +947,19 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A

for service_name, service in gapic_metadata.services.items():
api_short_name = api_schema.services[f"{api_schema.naming.proto_package}.{service_name}"].shortname
for transport_type, client in service.clients.items():
if transport_type == "grpc-async":
# TODO(busunkim): Enable generation of async samples
continue
for transport_name, client in service.clients.items():
if transport_name == "grpc-async":
transport_type = "async"
else:
transport_type = "sync"
for rpc_name, method_list in client.rpcs.items():
# Region Tag Format:
# [{START|END} ${apishortname}_generated_${api}_${apiVersion}_${serviceName}_${rpcName}_{sync|async}_${overloadDisambiguation}]
region_tag = f"{api_short_name}_generated_{api_schema.naming.versioned_module_name}_{service_name}_{rpc_name}_{transport_type}"
spec = {
"sample_type": "standalone",
"rpc": rpc_name,
"transport": transport_name,
"request": [],
# response is populated in `preprocess_sample`
"service": f"{api_schema.naming.proto_package}.{service_name}",
Expand Down
16 changes: 10 additions & 6 deletions gapic/templates/examples/feature_fragments.j2
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ request=request
{% endmacro %}


{% macro render_method_call(sample, calling_form, calling_form_enum) %}
{% macro render_method_call(sample, calling_form, calling_form_enum, transport) %}
{# Note: this doesn't deal with enums or unions #}
{% if transport == "grpc-async" and calling_form not in [calling_form_enum.LongRunningRequestPromise,
calling_form_enum.RequestPagedAll] %}
await{{ " "}}
{%- endif -%}
{% if calling_form in [calling_form_enum.RequestStreamingBidi,
calling_form_enum.RequestStreamingClient] %}
client.{{ sample.rpc|snake_case }}([{{ render_request_params(sample.request.request_list)|trim }}])
Expand All @@ -215,7 +219,7 @@ client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request

{# Setting up the method invocation is the responsibility of the caller: #}
{# it's just easier to set up client side streaming and other things from outside this macro. #}
{% macro render_calling_form(method_invocation_text, calling_form, calling_form_enum, response_statements ) %}
{% macro render_calling_form(method_invocation_text, calling_form, calling_form_enum, transport, response_statements ) %}
# Make the request
{% if calling_form == calling_form_enum.Request %}
response = {{ method_invocation_text|trim }}
Expand All @@ -226,21 +230,21 @@ response = {{ method_invocation_text|trim }}
{% endfor %}
{% elif calling_form == calling_form_enum.RequestPagedAll %}
page_result = {{ method_invocation_text|trim }}
for response in page_result:
{% if transport == "grpc-async" %}async {% endif %}for response in page_result:
{% for statement in response_statements %}
{{ dispatch_statement(statement)|trim }}
{% endfor %}
{% elif calling_form == calling_form_enum.RequestPaged %}
page_result = {{ method_invocation_text|trim }}
for page in page_result.pages():
{% if transport == "grpc-async" %}async {% endif %}for page in page_result.pages():
for response in page:
{% for statement in response_statements %}
{{ dispatch_statement(statement)|trim }}
{% endfor %}
{% elif calling_form in [calling_form_enum.RequestStreamingServer,
calling_form_enum.RequestStreamingBidi] %}
stream = {{ method_invocation_text|trim }}
for response in stream:
{% if transport == "grpc-async" %}async {% endif %}for response in stream:
{% for statement in response_statements %}
{{ dispatch_statement(statement)|trim }}
{% endfor %}
Expand All @@ -249,7 +253,7 @@ operation = {{ method_invocation_text|trim }}

print("Waiting for operation to complete...")

response = operation.result()
response = {% if transport == "grpc-async" %}await {% endif %}operation.result()
{% for statement in response_statements %}
{{ dispatch_statement(statement)|trim }}
{% endfor %}
Expand Down
6 changes: 3 additions & 3 deletions gapic/templates/examples/sample.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ from {{ sample.module_namespace|join(".") }} import {{ sample.module_name }}


{# also need calling form #}
def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_params(sample.request)|trim }}):
{% if sample.transport == "grpc-async" %}async {% endif %}def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_params(sample.request)|trim }}):
"""{{ sample.description }}"""

{{ frags.render_client_setup(sample.module_name, sample.client_name)|indent }}
{{ frags.render_request_setup(sample.request, sample.module_name, sample.request_type)|indent }}
{% with method_call = frags.render_method_call(sample, calling_form, calling_form_enum) %}
{{ frags.render_calling_form(method_call, calling_form, calling_form_enum, sample.response)|indent -}}
{% with method_call = frags.render_method_call(sample, calling_form, calling_form_enum, sample.transport) %}
{{ frags.render_calling_form(method_call, calling_form, calling_form_enum, sample.transport, sample.response)|indent -}}
{% endwith %}

# [END {{ sample.id }}]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Generated code. DO NOT EDIT!
#
# Snippet for ListResources
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_ListResources_async]
from animalia import mollusca_v1


async def sample_list_resources():
"""Snippet for list_resources"""

# Create a client
client = mollusca_v1.SnippetsAsyncClient()

# Initialize request argument(s)
request = mollusca_v1.ListResourcesRequest(
)

# Make the request
page_result = client.list_resources(request=request)
async for response in page_result:
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_ListResources_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_ListResources_grpc]
# [START mollusca_generated_mollusca_v1_Snippets_ListResources_sync]
from animalia import mollusca_v1


Expand All @@ -42,4 +42,4 @@ def sample_list_resources():
for response in page_result:
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_ListResources_grpc]
# [END mollusca_generated_mollusca_v1_Snippets_ListResources_sync]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Generated code. DO NOT EDIT!
#
# Snippet for MethodBidiStreaming
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_async]
from animalia import mollusca_v1


async def sample_method_bidi_streaming():
"""Snippet for method_bidi_streaming"""

# Create a client
client = mollusca_v1.SnippetsAsyncClient()

# Initialize request argument(s)
request = mollusca_v1.SignatureRequest(
)

# Make the request
stream = await client.method_bidi_streaming([])
async for response in stream:
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_grpc]
# [START mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_sync]
from animalia import mollusca_v1


Expand All @@ -42,4 +42,4 @@ def sample_method_bidi_streaming():
for response in stream:
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_grpc]
# [END mollusca_generated_mollusca_v1_Snippets_MethodBidiStreaming_sync]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Generated code. DO NOT EDIT!
#
# Snippet for MethodLroSignatures
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_async]
from animalia import mollusca_v1


async def sample_method_lro_signatures():
"""Snippet for method_lro_signatures"""

# Create a client
client = mollusca_v1.SnippetsAsyncClient()

# Initialize request argument(s)
request = mollusca_v1.SignatureRequest(
)

# Make the request
operation = client.method_lro_signatures(request=request)

print("Waiting for operation to complete...")

response = await operation.result()
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_grpc]
# [START mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_sync]
from animalia import mollusca_v1


Expand All @@ -45,4 +45,4 @@ def sample_method_lro_signatures():
response = operation.result()
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_grpc]
# [END mollusca_generated_mollusca_v1_Snippets_MethodLroSignatures_sync]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Generated code. DO NOT EDIT!
#
# Snippet for MethodOneSignature
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_async]
from animalia import mollusca_v1


async def sample_method_one_signature():
"""Snippet for method_one_signature"""

# Create a client
client = mollusca_v1.SnippetsAsyncClient()

# Initialize request argument(s)
request = mollusca_v1.SignatureRequest(
)

# Make the request
response = await client.method_one_signature(request=request)

# Handle response
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_grpc]
# [START mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_sync]
from animalia import mollusca_v1


Expand All @@ -43,4 +43,4 @@ def sample_method_one_signature():
# Handle response
print("{}".format(response))

# [END mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_grpc]
# [END mollusca_generated_mollusca_v1_Snippets_MethodOneSignature_sync]
Loading

0 comments on commit 22ecced

Please sign in to comment.