-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
44 lines (40 loc) · 1.5 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
var request = require('request');
var _ = require('underscore');
var getQueryParams = function(options) {
return _.mapObject(options, function(val, key) {
if (_.isArray(val)) return val.join('-');
if (_.isBoolean(val)) return val ? 1 : 0;
return val;
});
};
var Client = module.exports = function(apiKey) {
var getRequest = function(path) {
return function(options, cb) {
if (!cb) cb = options;
request.get('https://api.uptimerobot.com' + path, {
qs: _.extend({
apiKey: apiKey,
format: 'json',
noJsonCallback: 1
}, getQueryParams(options))
}, function(err, res, body) {
try {
var data = JSON.parse(body);
process.nextTick(function() {
cb(data.stat === 'fail' ? data.message : null, data);
});
} catch(ex) {
cb(err || ex, body);
}
});
};
};
this.getMonitors = getRequest('/getMonitors');
this.newMonitor = getRequest('/newMonitor');
this.editMonitor = getRequest('/editMonitor');
this.deleteMonitor = getRequest('/deleteMonitor');
this.resetMonitor = getRequest('/resetMonitor');
this.getAlertContacts = getRequest('/getAlertContacts');
this.newAlertContact = getRequest('/newAlertContact');
this.deleteAlertContact = getRequest('/deleteAlertContact');
};