-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (136 loc) · 5.56 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
'use strict';
var util = require('util');
var json2xml = require('js2xmlparser');
var xml2js = require('xml2js');
var validator = require('validator');
var request = require('request-promise-native');
// CONSTANTS
var SECURE_SCHEME = "https";
var PLAIN_SCHEME = "http";
var TASK_URI = "%s://%s/ccp/task/feed/%d";
var CALL_VAR_PREFIX = "cv_";
var ECC_VAR_PREFIX = "user_";
var config = null;
function createVariable(name, value) {
return { "name" : name, "value" : value};
}
function createCallVariables(variableArray) {
var callVars = new Array();
for (var i = 1; i <= variableArray.length; i++) {
callVars.push(createVariable(CALL_VAR_PREFIX + i, variableArray[i-1]));
}
return callVars;
}
// TODO - Refactor `createCallVariables` and `createEccVariables` into a single function
// TODO - Move all payload creation code into a separate task_util module?
function createEccVariables(variableArray) {
var eccVars = new Array();
for (var i = 1; i <= variableArray.length; i++) {
eccVars.push(createVariable(ECC_VAR_PREFIX + i, variableArray[i-1]));
}
return eccVars;
}
function createOtherVariables(variableMap) {
var otherVars = new Array();
for (var [name, value] of variableMap) {
otherVars.push(createVariable(name, value));
}
return otherVars;
}
function createVariables(callVariables, eccVariables, otherVariables) {
var allVars = new Array();
allVars = allVars.concat(createCallVariables(callVariables))
.concat(createEccVariables(eccVariables))
.concat(createOtherVariables(otherVariables));
return allVars;
}
function constructTaskPayload(name, title, description, scriptSelector,
callVariables, eccVariables, otherVariables, requeueOnRecovery) {
var taskJson = {};
taskJson.name = name;
taskJson.title = title;
taskJson.description = description;
taskJson.scriptSelector = scriptSelector;
taskJson.requeueOnRecovery = requeueOnRecovery;
taskJson.variables = {};
taskJson.variables.variable = createVariables(callVariables, eccVariables, otherVariables);
return json2xml.parse("Task", taskJson);
}
function validateTaskRefURL(taskRefURL) {
if(!validator.isURL(taskRefURL, {protocols: config.secure === true ? SECURE_SCHEME : PLAIN_SCHEME,
require_tld: false, require_protocol: true, require_host: false,
allow_underscores:true})) {
throw new Error('Invalid input. \'' + taskRefURL + '\' is not a valid task URL');
}
}
function convertTaskStatusResponseToJson(responseXml) {
var status = null;
xml2js.parseString(responseXml, {explicitArray: false, explicitRoot: false}, function(err, result) {
status = result;
});
return status;
}
module.exports = {
init : function(socialMinerHost, taskFeedID, secure=true) {
if (!validator.isFQDN(socialMinerHost, {require_tld: false})) {
throw new Error('Invalid input. \'' + socialMinerHost + '\' is not a valid FQDN');
}
if (!validator.isInt(taskFeedID.toString())) {
throw new Error('Invalid input. \'' + taskFeedID + '\' is not a valid task feed ID');
}
config = new Object();
config.socialMinerHost = socialMinerHost;
config.taskFeedID = taskFeedID;
config.secure = secure;
},
createTaskRequest : function(name, title, description, scriptSelector,
callVariables, eccVariables, otherVariables,
requeueOnRecovery=false) {
if (name == undefined || title == undefined || description == undefined || scriptSelector == undefined) {
throw new Error('One or more mandatory args are undefined or null');
}
var taskPayload = constructTaskPayload(name, title, description, scriptSelector,
callVariables, eccVariables, otherVariables, requeueOnRecovery);
var options = {
uri: util.format(TASK_URI, config.secure === true ? SECURE_SCHEME : PLAIN_SCHEME,
config.socialMinerHost, config.taskFeedID),
method: 'POST',
body: taskPayload,
headers: {
'Content-Type' : 'application/xml'
},
resolveWithFullResponse: true,
json: false,
jar: true,
strictSSL: false
};
return request(options).then(function(response) {
return response.headers['location'];
});
},
getTaskStatus : function(taskRefURL) {
validateTaskRefURL(taskRefURL);
var options = {
uri: taskRefURL,
method: 'GET',
headers: {
'Accept' : 'application/xml'
},
jar: true,
strictSSL: false
};
return request(options).then(function(response) {
return convertTaskStatusResponseToJson(response);
});
},
cancelTaskRequest : function(taskRefURL) {
validateTaskRefURL(taskRefURL);
var options = {
uri: taskRefURL,
method: 'DELETE',
jar: true,
strictSSL: false
};
return request(options);
}
};