-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREST.js
52 lines (43 loc) · 1.4 KB
/
REST.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
var querystring = require('querystring');
var https = require('https');
module.exports = {
get: function (host, endpoint, token, data, success) {
this.call(host, endpoint, token, 'GET', data, success);
},
post: function (host, endpoint, token, data, success) {
this.call(host, endpoint, token, 'POST', data, success);
},
call: function (host, endpoint, token, method, data, success) {
var dataString = JSON.stringify(data);
var headers = {};
if (method == 'GET') {
endpoint += '?' + querystring.stringify(data);
}
else {
headers = {
'Content-Type': 'application/json',
'Content-Length': dataString.length
};
}
headers['Authorization'] = 'Bearer ' + token;
var options = {
host: host,
path: endpoint,
method: method,
headers: headers
};
var req = https.request(options, function (res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function (data) {
responseString += data;
});
res.on('end', function () {
var responseObject = JSON.parse(responseString);
success(responseObject);
});
});
req.write(dataString);
req.end();
}
}