-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (67 loc) · 2.31 KB
/
app.js
File metadata and controls
76 lines (67 loc) · 2.31 KB
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 request = require('request'),
crypto = require('crypto'),
exceptions = require('./exceptions');
const VERSION_PREFIX = "/v0";
var IrisClient = function(config){
this.config = config;
self = this;
}
//Report an incident
IrisClient.prototype.incident = function(plan, context){
return self.sendReq('incidents', {plan:plan,context:context});
}
// Notification are more, "high key" and the behaviors can be very different.
// Make sure we know what we're doing here
IrisClient.prototype.notify = function(role, target, subject, priority, mode, body, template, context, email_html){
data = {
'role': role,
'target': target,
'subject': subject,
}
if(mode){
data['mode'] = mode;
}
else{
if(!priority){
throw new exceptions.InvalidArgument('Missing both priority and mode arguments, need to at least specify one.');
}
data['priority'] = priority;
}
if (email_html){
data['email_html'] = email_html
}
else{
if(template && context){
data['template'] = template
data['context'] = context
data['context']['iris'] = {}
data['body'] = None
}else{
if(body){
data['body'] = body
}
}
}
return self.sendReq('notifications', data);
}
//attaches the headers and sends the request to each call.
IrisClient.prototype.sendReq= function(path, body){
var path = `${VERSION_PREFIX}/${path}`;
var body = JSON.stringify(body);
var words =`${Math.floor(Math.round((new Date()).getTime() / 1000) / 5) - 1} POST ${path} ${body}`;
var hmac = `hmac ${self.config.app}:${crypto.createHmac('sha512', self.config.key).update(words).digest('base64').replace(/\+/g, '-').replace(/\//g, '_')}`;
var headers = {'AUTHORIZATION': hmac};
r = request.post(
{
uri :`${self.config.url}${path}`,
headers : headers,
body : body
},function (error, response, body) {
if (error || response.statusCode>201) {
throw new exceptions.BadResponse('Server returned an error: '+ error);
}
console.log(body);
return body;
});
}
module.exports = IrisClient;