1 | var request = require('request');
|
2 | var ziptoo = require('funkit').functional.ziptoo;
|
3 |
|
4 |
|
5 | function init(config) {
|
6 |
|
7 | var baseUrl = 'https://api.pingdom.com/api/2.0/';
|
8 | var resources = ['actions', 'analysis', 'checks', 'contacts', 'credits', 'probes',
|
9 | 'reference', 'reports.email', 'reports.public', 'reports.shared',
|
10 | 'results', 'servertime', 'settings', 'summary.average', 'summary.hoursofday',
|
11 | 'summary.outage', 'summary.performance', 'summary.probes', 'single', 'traceroute'
|
12 | ];
|
13 |
|
14 | return ziptoo(resources.map(function(resource) {
|
15 | return [resource, template.bind(undefined, config, baseUrl, resource)];
|
16 | }));
|
17 | }
|
18 | module.exports = init;
|
19 |
|
20 | function template(config, baseUrl, property, cb, o) {
|
21 | o = o || {};
|
22 | var target = o.target || '';
|
23 | var qs = o.qs || {};
|
24 |
|
25 |
|
26 | if(qs.to) qs.to = dateToUnix(qs.to);
|
27 | if(qs.from) qs.from = dateToUnix(qs.from);
|
28 |
|
29 | request.get(baseUrl + property + '/' + target, {
|
30 | auth: config,
|
31 | headers: {
|
32 | 'App-Key': config.appkey
|
33 | },
|
34 | qs: qs
|
35 | }, function(err, res) {
|
36 | try {
|
37 | cb(err, JSON.parse(res.body)[property.split('.')[0]], res);
|
38 | }
|
39 | catch(e) {
|
40 | cb(e, null, res);
|
41 | }
|
42 | });
|
43 | }
|
44 |
|
45 | function dateToUnix(date) {
|
46 | return parseInt(date.getTime() / 1000, 10);
|
47 | }
|