-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenkgoapi.js
76 lines (60 loc) · 1.62 KB
/
genkgoapi.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
var https = require('https');
function GenkgoApi (url, api_key) {
this.url = url;
this.api_key = api_key;
this._debug = false;
}
GenkgoApi.prototype = {
command: function (part, command, parameters, readyFunction) {
var data = {
part: part,
command: command,
token: this.api_key
}
var post_data = {};
for (var attrname in data) { post_data[attrname] = data[attrname]; }
for (var attrname in parameters) { post_data[attrname] = parameters[attrname]; }
this.post(post_data, readyFunction);
},
post: function (data, readyFunction) {
var querystring = require('querystring');
var post_data = querystring.stringify(data);
var options = {
hostname: this.url,
port: 443,
path: '/admin/api',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
}
var obj = this;
var req = https.request(options, function(res) {
obj.debug('STATUS: ' + res.statusCode);
obj.debug('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (data) {
data = JSON.parse(data)
readyFunction(data);
obj.debug('BODY: ' + data);
});
});
req.on('error', function(e) {
obj.debug('problem with request: ' + e.message);
});
req.write(post_data);
req.end();
},
debug: function (data) {
if (typeof this._debug == 'function') {
this._debug(data);
}
},
setDebug: function (value) {
this._debug = value;
}
};
exports.create = function (url, apiKey) {
return new GenkgoApi(url, apiKey);
}