-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add samples to create, read, update, and delete produc…
…ts (#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 <partheniou@google.com>
- Loading branch information
1 parent
2cf7f35
commit 7cd2340
Showing
43 changed files
with
1,091 additions
and
116 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
generated_samples/interactive-tutorials/product/add_fulfillment_places.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2022 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
# [START retail_add_fulfillment_places] | ||
import datetime | ||
import os | ||
import random | ||
import string | ||
import time | ||
|
||
from google.cloud.retail import AddFulfillmentPlacesRequest, ProductServiceClient | ||
|
||
from setup_product.setup_cleanup import create_product, delete_product, get_product | ||
|
||
project_id = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
product_id = "".join(random.sample(string.ascii_lowercase, 8)) | ||
product_name = ( | ||
"projects/" | ||
+ project_id | ||
+ "/locations/global/catalogs/default_catalog/branches/default_branch/products/" | ||
+ product_id | ||
) | ||
|
||
# The request timestamp | ||
current_date = datetime.datetime.now() | ||
outdated_date = datetime.datetime.now() - datetime.timedelta(days=1) | ||
|
||
|
||
# add fulfillment request | ||
def get_add_fulfillment_request( | ||
product_name: str, timestamp, place_id | ||
) -> AddFulfillmentPlacesRequest: | ||
add_fulfillment_request = AddFulfillmentPlacesRequest() | ||
add_fulfillment_request.product = product_name | ||
add_fulfillment_request.type_ = "pickup-in-store" | ||
add_fulfillment_request.place_ids = [place_id] | ||
add_fulfillment_request.add_time = timestamp | ||
add_fulfillment_request.allow_missing = True | ||
|
||
print("---add fulfillment request---") | ||
print(add_fulfillment_request) | ||
|
||
return add_fulfillment_request | ||
|
||
|
||
# add fulfillment places to product | ||
def add_fulfillment_places(product_name: str, timestamp, place_id): | ||
add_fulfillment_request = get_add_fulfillment_request( | ||
product_name, timestamp, place_id | ||
) | ||
ProductServiceClient().add_fulfillment_places(add_fulfillment_request) | ||
|
||
# This is a long running operation and its result is not immediately present with get operations, | ||
# thus we simulate wait with sleep method. | ||
print("---add fulfillment places, wait 40 seconds :---") | ||
time.sleep(40) | ||
|
||
|
||
# [END retail_add_fulfillment_places] | ||
|
||
|
||
create_product(product_id) | ||
print("------add fulfilment places with current date: {}-----".format(current_date)) | ||
add_fulfillment_places(product_name, current_date, "store2") | ||
get_product(product_name) | ||
print("------add outdated fulfilment places: {}-----".format(outdated_date)) | ||
add_fulfillment_places(product_name, outdated_date, "store3") | ||
get_product(product_name) | ||
delete_product(product_name) |
34 changes: 34 additions & 0 deletions
34
generated_samples/interactive-tutorials/product/add_fulfillment_places_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2022 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import re | ||
import subprocess | ||
|
||
|
||
def test_add_fulfillment(): | ||
output = str( | ||
subprocess.check_output("python add_fulfillment_places.py", shell=True) | ||
) | ||
|
||
assert re.match(".*product is created.*", output) | ||
assert re.match(".*add fulfillment request.*", output) | ||
assert re.match(".*add fulfillment places.*", output) | ||
assert re.match( | ||
'.*get product response.*?fulfillment_info.*type_: "pickup-in-store".*?place_ids: "store1".*', | ||
output, | ||
) | ||
assert re.match( | ||
'.*get product response.*?fulfillment_info.*type_: "pickup-in-store".*?place_ids: "store2".*', | ||
output, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
generated_samples/interactive-tutorials/product/create_product.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2022 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
|
||
# [START retail_create_product] | ||
# Create product in a catalog using Retail API | ||
# | ||
import os | ||
import random | ||
import string | ||
|
||
from google.cloud.retail import CreateProductRequest, Product, ProductServiceClient | ||
from google.cloud.retail import PriceInfo | ||
from google.cloud.retail_v2.types import product | ||
|
||
from setup_product.setup_cleanup import delete_product | ||
|
||
project_id = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
default_branch_name = ( | ||
"projects/" | ||
+ project_id | ||
+ "/locations/global/catalogs/default_catalog/branches/default_branch" | ||
) | ||
generated_product_id = "".join(random.sample(string.ascii_lowercase, 8)) | ||
|
||
|
||
# generate product to create | ||
def generate_product() -> Product: | ||
price_info = PriceInfo() | ||
price_info.price = 30.0 | ||
price_info.original_price = 35.5 | ||
price_info.currency_code = "USD" | ||
return product.Product( | ||
title="Nest Mini", | ||
type_=product.Product.Type.PRIMARY, | ||
categories=["Speakers and displays"], | ||
brands=["Google"], | ||
price_info=price_info, | ||
availability="IN_STOCK", | ||
) | ||
|
||
|
||
# get create product request | ||
def get_create_product_request(product_to_create: Product, product_id: str) -> object: | ||
create_product_request = CreateProductRequest() | ||
create_product_request.product = product_to_create | ||
create_product_request.product_id = product_id | ||
create_product_request.parent = default_branch_name | ||
|
||
print("---create product request---") | ||
print(create_product_request) | ||
|
||
return create_product_request | ||
|
||
|
||
# call the Retail API to create product | ||
def create_product(product_id: str): | ||
create_product_request = get_create_product_request(generate_product(), product_id) | ||
product_created = ProductServiceClient().create_product(create_product_request) | ||
|
||
print("---created product:---") | ||
print(product_created) | ||
return product_created | ||
|
||
|
||
# create a product | ||
created_product = create_product(generated_product_id) | ||
# delete created product | ||
delete_product(created_product.name) | ||
# [END retail_create_product] |
29 changes: 29 additions & 0 deletions
29
generated_samples/interactive-tutorials/product/create_product_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2022 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import re | ||
import subprocess | ||
|
||
|
||
def test_create_product(): | ||
output = str(subprocess.check_output("python create_product.py", shell=True)) | ||
|
||
assert re.match(".*create product request.*", output) | ||
assert re.match(".*created product.*", output) | ||
assert re.match( | ||
'.*name: "projects/.+/locations/global/catalogs/default_catalog/branches/0/products/.*', | ||
output, | ||
) | ||
assert re.match('.*title: "Nest Mini".*', output) | ||
assert re.match(".*product.*was deleted.*", output) |
143 changes: 143 additions & 0 deletions
143
generated_samples/interactive-tutorials/product/crud_product.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Copyright 2022 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
|
||
# [START retail_crud_product] | ||
# Create product in a catalog using Retail API | ||
# | ||
import os | ||
import random | ||
import string | ||
|
||
from google.cloud.retail import ( | ||
CreateProductRequest, | ||
DeleteProductRequest, | ||
GetProductRequest, | ||
Product, | ||
ProductServiceClient, | ||
UpdateProductRequest, | ||
) | ||
from google.cloud.retail import PriceInfo | ||
from google.cloud.retail_v2.types import product | ||
|
||
project_id = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
default_branch_name = ( | ||
"projects/" | ||
+ project_id | ||
+ "/locations/global/catalogs/default_catalog/branches/default_branch" | ||
) | ||
product_id = "".join(random.sample(string.ascii_lowercase, 8)) | ||
product_name = "{}/products/{}".format(default_branch_name, product_id) | ||
|
||
|
||
# generate product for create | ||
def generate_product() -> Product: | ||
price_info = PriceInfo() | ||
price_info.price = 30.0 | ||
price_info.original_price = 35.5 | ||
price_info.currency_code = "USD" | ||
return product.Product( | ||
title="Nest Mini", | ||
type_=product.Product.Type.PRIMARY, | ||
categories=["Speakers and displays"], | ||
brands=["Google"], | ||
price_info=price_info, | ||
availability="IN_STOCK", | ||
) | ||
|
||
|
||
# generate product for update | ||
def generate_product_for_update() -> Product: | ||
price_info = PriceInfo() | ||
price_info.price = 20.0 | ||
price_info.original_price = 25.5 | ||
price_info.currency_code = "EUR" | ||
return product.Product( | ||
id=product_id, | ||
name=product_name, | ||
title="Updated Nest Mini", | ||
type_=product.Product.Type.PRIMARY, | ||
categories=["Updated Speakers and displays"], | ||
brands=["Updated Google"], | ||
availability="OUT_OF_STOCK", | ||
price_info=price_info, | ||
) | ||
|
||
|
||
# create product | ||
def create_product() -> object: | ||
create_product_request = CreateProductRequest() | ||
create_product_request.product = generate_product() | ||
create_product_request.product_id = product_id | ||
create_product_request.parent = default_branch_name | ||
|
||
print("---create product request---") | ||
print(create_product_request) | ||
|
||
product_created = ProductServiceClient().create_product(create_product_request) | ||
print("---created product:---") | ||
print(product_created) | ||
return product_created | ||
|
||
|
||
# get product | ||
def get_product() -> object: | ||
get_product_request = GetProductRequest() | ||
get_product_request.name = product_name | ||
|
||
print("---get product request---") | ||
print(get_product_request) | ||
|
||
get_product_response = ProductServiceClient().get_product(get_product_request) | ||
|
||
print("---get product response:---") | ||
print(get_product_response) | ||
return get_product_response | ||
|
||
|
||
# update product | ||
def update_product(): | ||
update_product_request = UpdateProductRequest() | ||
update_product_request.product = generate_product_for_update() | ||
update_product_request.allow_missing = True | ||
|
||
print("---update product request---") | ||
print(update_product_request) | ||
|
||
updated_product = ProductServiceClient().update_product(update_product_request) | ||
print("---updated product---:") | ||
print(updated_product) | ||
return updated_product | ||
|
||
|
||
# delete product | ||
def delete_product(): | ||
delete_product_request = DeleteProductRequest() | ||
delete_product_request.name = product_name | ||
|
||
print("---delete product request---") | ||
print(delete_product_request) | ||
|
||
ProductServiceClient().delete_product(delete_product_request) | ||
|
||
print("deleting product " + product_name) | ||
print("---product was deleted:---") | ||
|
||
|
||
# call the methods | ||
create_product() | ||
get_product() | ||
update_product() | ||
delete_product() | ||
# [END retail_crud_product] |
Oops, something went wrong.