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

fix: snippetgen should call await on the operation coroutine before calling result #1495

Merged
merged 2 commits into from
Nov 16, 2022
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
12 changes: 11 additions & 1 deletion gapic/templates/examples/feature_fragments.j2
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request
{% endif %}
{% endmacro %}

{% macro operation_text(transport) %}
{% if transport == "grpc-async" %}
(await operation)
{% else %}
operation
{% endif %}
{% endmacro %}

{# 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, transport, response_statements ) %}
Expand Down Expand Up @@ -286,7 +294,9 @@ operation = {{ method_invocation_text|trim }}

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

response = {% if transport == "grpc-async" %}await {% endif %}operation.result()
{% with operation_text = operation_text(transport) %}
response = {{ operation_text|trim }}.result()
{% endwith %}

# Handle the response
{% for statement in response_statements %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async def sample_export_assets():

Copy link
Collaborator Author

@dizcology dizcology Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to change the line above to the following (add await before the method call):

operation = await client.export_assets(request=request)

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -1610,7 +1610,7 @@ async def sample_analyze_iam_policy_longrunning():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def sample_analyze_iam_policy_longrunning():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def sample_export_assets():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ async def sample_create_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -593,7 +593,7 @@ async def sample_update_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -730,7 +730,7 @@ async def sample_delete_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def sample_create_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def sample_delete_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def sample_update_trigger():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ async def sample_create_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -643,7 +643,7 @@ async def sample_update_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -778,7 +778,7 @@ async def sample_upgrade_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -917,7 +917,7 @@ async def sample_import_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -1052,7 +1052,7 @@ async def sample_export_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -1181,7 +1181,7 @@ async def sample_failover_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down Expand Up @@ -1309,7 +1309,7 @@ async def sample_delete_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def sample_create_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def sample_delete_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def sample_export_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def sample_failover_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def sample_import_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def sample_update_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def sample_upgrade_instance():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def sample_method_lro_signatures():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print(response)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/samplegen/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def test_render_calling_form_longrunning_async():

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

response = await operation.result()
response = (await operation).result()

# Handle the response
print("Test print statement")
Expand Down