-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrules.js
87 lines (76 loc) · 2.3 KB
/
rules.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
const rp = require('request-promise');
const prettyjson = require('prettyjson');
const requests = require('./requests');
/**
* List of rules for the location for the given token principal.
* @param {string} authToken - OAuth token.
* @returns {List<Object>}
*/
function rulesList(authToken) {
return rp(requests.request(authToken, `rules`, 'GET'));
}
/**
* Create a rule for the location and token principal.
* @param {string} authToken - OAuth token.
* @param {string} name - Name for the rule.
* @param {Array<Object>} actions - Array of Action.
* @param {string} timeZoneId - Time zone ID for this rule.
*/
function newRule(authToken, name, actions, timeZoneId) {
let body = {};
body.name = name;
body.actions = actions;
if (timeZoneId)
body.timeZoneId = timeZoneId;
return rp(requests.request(authToken, `rules`, 'POST', body));
}
/**
* Get a rule.
* @param {string} authToken - OAuth token.
* @param {string} ruleId - The rule ID.
* @returns {Object}
*/
function ruleData(authToken, ruleId) {
return rp(requests.request(authToken, `rules/${ruleId}`, 'GET'));
}
/**
* Update a rule.
* @param {string} authToken - OAuth token.
* @param {string} ruleId - The rule ID.
* @param {string} name - Name for the rule.
* @param {Array<Object>} actions - Array of Action.
* @param {string} timeZoneId - Time zone ID for this rule.
*/
function updateRule(authToken, ruleId, name, actions, timeZoneId) {
let body = {};
body.name = name;
body.actions = actions;
if (timeZoneId)
body.timeZoneId = timeZoneId;
return rp(requests.request(authToken, `rules/${ruleId}`, 'PUT', body));
}
/**
* Delete a rule.
* @param {string} authToken - OAuth token.
* @param {string} ruleId - The rule ID.
*/
function deleteRule(authToken, ruleId) {
return rp(requests.request(authToken, `rules/${ruleId}`, 'DELETE'));
}
/**
* Trigger Rule execution given a rule ID.
* @param {string} authToken - OAuth token.
* @param {string} ruleId - The rule ID.
*/
function executeRule(authToken, ruleId) {
let body = {};
return rp(requests.request(authToken, `rules/execute/${ruleId}`, 'POST', body));
}
module.exports = {
rulesList: rulesList,
newRule: newRule,
ruleData: ruleData,
updateRule: updateRule,
deleteRule: deleteRule,
executeRule: executeRule
};