-
Notifications
You must be signed in to change notification settings - Fork 6
/
feed_action.js
64 lines (58 loc) · 1.67 KB
/
feed_action.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
const request = require('request')
let _resolve, _reject
function main (msg) {
console.dir(msg);
let promise;
if (msg.lifecycleEvent === 'CREATE') {
promise = new Promise( (resolve, reject) => {
_resolve = resolve
_reject = reject
create(msg)
})
} else if (msg.lifecycleEvent === 'DELETE') {
promise = new Promise( (resolve, reject) => {
_resolve = resolve
_reject = reject
remove(msg)
})
}
return (typeof promise !== 'undefined' ? promise : {done: true})
}
function create (msg) {
if (!msg.hasOwnProperty('url') || !msg.hasOwnProperty('topic')) {
_reject({done:true, error: 'Missing mandatory feed properties, must include url and topic.' })
}
const user_pass = msg.authKey.split(':')
const body = {
trigger: msg.triggerName,
url: msg.url,
topic: msg.topic,
username: user_pass[0],
password: user_pass[1]
}
console.dir(body)
request({
method: "POST",
uri: msg.provider_endpoint,
json: body
}, handle_response)
}
function remove (msg) {
request({
method: "DELETE",
uri: msg.provider_endpoint + msg.triggerName
}, handle_response)
}
function handle_response (err, res, body) {
if (!err && res.statusCode === 200) {
console.log('mqtt feed: http request success.')
_resolve({done: true})
}
if(res) {
console.log('mqtt feed: Error invoking provider: ', res.statusCode, body)
_reject({done: true, error: 'mqtt feed: Error invoking provider: ' + res.statusCode + '\n' + JSON.stringify(body, null, 4)})
} else {
console.log('mqtt feed: Error invoking provider:', err);
_reject ({done: true, error: 'mqtt feed: Error invoking provider: ' + err })
}
}