Skip to content

Commit adabe0b

Browse files
authored
feat: add async samples (#861)
1 parent e9c9e2a commit adabe0b

File tree

134 files changed

+3365
-161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+3365
-161
lines changed

packages/gapic-generator/gapic/samplegen/samplegen.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,15 @@ def preprocess_sample(sample, api_schema: api.API, rpc: wrappers.Method):
293293
sample["module_name"] = api_schema.naming.versioned_module_name
294294
sample["module_namespace"] = api_schema.naming.module_namespace
295295

296-
sample["client_name"] = api_schema.services[sample["service"]].client_name
296+
# Assume the gRPC transport if the transport is not specified
297+
sample.setdefault("transport", api.TRANSPORT_GRPC)
298+
299+
if sample["transport"] == api.TRANSPORT_GRPC_ASYNC:
300+
sample["client_name"] = api_schema.services[sample["service"]
301+
].async_client_name
302+
else:
303+
sample["client_name"] = api_schema.services[sample["service"]].client_name
304+
297305
# the type of the request object passed to the rpc e.g, `ListRequest`
298306
sample["request_type"] = rpc.input.ident.name
299307

@@ -946,17 +954,16 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A
946954

947955
for service_name, service in gapic_metadata.services.items():
948956
api_short_name = api_schema.services[f"{api_schema.naming.proto_package}.{service_name}"].shortname
949-
for transport_type, client in service.clients.items():
950-
if transport_type == "grpc-async":
951-
# TODO(busunkim): Enable generation of async samples
952-
continue
957+
for transport, client in service.clients.items():
958+
transport_type = "async" if transport == api.TRANSPORT_GRPC_ASYNC else "sync"
953959
for rpc_name, method_list in client.rpcs.items():
954960
# Region Tag Format:
955961
# [{START|END} ${apishortname}_generated_${api}_${apiVersion}_${serviceName}_${rpcName}_{sync|async}_${overloadDisambiguation}]
956962
region_tag = f"{api_short_name}_generated_{api_schema.naming.versioned_module_name}_{service_name}_{rpc_name}_{transport_type}"
957963
spec = {
958964
"sample_type": "standalone",
959965
"rpc": rpc_name,
966+
"transport": transport,
960967
"request": [],
961968
# response is populated in `preprocess_sample`
962969
"service": f"{api_schema.naming.proto_package}.{service_name}",

packages/gapic-generator/gapic/schema/api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
from gapic.utils import RESERVED_NAMES
4646

4747

48+
TRANSPORT_GRPC = "grpc"
49+
TRANSPORT_GRPC_ASYNC = "grpc-async"
50+
TRANSPORT_REST = "rest"
51+
52+
4853
@dataclasses.dataclass(frozen=True)
4954
class Proto:
5055
"""A representation of a particular proto file within an API."""
@@ -414,11 +419,12 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata:
414419
# This assumes the options are generated by the class method factory.
415420
transports = []
416421
if "grpc" in options.transport:
417-
transports.append(("grpc", service.client_name))
418-
transports.append(("grpc-async", service.async_client_name))
422+
transports.append((TRANSPORT_GRPC, service.client_name))
423+
transports.append(
424+
(TRANSPORT_GRPC_ASYNC, service.async_client_name))
419425

420426
if "rest" in options.transport:
421-
transports.append(("rest", service.client_name))
427+
transports.append((TRANSPORT_REST, service.client_name))
422428

423429
methods = sorted(service.methods.values(), key=lambda m: m.name)
424430
for tprt, client_name in transports:

packages/gapic-generator/gapic/templates/examples/feature_fragments.j2

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,13 @@ request=request
202202
{% endmacro %}
203203

204204

205-
{% macro render_method_call(sample, calling_form, calling_form_enum) %}
205+
{% macro render_method_call(sample, calling_form, calling_form_enum, transport) %}
206206
{# Note: this doesn't deal with enums or unions #}
207+
{# LROs return operation objects and paged requests return pager objects #}
208+
{% if transport == "grpc-async" and calling_form not in
209+
[calling_form_enum.LongRunningRequestPromise, calling_form_enum.RequestPagedAll] %}
210+
await{{ " "}}
211+
{%- endif -%}
207212
{% if calling_form in [calling_form_enum.RequestStreamingBidi,
208213
calling_form_enum.RequestStreamingClient] %}
209214
client.{{ sample.rpc|snake_case }}([{{ render_request_params(sample.request.request_list)|trim }}])
@@ -215,7 +220,7 @@ client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request
215220

216221
{# Setting up the method invocation is the responsibility of the caller: #}
217222
{# it's just easier to set up client side streaming and other things from outside this macro. #}
218-
{% macro render_calling_form(method_invocation_text, calling_form, calling_form_enum, response_statements ) %}
223+
{% macro render_calling_form(method_invocation_text, calling_form, calling_form_enum, transport, response_statements ) %}
219224
# Make the request
220225
{% if calling_form == calling_form_enum.Request %}
221226
response = {{ method_invocation_text|trim }}
@@ -228,21 +233,21 @@ response = {{ method_invocation_text|trim }}
228233
{% endif %}
229234
{% elif calling_form == calling_form_enum.RequestPagedAll %}
230235
page_result = {{ method_invocation_text|trim }}
231-
for response in page_result:
236+
{% if transport == "grpc-async" %}async {% endif %}for response in page_result:
232237
{% for statement in response_statements %}
233238
{{ dispatch_statement(statement)|trim }}
234239
{% endfor %}
235240
{% elif calling_form == calling_form_enum.RequestPaged %}
236241
page_result = {{ method_invocation_text|trim }}
237-
for page in page_result.pages():
242+
{% if transport == "grpc-async" %}async {% endif %}for page in page_result.pages():
238243
for response in page:
239244
{% for statement in response_statements %}
240245
{{ dispatch_statement(statement)|trim }}
241246
{% endfor %}
242247
{% elif calling_form in [calling_form_enum.RequestStreamingServer,
243248
calling_form_enum.RequestStreamingBidi] %}
244249
stream = {{ method_invocation_text|trim }}
245-
for response in stream:
250+
{% if transport == "grpc-async" %}async {% endif %}for response in stream:
246251
{% for statement in response_statements %}
247252
{{ dispatch_statement(statement)|trim }}
248253
{% endfor %}
@@ -251,7 +256,7 @@ operation = {{ method_invocation_text|trim }}
251256

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

254-
response = operation.result()
259+
response = {% if transport == "grpc-async" %}await {% endif %}operation.result()
255260
{% for statement in response_statements %}
256261
{{ dispatch_statement(statement)|trim }}
257262
{% endfor %}

packages/gapic-generator/gapic/templates/examples/sample.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ from {{ sample.module_namespace|join(".") }} import {{ sample.module_name }}
3131

3232

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

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

4343
# [END {{ sample.id }}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for AnalyzeIamPolicy
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install google-cloud-asset
24+
25+
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]
27+
from google.cloud import asset_v1
28+
29+
30+
async def sample_analyze_iam_policy():
31+
"""Snippet for analyze_iam_policy"""
32+
33+
# Create a client
34+
client = asset_v1.AssetServiceAsyncClient()
35+
36+
# Initialize request argument(s)
37+
request = asset_v1.AnalyzeIamPolicyRequest(
38+
)
39+
40+
# Make the request
41+
response = await client.analyze_iam_policy(request=request)
42+
43+
# Handle response
44+
print("{}".format(response))
45+
46+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for AnalyzeIamPolicyLongrunning
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install google-cloud-asset
24+
25+
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]
27+
from google.cloud import asset_v1
28+
29+
30+
async def sample_analyze_iam_policy_longrunning():
31+
"""Snippet for analyze_iam_policy_longrunning"""
32+
33+
# Create a client
34+
client = asset_v1.AssetServiceAsyncClient()
35+
36+
# Initialize request argument(s)
37+
request = asset_v1.AnalyzeIamPolicyLongrunningRequest(
38+
)
39+
40+
# Make the request
41+
operation = client.analyze_iam_policy_longrunning(request=request)
42+
43+
print("Waiting for operation to complete...")
44+
45+
response = await operation.result()
46+
print("{}".format(response))
47+
48+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_longrunning_grpc.py renamed to packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_longrunning_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# python3 -m pip install google-cloud-asset
2424

2525

26-
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]
2727
from google.cloud import asset_v1
2828

2929

@@ -45,4 +45,4 @@ def sample_analyze_iam_policy_longrunning():
4545
response = operation.result()
4646
print("{}".format(response))
4747

48-
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
48+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_grpc.py renamed to packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# python3 -m pip install google-cloud-asset
2424

2525

26-
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
26+
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_sync]
2727
from google.cloud import asset_v1
2828

2929

@@ -43,4 +43,4 @@ def sample_analyze_iam_policy():
4343
# Handle response
4444
print("{}".format(response))
4545

46-
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
46+
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_sync]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for BatchGetAssetsHistory
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install google-cloud-asset
24+
25+
26+
# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]
27+
from google.cloud import asset_v1
28+
29+
30+
async def sample_batch_get_assets_history():
31+
"""Snippet for batch_get_assets_history"""
32+
33+
# Create a client
34+
client = asset_v1.AssetServiceAsyncClient()
35+
36+
# Initialize request argument(s)
37+
request = asset_v1.BatchGetAssetsHistoryRequest(
38+
)
39+
40+
# Make the request
41+
response = await client.batch_get_assets_history(request=request)
42+
43+
# Handle response
44+
print("{}".format(response))
45+
46+
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]

packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_batch_get_assets_history_grpc.py renamed to packages/gapic-generator/tests/integration/goldens/asset/samples/generated_samples/cloudasset_generated_asset_v1_asset_service_batch_get_assets_history_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# python3 -m pip install google-cloud-asset
2424

2525

26-
# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
26+
# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_sync]
2727
from google.cloud import asset_v1
2828

2929

@@ -43,4 +43,4 @@ def sample_batch_get_assets_history():
4343
# Handle response
4444
print("{}".format(response))
4545

46-
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
46+
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_sync]

0 commit comments

Comments
 (0)