forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-products-inline-source.js
More file actions
129 lines (116 loc) · 3.88 KB
/
import-products-inline-source.js
File metadata and controls
129 lines (116 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2022 Google Inc.
//
// 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';
// [START retail_import_products_from_inline_source]
async function main(id1, id2) {
// Imports the Google Cloud client library.
const {ProductServiceClient} = require('@google-cloud/retail').v2;
const utils = require('../setup/setup-cleanup');
// Instantiates a client.
const retailClient = new ProductServiceClient();
const projectId = await retailClient.getProjectId();
// Placement
const parent = `projects/${projectId}/locations/global/catalogs/default_catalog/branches/default_branch`;
const product1 = {
id: id1 ? id1 : Math.random().toString(36).slice(2).toUpperCase(),
title: '#IamRemarkable Pen', //TO CHECK ERROR HANDLING COMMENT OUT THE PRODUCT TITLE HERE
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Office/IamRemarkable+Pen',
brands: ['#IamRemarkable'],
categories: ['Apparel'],
priceInfo: {
price: 16.0,
originalPrice: 45.0,
cost: 12.0,
currencyCode: 'USD',
},
colorInfo: {
colorFamilies: ['Blue'],
colors: ['Light blue', 'Blue', 'Dark blue'],
},
fulFillmentInfo: {
type: 'pickup-in-store',
placeIds: ['store1', 'store2'],
},
retrievable_fields: {
paths: ['title', 'categories', 'price_info', 'color_info'],
},
};
const product2 = {
id: id2 ? id2 : Math.random().toString(36).slice(2).toUpperCase(),
title: 'Android Embroidered Crewneck Sweater',
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Android+Embroidered+Crewneck+Sweater',
brands: ['Android'],
categories: ['Apparel'],
priceInfo: {
price: 35.0,
originalPrice: 45.0,
cost: 12.0,
currencyCode: 'USD',
},
colorInfo: {
colorFamilies: ['Blue'],
colors: ['Sky blue'],
},
fulFillmentInfo: {
type: 'pickup-in-store',
placeIds: ['store2', 'store3'],
},
retrievable_fields: {
paths: ['title', 'categories', 'price_info', 'color_info'],
},
};
// The desired input location of the data.
const inputConfig = {
productInlineSource: {
products: [product1, product2],
},
};
const IResponseParams = {
IImportProductsResponse: 0,
IImportMetadata: 1,
IOperation: 2,
};
const callImportProducts = async () => {
// Construct request
const request = {
parent,
inputConfig,
};
console.log('Import products request:', request);
// Run request
const [operation] = await retailClient.importProducts(request);
const response = await operation.promise();
const result = response[IResponseParams.IImportMetadata];
console.log(
`Number of successfully imported products: ${result.successCount | 0}`
);
console.log(
`Number of failures during the importing: ${result.failureCount | 0}`
);
console.log(`Operation result: ${JSON.stringify(response)}`);
};
// Start import products
console.log('Start import products');
await callImportProducts();
console.log('Import products finished');
// Delete imported products
await utils.deleteProductsByIds(projectId, [product1.id, product2.id]);
console.log('Products deleted');
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
// [END retail_import_products_from_inline_source]