-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
263 lines (232 loc) · 9.69 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
'use strict'
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
const unifyOptions = require('./libs/createClient');
const requestOptions = require('./libs/requestOptions');
const socket = require('./libs/socketConnection');
const https = require('https');
const path = require('path');
const fs = require('fs');
const helpers = require('./libs/helpers')
const querystring = require('querystring');
function QlikConnection (options) {
this.options = options
this.options.certPath = path.join('C:', 'ProgramData', 'Qlik', 'Sense', 'Repository', 'Exported Certificates', '.Local Certificates');
}
QlikConnection.prototype.getAbout = async function () {
let reqOptions = requestOptions.getOptions('about', this.options)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getApps = async function () {
let reqOptions = requestOptions.getOptions('apps', this.options)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getEngine = async function () {
let reqOptions = requestOptions.getOptions('engine', this.options)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getHealthCheck = async function () {
let reqOptions = requestOptions.getOptions('healthcheck', this.options)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.doReloadTask = async function (taskName) {
if (!taskName) throw new Error('No taskName or file Reference Id declared')
let reqOptions = requestOptions.getOptions('doReloadTask', this.options)
reqOptions.path = reqOptions.path.replace('##path##', encodeURIComponent(taskName))
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getReloadTaskToken = async function (id, fileId) {
if(!id || !fileId) throw new Error('No taskId or file Reference Id declared')
let path = `ReloadTask/${id}/scriptlog?fileReferenceId=${fileId}`
let reqOptions = requestOptions.getOptions('reloadTask', this.options)
reqOptions.path = reqOptions.path.replace('##path##', path)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getTaskLog = async function (taskId) {
if(!taskId) throw new Error('No taskId or file Reference Id declared')
let task = await this.getQsr(`ReloadTask/${taskId}`)
let token = await this.getReloadTaskToken(task.id, task.operational.lastExecutionResult.fileReferenceID)
let log = await this.getExeutionLog(token.value, task.name)
return log
}
QlikConnection.prototype.getExecutionResult = async function () {
let reqOptions = requestOptions.getOptions('executionresult', this.options)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getQsr = async function (path) {
let reqOptions = requestOptions.getOptions('qrs', this.options)
reqOptions.path = reqOptions.path.replace('##path##', path)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.postQsr = async function (path) {
let reqOptions = requestOptions.getOptions('postQrs', this.options)
reqOptions.path = reqOptions.path.replace('##path##', path)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.getExeutionLog = async function (referenceId, taskName) {
let path = `download/reloadtask/${referenceId}/${querystring.escape(taskName)}.log`
let reqOptions = requestOptions.getOptions('qrs', this.options)
reqOptions.path = reqOptions.path.replace('##path##', path)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.engine = async function (method, params) {
if (!params) params = []
let ws = await socket.creatSocket(this.options)
return new Promise((resolve, reject)=>{
ws.onopen = async function (event) {
let msg = helpers.buildMessage(method, -1, params, 2)
ws.send(msg);
ws.onmessage = function (event) {
let parsedAwnser = JSON.parse(event.data)
if(parsedAwnser.error) resolve(parsedAwnser.error)
if(parsedAwnser.method != 'OnConnected'){
resolve(parsedAwnser.result)
ws.close()
}
}
}
})
}
QlikConnection.prototype.openDoc = async function (docId, method, params) {
if (!params) params = []
if (!method || !docId) throw new Error('No method or docId declared')
let ws = await socket.creatSocket(this.options)
return new Promise((resolve, reject)=>{
ws.onopen = async function (event) {
let msg = helpers.buildMessage('OpenDoc', -1, [docId], 2)
ws.send(msg);
ws.onmessage = function (event) {
let parsedAwnser = JSON.parse(event.data)
if(parsedAwnser.method != 'OnConnected'){
let msg = helpers.buildMessage(method, 1, params, 5)
ws.send(msg);
ws.onmessage = function (event) {
let parsedAwnser = JSON.parse(event.data)
resolve(parsedAwnser)
ws.close()
}
}
}
}
})
}
QlikConnection.prototype.generateSession = async function (userdirectory, userName) {
let reqOptions = requestOptions.getOptions('createSession', this.options)
let bodyOptions = { 'UserDirectory': userdirectory.toString() , 'UserId': userName.toString(), "SessionId": helpers.generateUUID() }
let res = await requestPostDispatcher(reqOptions, bodyOptions)
return res
}
QlikConnection.prototype.uploadApp= async function (name, filePath) {
let reqOptions = requestOptions.getOptions('uploadApp', this.options)
reqOptions.path = reqOptions.path.replace('##path##', `name=${name}&keepdata=true&excludeConnections=false`)
let res = await requestPostApp(reqOptions, filePath)
return res
}
QlikConnection.prototype.replaceApp= async function (id,appid) {
let reqOptions = requestOptions.getOptions('replaceApp', this.options)
reqOptions.path = reqOptions.path.replace('##path##', `${id}/replace?app=${appid}`)
let res = await requestPostDispatcher(reqOptions, {})
return res
}
QlikConnection.prototype.publishApp= async function (id,streamId, name) {
let reqOptions = requestOptions.getOptions('replaceApp', this.options)
reqOptions.path = reqOptions.path.replace('##path##', `${id}/publish?stream=${streamId}&name=${name}`)
let res = await requestPostDispatcher(reqOptions, {})
return res
}
QlikConnection.prototype.deleteApp= async function (id) {
let reqOptions = requestOptions.getOptions('deleteApp', this.options)
reqOptions.path = reqOptions.path.replace('##path##', `${id}`)
let res = await requestPostDispatcher(reqOptions, {})
return res
}
QlikConnection.prototype.getSession = async function (sessionId) {
let reqOptions = requestOptions.getOptions('getSession', this.options)
reqOptions.path = reqOptions.path.replace('##path##', sessionId)
let res = await requestGetDispatcher(reqOptions)
return res
}
QlikConnection.prototype.deleteSession = async function (sessionId) {
let reqOptions = requestOptions.getOptions('deleteSession', this.options)
reqOptions.path = reqOptions.path.replace('##path##', sessionId)
let res = await requestGetDispatcher(reqOptions)
return res
}
async function requestGetDispatcher(reqOptions){
return new Promise(async function(resolve, reject){
https.get(reqOptions, function(res) {
let body = '';
res.on("data", function(chunk) {
body += chunk;
});
res.on('end', function () {
try{
resolve(JSON.parse(body.toString()))
}catch(e){
resolve(body.toString())
}
});
}).on('error', function(e) {
reject(e)
});
})
}
async function requestPostDispatcher(reqOptions, bodyOptions){
reqOptions.headers = {...reqOptions.headers, 'Content-Type': 'application/json'}
return new Promise((resolve, reject) => {
let sessionreq = https.request(reqOptions, async function (sessionres) {
let body = '';
sessionres.on("data", function(chunk) {
body += chunk;
});
sessionres.on('end', function () {
try{
resolve(JSON.parse(body.toString()))
} catch(e){
resolve(body.toString())
}
});
}).on('error', function(e) {
reject(e)
});
let jsonrequest = JSON.stringify(bodyOptions);
sessionreq.write(jsonrequest);
sessionreq.end();
sessionreq.on('error', function (e) {
reject('Error' + e);
});
})
}
async function requestPostApp(reqOptions, bodyOptions){
reqOptions.headers = {...reqOptions.headers, 'Content-Type': 'application/vnd.qlik.sense.app'}
return new Promise((resolve, reject) => {
let sessionreq = https.request(reqOptions, async function (sessionres) {
let body = '';
sessionres.on("data", function(chunk) {
body += chunk;
});
sessionres.on('end', function () {
resolve(JSON.parse(body.toString()))
});
}).on('error', function(e) {
reject(e)
});
sessionreq.write(fs.readFileSync(bodyOptions));
sessionreq.end();
sessionreq.on('error', function (e) {
reject('Error' + e);
});
})
}
exports.createClient = function () {
return new QlikConnection(unifyOptions.apply(null,arguments));
};