Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into tutorials-events-crud
Browse files Browse the repository at this point in the history
  • Loading branch information
kweinmeister authored Feb 18, 2022
2 parents 8b83681 + d8f8e34 commit f50adb5
Show file tree
Hide file tree
Showing 49 changed files with 1,250 additions and 302 deletions.
2 changes: 1 addition & 1 deletion samples/interactive-tutorials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_
1. To create the bucket and upload the JSON file, run the following command in the Terminal:

```bash
python product/setup/create_gcs_bucket.py
python product/setup_product/create_gcs_bucket.py
```

Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded.
Expand Down
80 changes: 80 additions & 0 deletions samples/interactive-tutorials/product/add_fulfillment_places.py
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)
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,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Google Inc. All Rights Reserved.
# 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.
Expand All @@ -12,14 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import pytest
import test_utils.prefixer

from setup_cleanup import delete_bucket
prefixer = test_utils.prefixer.Prefixer(
"python-retail", "samples/interactive-tutorials/product"
)


def delete_bucket_by_name(name: str):
if name is None:
bucket_name = os.environ["BUCKET_NAME"]
delete_bucket(bucket_name)
else:
delete_bucket(name)
@pytest.fixture(scope="session")
def table_id_prefix() -> str:
return prefixer.create_prefix()


@pytest.fixture(scope="session")
def bucket_name_prefix() -> str:
return prefixer.create_prefix()
81 changes: 81 additions & 0 deletions samples/interactive-tutorials/product/create_product.py
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 samples/interactive-tutorials/product/create_product_test.py
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)
Loading

0 comments on commit f50adb5

Please sign in to comment.