-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(samples): interactive tutorials code samples for CRUD products (#…
…149)
- Loading branch information
1 parent
eca0809
commit c70edc6
Showing
18 changed files
with
1,621 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
retail/interactive-tutorials/product/add-fulfillment-places.js
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,98 @@ | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
async function main(generatedProductId) { | ||
// [START retail_add_remove_fulfillment_places] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
const utils = require('../setup/setup-cleanup'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Create product | ||
const createdProduct = await utils.createProduct( | ||
projectNumber, | ||
generatedProductId | ||
); | ||
|
||
// Full resource name of Product | ||
const product = createdProduct.name; | ||
|
||
// The fulfillment type, including commonly used types (such as | ||
// pickup in store and same day delivery), and custom types. | ||
const type = 'same-day-delivery'; | ||
|
||
// The IDs for this type, such as the store IDs for "pickup-in-store" or the region IDs for | ||
// "same-day-delivery" to be added for this type. | ||
const placeIds = ['store1', 'store2', 'store3']; | ||
|
||
// The time when the fulfillment updates are issued, used to prevent | ||
// out-of-order updates on fulfillment information. | ||
const addTime = { | ||
seconds: Math.round(Date.now() / 1000), | ||
}; | ||
|
||
//If set to true, and the product is not found, the fulfillment information will still be processed and retained for | ||
// at most 1 day and processed once the product is created | ||
const allowMissing = true; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const calladdFulfillmentPlaces = async () => { | ||
// Construct request | ||
const request = { | ||
product, | ||
type, | ||
placeIds, | ||
addTime, | ||
allowMissing, | ||
}; | ||
|
||
console.log('Add fulfillment request:', request); | ||
|
||
// Run request | ||
await retailClient.addFulfillmentPlaces(request); | ||
|
||
console.log('Waiting to complete add operation..'); | ||
}; | ||
|
||
// Add fulfillment places with current time | ||
console.log('Start add fulfillment'); | ||
await calladdFulfillmentPlaces(); | ||
await utils.delay(180000); | ||
|
||
//Get product | ||
const response = await utils.getProduct(product); | ||
console.log( | ||
'Updated product with current time: ', | ||
JSON.stringify(response[0]) | ||
); | ||
console.log('Add fulfillment finished'); | ||
|
||
// Delete product | ||
await utils.deleteProduct(product); | ||
console.log(`Product ${createdProduct.id} deleted`); | ||
// [END retail_add_remove_fulfillment_places] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error('ERROR', err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(...process.argv.slice(2)); |
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,83 @@ | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
async function main(generatedProductId) { | ||
// [START retail_create_product] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
const utils = require('../setup/setup-cleanup'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// The parent catalog resource name | ||
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; | ||
|
||
// The ID to use for the product | ||
const productId = generatedProductId | ||
? generatedProductId | ||
: Math.random().toString(36).slice(2).toUpperCase(); | ||
|
||
// The product to create. | ||
const product = { | ||
title: 'Nest Mini', | ||
type: 'PRIMARY', | ||
categories: ['Speakers and displays'], | ||
brands: ['Google'], | ||
priceInfo: { | ||
price: 30.0, | ||
originalPrice: 35.5, | ||
currencyCode: 'USD', | ||
}, | ||
availability: 'IN_STOCK', | ||
}; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callCreateProduct = async () => { | ||
// Construct request | ||
const request = { | ||
parent, | ||
product, | ||
productId, | ||
}; | ||
console.log('Create product request:', request); | ||
|
||
// Run request | ||
const response = await retailClient.createProduct(request); | ||
console.log('Created product:', response); | ||
return response[0]; | ||
}; | ||
|
||
// Create product | ||
console.log('Start to create the product'); | ||
const createdProduct = await callCreateProduct(); | ||
console.log(`Product ${createdProduct.id} creation ended`); | ||
|
||
// Delete product | ||
await utils.deleteProduct(createdProduct.name); | ||
console.log(`Product ${createdProduct.id} deleted`); | ||
|
||
// [END retail_create_product] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(...process.argv.slice(2)); |
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,155 @@ | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
async function main(generatedProductId) { | ||
// [START retail_crud_product] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// The parent catalog resource name | ||
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; | ||
|
||
// The ID to use for the product | ||
const productId = generatedProductId | ||
? generatedProductId | ||
: Math.random().toString(36).slice(2).toUpperCase(); | ||
|
||
// Full resource name of Product | ||
const name = `${parent}/products/${productId}`; | ||
|
||
// The product to create. | ||
const productForCreate = { | ||
title: 'Nest Mini', | ||
type: 'PRIMARY', | ||
categories: ['Speakers and displays'], | ||
brands: ['Google'], | ||
priceInfo: { | ||
price: 30.0, | ||
originalPrice: 35.5, | ||
currencyCode: 'USD', | ||
}, | ||
availability: 'IN_STOCK', | ||
}; | ||
|
||
// The product to update. | ||
const productForUpdate = { | ||
productId, | ||
name, | ||
title: 'Updated Nest Mini', | ||
type: 'PRIMARY', | ||
categories: ['Updated Speakers and displays'], | ||
brands: ['Updated Google'], | ||
priceInfo: { | ||
price: 20.0, | ||
originalPrice: 25.5, | ||
currencyCode: 'EUR', | ||
}, | ||
availability: 'OUT_OF_STOCK', | ||
}; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callCreateProduct = async () => { | ||
// Construct request | ||
const request = { | ||
parent, | ||
product: productForCreate, | ||
productId, | ||
}; | ||
console.log('Create product request:', request); | ||
|
||
// Run request | ||
const response = await retailClient.createProduct(request); | ||
console.log('Created product:', response); | ||
return response[0]; | ||
}; | ||
|
||
const callGetProduct = async () => { | ||
// Construct request | ||
const request = { | ||
name, | ||
}; | ||
console.log('Get product request:', request); | ||
|
||
// Run request | ||
const response = await retailClient.getProduct(request); | ||
console.log('Get product response:', response); | ||
|
||
return response[0]; | ||
}; | ||
|
||
const callUpdateProduct = async () => { | ||
// Construct request | ||
const request = { | ||
product: productForUpdate, | ||
}; | ||
console.log('Update product request:', request); | ||
|
||
// Run request | ||
const response = await retailClient.updateProduct(request); | ||
console.log('Updated product:', response); | ||
|
||
return response[0]; | ||
}; | ||
|
||
const callDeleteProduct = async () => { | ||
// Construct request | ||
const request = { | ||
name, | ||
}; | ||
console.log('Delete product request:', request); | ||
|
||
// Run request | ||
await retailClient.deleteProduct(request); | ||
}; | ||
|
||
console.log('Start CRUD product'); | ||
// Create product | ||
console.log('Start to create the product'); | ||
const createdProduct = await callCreateProduct(); | ||
console.log(`Product ${createdProduct.id} creation finished`); | ||
|
||
// Get product | ||
console.log('Start product get operation'); | ||
const foundProduct = await callGetProduct(); | ||
console.log(`Product ${foundProduct.id} get operation finished`); | ||
|
||
// Update product | ||
console.log('Start product update'); | ||
const updatedProduct = await callUpdateProduct(); | ||
console.log( | ||
`Product ${updatedProduct.id} update finished: `, | ||
JSON.stringify(updatedProduct) | ||
); | ||
|
||
// Delete product | ||
console.log('Start deleting the product'); | ||
await callDeleteProduct(); | ||
console.log(`Product ${createdProduct.id} deleted`); | ||
console.log('CRUD product finished'); | ||
// [END retail_crud_product] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(...process.argv.slice(2)); |
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,58 @@ | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
async function main(generatedProductId) { | ||
// [START retail_delete_product] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
const utils = require('../setup/setup-cleanup'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Create product | ||
const product = await utils.createProduct(projectNumber, generatedProductId); | ||
|
||
// Full resource name of Product | ||
const name = product.name; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callDeleteProduct = async () => { | ||
// Construct request | ||
const request = { | ||
name, | ||
}; | ||
console.log('Delete product request:', request); | ||
|
||
// Run request | ||
await retailClient.deleteProduct(request); | ||
}; | ||
|
||
// Delete product | ||
console.log('Start deleting the product'); | ||
await callDeleteProduct(); | ||
console.log(`Product ${product.id} deleted`); | ||
// [END retail_delete_product] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(...process.argv.slice(2)); |
Oops, something went wrong.