Skip to content

Commit e2c523c

Browse files
tetiana-karasovagcf-owl-bot[bot]parthea
authored andcommitted
docs(samples): add samples to create, read, update, and delete products (#150)
* feat: Retail Tutorials Product CRUD * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix requirements.txt * fix lint errors * lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix tests * lint * lint * revert change * remove client options * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove create/delete bucket which does not work with concurrent tests * lint * allow import_products_gcs_test to run concurrently * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove pytest.mark.flaky * lint * fix set inventory test * update copyright year Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent ebe545a commit e2c523c

Some content is hidden

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

43 files changed

+1091
-116
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START retail_add_fulfillment_places]
16+
import datetime
17+
import os
18+
import random
19+
import string
20+
import time
21+
22+
from google.cloud.retail import AddFulfillmentPlacesRequest, ProductServiceClient
23+
24+
from setup_product.setup_cleanup import create_product, delete_product, get_product
25+
26+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
product_id = "".join(random.sample(string.ascii_lowercase, 8))
28+
product_name = (
29+
"projects/"
30+
+ project_id
31+
+ "/locations/global/catalogs/default_catalog/branches/default_branch/products/"
32+
+ product_id
33+
)
34+
35+
# The request timestamp
36+
current_date = datetime.datetime.now()
37+
outdated_date = datetime.datetime.now() - datetime.timedelta(days=1)
38+
39+
40+
# add fulfillment request
41+
def get_add_fulfillment_request(
42+
product_name: str, timestamp, place_id
43+
) -> AddFulfillmentPlacesRequest:
44+
add_fulfillment_request = AddFulfillmentPlacesRequest()
45+
add_fulfillment_request.product = product_name
46+
add_fulfillment_request.type_ = "pickup-in-store"
47+
add_fulfillment_request.place_ids = [place_id]
48+
add_fulfillment_request.add_time = timestamp
49+
add_fulfillment_request.allow_missing = True
50+
51+
print("---add fulfillment request---")
52+
print(add_fulfillment_request)
53+
54+
return add_fulfillment_request
55+
56+
57+
# add fulfillment places to product
58+
def add_fulfillment_places(product_name: str, timestamp, place_id):
59+
add_fulfillment_request = get_add_fulfillment_request(
60+
product_name, timestamp, place_id
61+
)
62+
ProductServiceClient().add_fulfillment_places(add_fulfillment_request)
63+
64+
# This is a long running operation and its result is not immediately present with get operations,
65+
# thus we simulate wait with sleep method.
66+
print("---add fulfillment places, wait 40 seconds :---")
67+
time.sleep(40)
68+
69+
70+
# [END retail_add_fulfillment_places]
71+
72+
73+
create_product(product_id)
74+
print("------add fulfilment places with current date: {}-----".format(current_date))
75+
add_fulfillment_places(product_name, current_date, "store2")
76+
get_product(product_name)
77+
print("------add outdated fulfilment places: {}-----".format(outdated_date))
78+
add_fulfillment_places(product_name, outdated_date, "store3")
79+
get_product(product_name)
80+
delete_product(product_name)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import re
16+
import subprocess
17+
18+
19+
def test_add_fulfillment():
20+
output = str(
21+
subprocess.check_output("python add_fulfillment_places.py", shell=True)
22+
)
23+
24+
assert re.match(".*product is created.*", output)
25+
assert re.match(".*add fulfillment request.*", output)
26+
assert re.match(".*add fulfillment places.*", output)
27+
assert re.match(
28+
'.*get product response.*?fulfillment_info.*type_: "pickup-in-store".*?place_ids: "store1".*',
29+
output,
30+
)
31+
assert re.match(
32+
'.*get product response.*?fulfillment_info.*type_: "pickup-in-store".*?place_ids: "store2".*',
33+
output,
34+
)

