-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsampleGetItemsApi.js
More file actions
126 lines (112 loc) · 5.13 KB
/
sampleGetItemsApi.js
File metadata and controls
126 lines (112 loc) · 5.13 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
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. 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.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
// Run `npm install` locally before executing following code with `node sampleGetItemsApi.js`
/**
* This sample code snippet is for ProductAdvertisingAPI 5.0's GetItems API
* For more details, refer:
* https://webservices.amazon.com/paapi5/documentation/get-items.html
*/
var ProductAdvertisingAPIv1 = require('./src/index');
var defaultClient = ProductAdvertisingAPIv1.ApiClient.instance;
// Specify your credentials here. These are used to create and sign the request.
defaultClient.accessKey = 'AKIAINXLTJNAZHBE753Q';
defaultClient.secretKey = '2TVWkJOO1ost5oCglZsO9';
// myConfig.accessKey = 'AKIAINXLTJNAZHBE753Q'
// myConfig.secretKey = '2TVWkJOO1ost5oCglZsO9'
// myConfig.partnerTag = 'getaheadphone-20'
/**
* PAAPI Host and Region to which you want to send request.
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
*/
defaultClient.host = 'webservices.amazon.com';
defaultClient.region = 'us-east-1';
var api = new ProductAdvertisingAPIv1.DefaultApi();
// Request Initialization
var getItemsRequest = new ProductAdvertisingAPIv1.GetItemsRequest();
/** Enter your partner tag (store/tracking id) and partner type */
getItemsRequest['PartnerTag'] = 'getaheadphone-20';
getItemsRequest['PartnerType'] = 'Associates';
/** Enter the Item IDs for which item information is desired */
getItemsRequest['ItemIds'] = ['B00XBC3BF0'];
getItemsRequest['Condition'] = 'New';
/**
* Choose resources you want from GetItemsResource enum
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter
*/
getItemsRequest['Resources'] = ['Images.Primary.Medium', 'ItemInfo.Title', 'Offers.Listings.Price'];
/**
* Function to parse GetItemsResponse into an object with key as ASIN
*/
function parseResponse(itemsResponseList) {
var mappedResponse = {};
for (var i in itemsResponseList) {
mappedResponse[itemsResponseList[i]['ASIN']] = itemsResponseList[i];
}
return mappedResponse;
}
var callback = function (error, data, response) {
if (error) {
console.log('Error calling PA-API 5.0!');
console.log('Printing Full Error Object:\n' + JSON.stringify(error, null, 1));
console.log('Status Code: ' + error['status']);
if (error['response'] !== undefined && error['response']['text'] !== undefined) {
console.log('Error Object: ' + JSON.stringify(error['response']['text'], null, 1));
}
} else {
console.log('API called successfully.');
var getItemsResponse = ProductAdvertisingAPIv1.GetItemsResponse.constructFromObject(data);
console.log('Complete Response: \n' + JSON.stringify(getItemsResponse, null, 1));
if (getItemsResponse['ItemsResult'] !== undefined) {
console.log('Printing All Item Information in ItemsResult:');
var response_list = parseResponse(getItemsResponse['ItemsResult']['Items']);
for (var i in getItemsRequest['ItemIds']) {
var itemId = getItemsRequest['ItemIds'][i];
console.log('\nPrinting information about the Item with Id: ' + itemId);
if (itemId in response_list) {
var item = response_list[itemId];
if (item !== undefined) {
if (item['ASIN'] !== undefined) {
console.log('ASIN: ' + item['ASIN']);
}
if (item['DetailPageURL'] !== undefined) {
console.log('DetailPageURL: ' + item['DetailPageURL']);
}
if (item['ItemInfo'] !== undefined && item['ItemInfo']['Title'] !== undefined && item['ItemInfo']['Title']['DisplayValue'] !== undefined) {
console.log('Title: ' + item['ItemInfo']['Title']['DisplayValue']);
}
if (item['Offers'] !== undefined && item['Offers']['Listings'] !== undefined && item['Offers']['Listings'][0]['Price'] !== undefined && item['Offers']['Listings'][0]['Price']['DisplayAmount'] !== undefined) {
console.log('Buying Price: ' + item['Offers']['Listings'][0]['Price']['DisplayAmount']);
}
}
} else {
console.log('Item not found, check errors')
}
}
}
if (getItemsResponse['Errors'] !== undefined) {
console.log('\nErrors:');
console.log('Complete Error Response: ' + JSON.stringify(getItemsResponse['Errors'], null, 1));
console.log('Printing 1st Error:');
var error_0 = getItemsResponse['Errors'][0];
console.log('Error Code: ' + error_0['Code']);
console.log('Error Message: ' + error_0['Message']);
}
}
};
try {
api.getItems(getItemsRequest, callback);
} catch (ex) {
console.log("Exception: " + ex);
}