This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into tutorials-events-crud
- Loading branch information
Showing
49 changed files
with
1,250 additions
and
302 deletions.
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
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
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
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
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) |
Oops, something went wrong.