forked from eladnava/cachet-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
300 lines (250 loc) · 8.76 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
var util = require('util');
var request = require('request');
var Promise = require('bluebird');
var statusCodes = require('./util/statusCodes');
// Internal package configuration
var config = {
// Supported Cachet API version
apiVersion: 1
};
// Package constructor
function CachetAPI(options) {
// Make sure the developer provided the status page URL
if (!options.url) {
throw new Error('Please provide your Cachet API endpoint URL to use this package.');
}
// Make sure the developer provided the Cachet API key
if (!options.apiKey) {
throw new Error('Please provide your API key to use this package.');
}
// Add trailing slash if ommitted in status page URL
if (options.url.substr(-1) !== '/') {
options.url += '/';
}
// Append api/v1 to the URL
options.url += 'api/v' + config.apiVersion;
// Keep track of URL for later
this.url = options.url;
// Prepare extra headers to be sent with all API requests
this.headers = {
// Cachet API authentication header
'X-Cachet-Token': options.apiKey
};
}
function parseFilter(filter) {
return Object.keys(filter).map(function(param, idx) {
var queryParam = param + '=' + filter[param];
return idx === 0 ? '?' + queryParam : queryParam;
}).join('&');
}
CachetAPI.prototype.publishMetricPoint = function(metricPoint) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// No metric point provided?
if (!metricPoint) {
return reject(new Error('Please provide the metric point to publish.'));
}
// Point must be an object
if (typeof metricPoint !== 'object') {
return reject(new Error('Please provide the metric point as an object.'));
}
// Check for missing metric ID
if (!metricPoint.id) {
return reject(new Error('Please provide the metric ID.'));
}
// Check for missing metric value
if (metricPoint.value === null) {
return reject(new Error('Please provide the metric point value.'));
}
// Prepare API request
var req = {
method: 'POST',
json: metricPoint,
headers: that.headers,
url: that.url + '/metrics/' + metricPoint.id + '/points'
};
// Execute request
request(req, function(err, res, body) {
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
CachetAPI.prototype.reportIncident = function(incident) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// No incident provided?
if (!incident) {
return reject(new Error('Please provide the incident to report.'));
}
// Incident must be an object
if (typeof incident !== 'object') {
return reject(new Error('Please provide the incident as an object.'));
}
// Check for required parameters
if (!incident.name || !incident.message || !incident.status || !incident.visible) {
return reject(new Error('Please provide the incident name, message, status, and visibility.'));
}
// Convert boolean values to integers
incident.notify = incident.notify ? 1 : 0;
incident.visible = incident.visible ? 1 : 0;
try {
// Attempt to convert incident status name to code
incident.status = statusCodes.getIncidentStatusCode(incident.status);
// Incident status provided?
if (incident.component_status) {
// Attempt to convert component status name to code
incident.component_status = statusCodes.getComponentStatusCode(incident.component_status);
}
} catch (err) {
// Bad status provided
return reject(err);
}
// Prepare API request
var req = {
method: 'POST',
json: incident,
headers: that.headers,
url: that.url + '/incidents'
};
// Execute request
request(req, function(err, res, body) {
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
CachetAPI.prototype.getComponentById = function(id) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// No component ID provided?
if (!id) {
return reject(new Error('Please provide the component ID to fetch.'));
}
// Prepare API request
var req = {
method: 'GET',
json: true,
headers: that.headers,
url: that.url + '/components/' + id + '?t=' + new Date().getTime()
};
// Execute request
request(req, function(err, res, body) {
// Extract data object from body if it exists
body = (body && body.data) ? body.data : body;
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
CachetAPI.prototype.updateIncident = function(incident) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// No incident provided?
if (!incident) {
return reject(new Error('Please provide the incident to report.'));
}
// Incident must be an object
if (typeof incident !== 'object') {
return reject(new Error('Please provide the incident as an object.'));
}
// Check for required parameters
if (!incident.id || !incident.status || !incident.message) {
return reject(new Error('Please provide the incident id, message and status.'));
}
// Prepare API request
var req = {
method: 'PUT',
json: true,
headers: that.headers,
body: incident,
url: that.url + '/incidents/' + incident.id
};
// Execute request
request(req, function(err, res, body) {
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
CachetAPI.prototype.updateComponent = function(component) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// No component provided?
if (!component) {
return reject(new Error('Please provide the component to report.'));
}
// Component must be an object
if (typeof component !== 'object') {
return reject(new Error('Please provide the component as an object.'));
}
// Check for required parameters
if (!component.id || !component.status) {
return reject(new Error('Please provide the incident id and status.'));
}
// Prepare API request
var req = {
method: 'PUT',
json: true,
headers: that.headers,
body: component,
url: that.url + '/components/' + component.id
};
// Execute request
request(req, function(err, res, body) {
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
CachetAPI.prototype.getIncidents = function(filter) {
// Dirty hack
var that = this;
// Return a promise
return new Promise(function(resolve, reject) {
// Prepare API request
var req = {
method: 'GET',
json: true,
headers: that.headers,
url: that.url + '/incidents' + parseFilter(filter)
};
// Execute request
request(req, function(err, res, body) {
// Extract data object from body if it exists
body = (body && body.data) ? body.data : body;
// Handle the response accordingly
handleResponse(err, res, body, reject, resolve);
});
});
};
function handleResponse(err, res, body, reject, resolve) {
// Handle errors by rejecting the promise
if (err) {
return reject(err);
}
// Error(s) returned?
if (body.errors) {
// Stringify and reject promise
return reject(new Error(util.inspect(body.errors)));
}
// Require 200 OK for success
if (res.statusCode != 200) {
// Throw generic error
return reject(new Error('An invalid response code was returned from the API: ' + res.statusCode));
}
// Resolve promise with request body
resolve(body);
}
// Expose the class object
module.exports = CachetAPI;