generated_samples/interactive-tutorials/product/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@
2323
@pytest.fixture(scope="session")
2424
def table_id_prefix() -> str:
2525
return prefixer.create_prefix()
26+
27+
28+
@pytest.fixture(scope="session")
29+
def bucket_name_prefix() -> str:
30+
return prefixer.create_prefix()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START retail_create_product]
17+
# Create product in a catalog using Retail API
18+
#
19+
import os
20+
import random
21+
import string
22+
23+
from google.cloud.retail import CreateProductRequest, Product, ProductServiceClient
24+
from google.cloud.retail import PriceInfo
25+
from google.cloud.retail_v2.types import product
26+
27+
from setup_product.setup_cleanup import delete_product
28+
29+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
30+
default_branch_name = (
31+
"projects/"
32+
+ project_id
33+
+ "/locations/global/catalogs/default_catalog/branches/default_branch"
34+
)
35+
generated_product_id = "".join(random.sample(string.ascii_lowercase, 8))
36+
37+
38+
# generate product to create
39+
def generate_product() -> Product:
40+
price_info = PriceInfo()
41+
price_info.price = 30.0
42+
price_info.original_price = 35.5
43+
price_info.currency_code = "USD"
44+
return product.Product(
45+
title="Nest Mini",
46+
type_=product.Product.Type.PRIMARY,
47+
categories=["Speakers and displays"],
48+
brands=["Google"],
49+
price_info=price_info,
50+
availability="IN_STOCK",
51+
)
52+
53+
54+
# get create product request
55+
def get_create_product_request(product_to_create: Product, product_id: str) -> object:
56+
create_product_request = CreateProductRequest()
57+
create_product_request.product = product_to_create
58+
create_product_request.product_id = product_id
59+
create_product_request.parent = default_branch_name
60+
61+
print("---create product request---")
62+
print(create_product_request)
63+
64+
return create_product_request
65+
66+
67+
# call the Retail API to create product
68+
def create_product(product_id: str):
69+
create_product_request = get_create_product_request(generate_product(), product_id)
70+
product_created = ProductServiceClient().create_product(create_product_request)
71+
72+
print("---created product:---")
73+
print(product_created)
74+
return product_created
75+
76+
77+
# create a product
78+
created_product = create_product(generated_product_id)
79+
# delete created product
80+
delete_product(created_product.name)
81+
# [END retail_create_product]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import re
16+
import subprocess
17+
18+
19+
def test_create_product():
20+
output = str(subprocess.check_output("python create_product.py", shell=True))
21+
22+
assert re.match(".*create product request.*", output)
23+
assert re.match(".*created product.*", output)
24+
assert re.match(
25+
'.*name: "projects/.+/locations/global/catalogs/default_catalog/branches/0/products/.*',
26+
output,
27+
)
28+
assert re.match('.*title: "Nest Mini".*', output)
29+
assert re.match(".*product.*was deleted.*", output)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START retail_crud_product]
17+
# Create product in a catalog using Retail API
18+
#
19+
import os
20+
import random
21+
import string
22+
23+
from google.cloud.retail import (
24+
CreateProductRequest,
25+
DeleteProductRequest,
26+
GetProductRequest,
27+
Product,
28+
ProductServiceClient,
29+
UpdateProductRequest,
30+
)
31+
from google.cloud.retail import PriceInfo
32+
from google.cloud.retail_v2.types import product
33+
34+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
35+
default_branch_name = (
36+
"projects/"
37+
+ project_id
38+
+ "/locations/global/catalogs/default_catalog/branches/default_branch"
39+
)
40+
product_id = "".join(random.sample(string.ascii_lowercase, 8))
41+
product_name = "{}/products/{}".format(default_branch_name, product_id)
42+
43+
44+
# generate product for create
45+
def generate_product() -> Product:
46+
price_info = PriceInfo()
47+
price_info.price = 30.0
48+
price_info.original_price = 35.5
49+
price_info.currency_code = "USD"
50+
return product.Product(
51+
title="Nest Mini",
52+
type_=product.Product.Type.PRIMARY,
53+
categories=["Speakers and displays"],
54+
brands=["Google"],
55+
price_info=price_info,
56+
availability="IN_STOCK",
57+
)
58+
59+
60+
# generate product for update
61+
def generate_product_for_update() -> Product:
62+
price_info = PriceInfo()
63+
price_info.price = 20.0
64+
price_info.original_price = 25.5
65+
price_info.currency_code = "EUR"
66+
return product.Product(
67+
id=product_id,
68+
name=product_name,
69+
title="Updated Nest Mini",
70+
type_=product.Product.Type.PRIMARY,
71+
categories=["Updated Speakers and displays"],
72+
brands=["Updated Google"],
73+
availability="OUT_OF_STOCK",
74+
price_info=price_info,
75+
)
76+
77+
78+
# create product
79+
def create_product() -> object:
80+
create_product_request = CreateProductRequest()
81+
create_product_request.product = generate_product()
82+
create_product_request.product_id = product_id
83+
create_product_request.parent = default_branch_name
84+
85+
print("---create product request---")
86+
print(create_product_request)
87+
88+
product_created = ProductServiceClient().create_product(create_product_request)
89+
print("---created product:---")
90+
print(product_created)
91+
return product_created
92+
93+
94+
# get product
95+
def get_product() -> object:
96+
get_product_request = GetProductRequest()
97+
get_product_request.name = product_name
98+
99+
print("---get product request---")
100+
print(get_product_request)
101+
102+
get_product_response = ProductServiceClient().get_product(get_product_request)
103+
104+
print("---get product response:---")
105+
print(get_product_response)
106+
return get_product_response
107+
108+
109+
# update product
110+
def update_product():
111+
update_product_request = UpdateProductRequest()
112+
update_product_request.product = generate_product_for_update()
113+
update_product_request.allow_missing = True
114+
115+
print("---update product request---")
116+
print(update_product_request)
117+
118+
updated_product = ProductServiceClient().update_product(update_product_request)
119+
print("---updated product---:")
120+
print(updated_product)
121+
return updated_product
122+
123+
124+
# delete product
125+
def delete_product():
126+
delete_product_request = DeleteProductRequest()
127+
delete_product_request.name = product_name
128+
129+
print("---delete product request---")
130+
print(delete_product_request)
131+
132+
ProductServiceClient().delete_product(delete_product_request)
133+
134+
print("deleting product " + product_name)
135+
print("---product was deleted:---")
136+
137+
138+
# call the methods
139+
create_product()
140+
get_product()
141+
update_product()
142+
delete_product()
143+
# [END retail_crud_product]

0 commit comments

Comments
 (0)