-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperStore.js
81 lines (63 loc) · 2.4 KB
/
SuperStore.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
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
const https = require('follow-redirects').https
const fs = require('fs')
const {gzip, ungzip} = require('node-gzip')
const {PCExpressAPIRequestBuilder, SampleValues} = require('./PCExpressAPIRequestBuilder.js')
//var rb = new PCExpressAPIRequestBuilder(SampleValues.xapikey, SampleValues.cartId)
var items = []
class SuperStore {
constructor(storeId = SampleValues.halifaxStoreId, cartId = SampleValues.cartId){
this.storeId = storeId
this.cartId = cartId
this.rb = new PCExpressAPIRequestBuilder(SampleValues.xapikey, this.cartId)
}
search(query){
var postData = this.rb.searchBody(query, this.storeId, this.cartId)
var searchHeader = this.rb.searchHeader(postData.length)
var items = []
var req = https.request(searchHeader, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks)
ungzip(body).then((decompressed) => {
var items = []
var content = JSON.parse(decompressed.toString())
for (let result of content.results){
items.push({
name: result.name,
brand: result.brand,
code: result.code
})
}
// return
})
});
res.on("error", function (error) {
console.error(error);
});
});
req.write(postData);
req.end();
return items
}
getItem(itemId){
var options = this.rb.singleItemHeader(itemId, this.storeId)
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var content = JSON.parse(Buffer.concat(chunks).toString());
//return
});
res.on("error", function (error) {
console.error(error);
});
});
req.end();
}
}
exports.SuperStore = SuperStore