forked from dancalligaro/popit-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
263 lines (184 loc) · 4.45 KB
/
index.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var http = require('http');
var Q = require('q');
var config;
function setConfig(_conf){
config = _conf;
}
function postItems(kind, items){
return enqueue(postThisJson, kind, items);
}
function deleteItems(kind, items){
return enqueue(deleteItem, kind, items);
}
function enqueue(fn, kind, items){
var deferred = Q.defer();
// Max 50 simultaneous
var currents = 0;
var max = 50;
function goNext(){
var item;
while( (max - currents) > 0 && ( item = items.shift() ) ){
currents++;
(function(p){
fn( item , kind, function(result){
deferred.notify(result);
currents--;
if(items.length == 0 && currents ==0){
deferred.resolve();
}
} );
})(item);
}
if(items.length > 0){
setTimeout(goNext, 100);
}
}
goNext();
return deferred.promise;
}
function postThisJson(objToPost, api, cb){
// api can be one of ( persons, organizations, memberships, posts );
var user = config.user;
var pwd = config.password;
var postString = JSON.stringify(objToPost);
var headers = {
'Authorization': 'Basic ' + new Buffer(user + ':' + pwd).toString('base64'),
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postString)
};
var options = {
host: config.host,
port: 80,
path: '/api/v0.1/' + api,
method: 'POST',
headers: headers
};
// Setup the request. The options parameter is
// the object we defined above.
var req = http.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
if(typeof cb == "function"){
cb(responseString);
}
});
});
req.on('error', function(e) {
// TODO: handle error.
console.log("ERROR", e);
});
req.write(postString);
req.end();
}
function deleteItem(id, api, cb){
// api can be one of ( persons, organizations, memberships, posts );
var user = config.user;
var pwd = config.password;
var headers = {
'Authorization': 'Basic ' + new Buffer(user + ':' + pwd).toString('base64'),
'Content-Type': 'application/json',
'Content-Length': '0'
};
var options = {
host: config.host,
port: 80,
path: '/api/v0.1/' + api + "/" + id,
method: 'DELETE',
headers: headers
};
// Setup the request. The options parameter is
// the object we defined above.
var req = http.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
if(typeof cb == "function"){
cb(responseString);
}
});
});
req.on('error', function(e) {
// TODO: handle error.
console.log("ERROR", e);
});
req.end();
}
function getJson(path){
var deferred = Q.defer();
var headers = {
};
var options = {
host: config.host,
port: 80,
path: path,
method: 'GET',
headers: headers
};
var req = http.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
try{
deferred.resolve( JSON.parse(responseString) );
}catch(ex){
deferred.reject(ex);
}
});
});
req.on('error', function(e) {
console.log("ERROR", e);
deferred.reject(e);
});
req.end();
return deferred.promise;
}
function loadAllItems(type){
var deferred = Q.defer();
var url = '/api/v0.1/' + type + '?per_page=200&page={{page}}';
var page = 1;
var results = [];
function getUrl(page){
return url.replace("{{page}}", page);
}
function loadPage(){
getJson( getUrl(1) ).then(function(response){
results = response.result;
if(response.has_more){
var totalPages = Math.ceil(response.total / 200); //200 = items per page
var queue = [];
deferred.notify({ totalPages: totalPages, totalItems: response.total });
for(var i = 2 ; i <= totalPages ; i++){
queue.push ( getJson( getUrl(i) ) );
}
Q.all(queue).then(function(responses){
responses.forEach(function(response){
results = results.concat(response.result);
});
deferred.resolve(results);
});
}else{
deferred.resolve(results);
}
}, function(){
console.log("error loading");
});
}
loadPage();
return deferred.promise;
}
module.exports = {
loadAllItems: loadAllItems,
postItems: postItems,
deleteItems: deleteItems,
config: setConfig
};