-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.js
49 lines (48 loc) · 1.54 KB
/
helper.js
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
var Appbase = require('appbase-js');
var appbaseCredentials = require('./appbase_credentials.json')
/*
This is function which just gives details about products and call callback along with the details.
*/
module.exports.getProductDetails = function(productId, callback) {
var request = require('request');
var options = {
uri: 'https://affiliate-api.flipkart.net/affiliate/1.0/product.json?id=' + productId,
method: 'GET',
headers: require('./flipkart_credentials.json')
};
var data;
request(options, function(error, response, body) {
if (!error && response != undefined && response.statusCode == 200) {
data = JSON.parse(body);
console.log(data)
} else {
console.log("Got an error: ", error, ", status code: ", response);
}
}).on('complete', function() {
if (data != undefined)
callback(data);
});
}
/*
Function for indexing the product detail into appbase databse.
*/
module.exports.indexProduct = function(productId) {
this.getProductDetails(productId, function(data) {
var price = data.productBaseInfo.productAttributes.sellingPrice.amount;
var name = data.productBaseInfo.productAttributes.productBrand
var appbaseRef = new Appbase(appbaseCredentials);
appbaseRef.index({
type: appbaseCredentials.type,
id: productId,
body: {
'price': price,
'productId': productId,
'name': name
}
}).on('data', function(response) {
console.log(response);
}).on('error', function(error) {
console.log(error);
});
});
}