Skip to content

Commit 14c9127

Browse files
kchertenkoparthea
andauthored
chore(samples): remove delays from LRO operations samples. (#335)
* fix: remove delays from LRO operations samples. * fix linters. * fix logic. * fix linters. Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 139d575 commit 14c9127

File tree

4 files changed

+57
-46
lines changed

4 files changed

+57
-46
lines changed

generated_samples/interactive-tutorials/product/add_fulfillment_places.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
# [START retail_add_fulfillment_places]
1616
# Adding place IDs using Retail API.
1717
#
18+
import asyncio
1819
import random
1920
import string
20-
import time
2121

22+
from google.api_core.exceptions import GoogleAPICallError
2223
import google.auth
23-
from google.cloud.retail import AddFulfillmentPlacesRequest, ProductServiceClient
24+
from google.cloud.retail import AddFulfillmentPlacesRequest, ProductServiceAsyncClient
2425

2526
from setup_product.setup_cleanup import create_product, delete_product, get_product
2627

@@ -50,23 +51,23 @@ def get_add_fulfillment_request(
5051
return add_fulfillment_request
5152

5253

53-
# add fulfillment places to product
54-
def add_fulfillment_places(product_name: str, place_id):
55-
add_fulfillment_request = get_add_fulfillment_request(product_name, place_id)
56-
ProductServiceClient().add_fulfillment_places(add_fulfillment_request)
54+
async def add_places(product_name: str):
55+
print("------add fulfillment places-----")
56+
add_fulfillment_request = get_add_fulfillment_request(product_name, "store2")
57+
operation = await ProductServiceAsyncClient().add_fulfillment_places(
58+
add_fulfillment_request
59+
)
60+
# This operation doesn't have result or errors. So GoogleAPICallError will be raised.
61+
try:
62+
await operation.result()
63+
except GoogleAPICallError:
64+
pass
5765

58-
# This is a long running operation and its result is not immediately present with get operations,
59-
# thus we simulate wait with sleep method.
60-
print("---add fulfillment places, wait 90 seconds :---")
61-
time.sleep(90)
62-
63-
64-
# [END retail_add_fulfillment_places]
66+
get_product(product_name)
67+
delete_product(product_name)
6568

6669

6770
create_product(product_id)
6871

69-
print("------add fulfilment places-----")
70-
add_fulfillment_places(product_name, "store2")
71-
get_product(product_name)
72-
delete_product(product_name)
72+
asyncio.run(add_places(product_name))
73+
# [END retail_add_fulfillment_places]

generated_samples/interactive-tutorials/product/remove_fulfillment_places.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
# [START retail_remove_fulfillment_places]
1616
# Remove place IDs using Retail API.
1717
#
18+
import asyncio
1819
import random
1920
import string
20-
import time
2121

22-
from google.cloud.retail import ProductServiceClient, RemoveFulfillmentPlacesRequest
22+
from google.api_core.exceptions import GoogleAPICallError
23+
from google.cloud.retail import (
24+
ProductServiceAsyncClient,
25+
RemoveFulfillmentPlacesRequest,
26+
)
2327

2428
from setup_product.setup_cleanup import create_product, delete_product, get_product
2529

@@ -42,23 +46,23 @@ def get_remove_fulfillment_request(
4246
return remove_fulfillment_request
4347

4448

45-
# remove fulfillment places to product
46-
def remove_fulfillment_places(product_name: str, store_id):
47-
remove_fulfillment_request = get_remove_fulfillment_request(product_name, store_id)
48-
ProductServiceClient().remove_fulfillment_places(remove_fulfillment_request)
49+
async def remove_places(product_name: str):
50+
print("------remove fulfillment places-----")
51+
remove_fulfillment_request = get_remove_fulfillment_request(product_name, "store0")
52+
operation = await ProductServiceAsyncClient().remove_fulfillment_places(
53+
remove_fulfillment_request
54+
)
55+
# This operation doesn't have result or errors. So GoogleAPICallError will be raised.
56+
try:
57+
await operation.result()
58+
except GoogleAPICallError:
59+
pass
4960

50-
# This is a long running operation and its result is not immediately present with get operations,
51-
# thus we simulate wait with sleep method.
52-
print("---remove fulfillment places, wait 90 seconds:---")
53-
time.sleep(90)
54-
55-
56-
# [END retail_remove_fulfillment_places]
61+
get_product(product_name)
62+
delete_product(product_name)
5763

5864

5965
product = create_product(product_id)
6066

61-
print("------remove fulfilment places-----")
62-
remove_fulfillment_places(product.name, "store0")
63-
get_product(product.name)
64-
delete_product(product.name)
67+
asyncio.run(remove_places(product.name))
68+
# [END retail_remove_fulfillment_places]

generated_samples/interactive-tutorials/product/set_inventory.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
# [START retail_set_inventory]
1616
# Updating inventory information using Retail API.
1717
#
18+
import asyncio
1819
import random
1920
import string
20-
import time
2121

22+
from google.api_core.exceptions import GoogleAPICallError
2223
import google.auth
2324
from google.cloud.retail import (
2425
FulfillmentInfo,
2526
PriceInfo,
2627
Product,
27-
ProductServiceClient,
28+
ProductServiceAsyncClient,
2829
SetInventoryRequest,
2930
)
3031
from google.protobuf.field_mask_pb2 import FieldMask
@@ -82,16 +83,21 @@ def get_set_inventory_request(product_name: str) -> SetInventoryRequest:
8283
# set inventory to product
8384
def set_inventory(product_name: str):
8485
set_inventory_request = get_set_inventory_request(product_name)
85-
ProductServiceClient().set_inventory(set_inventory_request)
86+
return ProductServiceAsyncClient().set_inventory(set_inventory_request)
8687

87-
# This is a long running operation and its result is not immediately present with get operations,
88-
# thus we simulate wait with sleep method.
89-
print("---set inventory, wait 90 seconds:---")
90-
time.sleep(90)
9188

89+
async def set_inventory_and_remove_product(product_name: str):
90+
operation = await set_inventory(product_name)
91+
# This operation doesn't have result or errors. So GoogleAPICallError will be raised.
92+
try:
93+
await operation.result()
94+
except GoogleAPICallError:
95+
pass
9296

93-
create_product(product_id)
94-
set_inventory(product_name)
95-
get_product(product_name)
96-
delete_product(product_name)
97+
get_product(product_name)
98+
delete_product(product_name)
99+
100+
101+
product = create_product(product_id)
102+
asyncio.run(set_inventory_and_remove_product(product.name))
97103
# [END retail_set_inventory]

generated_samples/interactive-tutorials/product/set_inventory_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ def test_set_inventory():
3434
output,
3535
)
3636
assert re.match(
37-
".*product projects/.*/locations/global/catalogs/default_catalog/branches/default_branch/products.* was deleted.*",
37+
".*product projects/.*/locations/global/catalogs/default_catalog/branches/0/products.* was deleted.*",
3838
output,
3939
)

0 commit comments

Comments
 (0)