|
| 1 | +# Copyright 2021 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_import_products_from_gcs] |
| 16 | +# Import products into a catalog from gcs using Retail API |
| 17 | +# |
| 18 | +import os |
| 19 | +import time |
| 20 | + |
| 21 | +from google.cloud.retail import ( |
| 22 | + GcsSource, |
| 23 | + ImportErrorsConfig, |
| 24 | + ImportProductsRequest, |
| 25 | + ProductInputConfig, |
| 26 | + ProductServiceClient, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +# Read the project number from the environment variable |
| 31 | +project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] |
| 32 | +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 33 | +bucket_name = os.environ["BUCKET_NAME"] |
| 34 | + |
| 35 | +# You can change the branch here. The "default_branch" is set to point to the branch "0" |
| 36 | +default_catalog = f"projects/{project_number}/locations/global/catalogs/default_catalog/branches/default_branch" |
| 37 | + |
| 38 | +gcs_bucket = f"gs://{bucket_name}" |
| 39 | +gcs_errors_bucket = f"{gcs_bucket}/error" |
| 40 | +gcs_products_object = "products.json" |
| 41 | + |
| 42 | + |
| 43 | +# TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT |
| 44 | +# gcs_products_object = "products_some_invalid.json" |
| 45 | + |
| 46 | + |
| 47 | +# get import products from gcs request |
| 48 | +def get_import_products_gcs_request(gcs_object_name: str): |
| 49 | + # TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: |
| 50 | + # default_catalog = "invalid_catalog_name" |
| 51 | + gcs_source = GcsSource() |
| 52 | + gcs_source.input_uris = [f"{gcs_bucket}/{gcs_object_name}"] |
| 53 | + |
| 54 | + input_config = ProductInputConfig() |
| 55 | + input_config.gcs_source = gcs_source |
| 56 | + print("GRS source:") |
| 57 | + print(gcs_source.input_uris) |
| 58 | + |
| 59 | + errors_config = ImportErrorsConfig() |
| 60 | + errors_config.gcs_prefix = gcs_errors_bucket |
| 61 | + |
| 62 | + import_request = ImportProductsRequest() |
| 63 | + import_request.parent = default_catalog |
| 64 | + import_request.reconciliation_mode = ( |
| 65 | + ImportProductsRequest.ReconciliationMode.INCREMENTAL |
| 66 | + ) |
| 67 | + import_request.input_config = input_config |
| 68 | + import_request.errors_config = errors_config |
| 69 | + |
| 70 | + print("---import products from google cloud source request---") |
| 71 | + print(import_request) |
| 72 | + |
| 73 | + return import_request |
| 74 | + |
| 75 | + |
| 76 | +# call the Retail API to import products |
| 77 | +def import_products_from_gcs(): |
| 78 | + import_gcs_request = get_import_products_gcs_request(gcs_products_object) |
| 79 | + gcs_operation = ProductServiceClient().import_products(import_gcs_request) |
| 80 | + |
| 81 | + print("---the operation was started:----") |
| 82 | + print(gcs_operation.operation.name) |
| 83 | + |
| 84 | + while not gcs_operation.done(): |
| 85 | + print("---please wait till operation is done---") |
| 86 | + time.sleep(30) |
| 87 | + print("---import products operation is done---") |
| 88 | + |
| 89 | + if gcs_operation.metadata is not None: |
| 90 | + print("---number of successfully imported products---") |
| 91 | + print(gcs_operation.metadata.success_count) |
| 92 | + print("---number of failures during the importing---") |
| 93 | + print(gcs_operation.metadata.failure_count) |
| 94 | + else: |
| 95 | + print("---operation.metadata is empty---") |
| 96 | + |
| 97 | + if gcs_operation.result is not None: |
| 98 | + print("---operation result:---") |
| 99 | + print(gcs_operation.result()) |
| 100 | + else: |
| 101 | + print("---operation.result is empty---") |
| 102 | + |
| 103 | + # The imported products needs to be indexed in the catalog before they become available for search. |
| 104 | + print( |
| 105 | + "Wait 2-5 minutes till products become indexed in the catalog, after that they will be available for search" |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +import_products_from_gcs() |
| 110 | + |
| 111 | +# [END retail_import_products_from_gcs] |
0 commit comments