Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add async samples #861

Merged
merged 8 commits into from
Jun 11, 2021
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
17 changes: 12 additions & 5 deletions gapic/samplegen/samplegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,15 @@ 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
# Assume the gRPC transport if the transport is not specified
sample.setdefault("transport", api.TRANSPORT_GRPC)

if sample["transport"] == api.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 @@ -946,17 +954,16 @@ 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, client in service.clients.items():
transport_type = "async" if transport == api.TRANSPORT_GRPC_ASYNC else "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,
"request": [],
# response is populated in `preprocess_sample`
"service": f"{api_schema.naming.proto_package}.{service_name}",
Expand Down
12 changes: 9 additions & 3 deletions gapic/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
from gapic.utils import RESERVED_NAMES


TRANSPORT_GRPC = "grpc"
TRANSPORT_GRPC_ASYNC = "grpc-async"
TRANSPORT_REST = "rest"


@dataclasses.dataclass(frozen=True)
class Proto:
"""A representation of a particular proto file within an API."""
Expand Down Expand Up @@ -414,11 +419,12 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata:
# This assumes the options are generated by the class method factory.
transports = []
if "grpc" in options.transport:
transports.append(("grpc", service.client_name))
transports.append(("grpc-async", service.async_client_name))
transports.append((TRANSPORT_GRPC, service.client_name))
transports.append(
(TRANSPORT_GRPC_ASYNC, service.async_client_name))

if "rest" in options.transport:
transports.append(("rest", service.client_name))
transports.append((TRANSPORT_REST, service.client_name))

methods = sorted(service.methods.values(), key=lambda m: m.name)
for tprt, client_name in transports:
Expand Down
17 changes: 11 additions & 6 deletions gapic/templates/examples/feature_fragments.j2
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,13 @@ 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 #}
{# LROs return operation objects and paged requests return pager objects #}
{% 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 +220,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 @@ -228,21 +233,21 @@ response = {{ method_invocation_text|trim }}
{% endif %}
{% 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 @@ -251,7 +256,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,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 AnalyzeIamPolicy
# 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 google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]
from google.cloud import asset_v1


async def sample_analyze_iam_policy():
"""Snippet for analyze_iam_policy"""

# Create a client
client = asset_v1.AssetServiceAsyncClient()

# Initialize request argument(s)
request = asset_v1.AnalyzeIamPolicyRequest(
)

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

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

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_async]
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 AnalyzeIamPolicyLongrunning
# 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 google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]
from google.cloud import asset_v1


async def sample_analyze_iam_policy_longrunning():
"""Snippet for analyze_iam_policy_longrunning"""

# Create a client
client = asset_v1.AssetServiceAsyncClient()

# Initialize request argument(s)
request = asset_v1.AnalyzeIamPolicyLongrunningRequest(
)

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

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

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

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]
from google.cloud import asset_v1


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

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_sync]
from google.cloud import asset_v1


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

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_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 BatchGetAssetsHistory
# 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 google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]
from google.cloud import asset_v1


async def sample_batch_get_assets_history():
"""Snippet for batch_get_assets_history"""

# Create a client
client = asset_v1.AssetServiceAsyncClient()

# Initialize request argument(s)
request = asset_v1.BatchGetAssetsHistoryRequest(
)

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

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

# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_sync]
from google.cloud import asset_v1


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

# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_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 CreateFeed
# 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 google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_CreateFeed_async]
from google.cloud import asset_v1


async def sample_create_feed():
"""Snippet for create_feed"""

# Create a client
client = asset_v1.AssetServiceAsyncClient()

# Initialize request argument(s)
request = asset_v1.CreateFeedRequest(
)

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

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

# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_async]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_CreateFeed_grpc]
# [START cloudasset_generated_asset_v1_AssetService_CreateFeed_sync]
from google.cloud import asset_v1


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

# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_grpc]
# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_sync]
Loading