A set of utility functions / classes for simplifying the call to Qlik Sense APIs
Author: Loic Formont
License: MIT Licensed
Copyright: Copyright (C) 2015 Loic Formont
Example
var utils = require("qlik-utils");
- qlik-utils
- static
- .ifnotundef(a, b, [c]) ⇒
*
- .generateXrfkey([size], [chars]) ⇒
string
- .request(options, [params]) ⇒
Promise.<*>
- .getTicket(params, options) ⇒
Promise.<ticket>
- .openSession(ticket, hostUri) ⇒
Promise.<string>
- .addToWhiteList(ip, options) ⇒
Promise.<Object>
- .loop(func, param, retry, retryTimeout, task)
- .basicAuth(users) ⇒
function
- .removeIf(array, callback)
- .setTimeout2Promise(timeout) ⇒
Promise
- .dynamicAppClone(options, templateAppId, scriptMarker, scriptReplace, scriptRegex, publishStreamId, task) ⇒
Promise
- .ifnotundef(a, b, [c]) ⇒
- inner
- ~Task
- ~options :
Object
- ~ticketParams :
Object
- ~ticket :
Object
- static
Two parameters mode If a is undefined, return b else a Three parameters mode If a is undefined, return c else b
Kind: static method of qlik-utils
Param | Type |
---|---|
a | * |
b | * |
[c] | * |
Example
var myHost = utils.ifnotundef(options.host, options.hostname);
Example
var myHost = utils.ifnotundef(options.host, options.hostname, 'localhost');
Generates a random Xrf key of a given size within a set of given chars
Kind: static method of qlik-utils
Param | Type | Default |
---|---|---|
[size] | int |
16 |
[chars] | string |
"abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789" |
Example
var xrf = utils.generateXrfkey(8);
Makes a request on a Qlik Sense API endpoint defined in the options object, posting the params object
Kind: static method of qlik-utils
Returns: Promise.<*>
- a promise resolving to the response to the request
Param | Type | Description |
---|---|---|
options | options |
the options to connect to the API endpoint |
[params] | Object |
the parameters to post to the API endpoint |
Example
utils.request({
restUri: 'https://10.76.224.72:4243/qps/ticket',
pfx: pfx,
passPhrase: ''
}, {
'UserId': 'qlikservice',
'UserDirectory': '2008R2-0',
'Attributes': []
}).then(function(retVal) {
console.log(retVal);
});
Generates a ticket on Qlik Sense QRS Api
Kind: static method of qlik-utils
Returns: Promise.<ticket>
- resolving to the generated ticket
Param | Type | Description |
---|---|---|
params | ticketParams |
the ticket parameters |
options | options |
the options to connect to the ticket API endpoint |
Example
utils.getTicket({
restUri: 'https://10.76.224.72:4243',
pfx: pfx,
passPhrase: ''
}, {
'UserId': 'qlikservice',
'UserDirectory': '2008R2-0',
'Attributes': []
}).then(function(retVal) {
console.log(retVal);
});
Opens a session on the Qlik Sense Hub with the given ticket and returns the session cookie
Kind: static method of qlik-utils
Returns: Promise.<string>
- a promise resolving to the session cookie
Param | Type | Description |
---|---|---|
ticket | ticket |
the generated ticket |
hostUri | string |
the URI of the Qlik Sense host (Hub or preferably QMC) to open a session on |
Example
utils.openSession({
UserDirectory: '2008R2-0',
UserId: 'qlikservice',
Attributes: [],
Ticket: 'QzSPXzBmJKjhucPF',
TargetUri: null
}, 'https://localhost/hub').then(function(retVal) {
console.log(retVal);
});
Adds the given ip address to the websocket whitelist of the given virtual proxy. Be careful: this restarts the proxy. The restart can take 1-2 seconds. All subsequent API calls within this restart will fail miserably with various random & useless error messages.
Kind: static method of qlik-utils
Returns: Promise.<Object>
- a promise resolving to the virtual proxy configuration when successfull
Param | Type | Description |
---|---|---|
ip | string |
the ip to add |
options | options |
the endpoint to add the ip to |
Example
readFile('./client.pfx').then(function(certif) {
return utils.addToWhiteList('10.76.224.72', {
restUri: 'https://10.76.224.72:4242',
pfx: certif,
passPhrase: '',
UserId: 'qlikservice',
UserDirectory: '2008R2-0'
});
}).then(function(ret) {
console.log(ret);
}, function(ret) {
console.log(ret);
});
Loops until func finishes successfully
Kind: static method of qlik-utils
Param | Type | Description |
---|---|---|
func | function |
the function to start (must return a promise) |
param | * |
the parameters to pass to the function (as an array) |
retry | int |
the number of times to retry |
retryTimeout | int |
the delay to wait before restarting the function after a failure |
task | Task |
the task object to update when required |
Example
utils.loop(
utils.addToWhiteList,
[
'10.76.224.72',
{
restUri: 'https://10.76.224.72:4242',
pfx: certif,
passPhrase: '',
UserId: 'qlikservice',
UserDirectory: '2008R2-0'
}
],
30,
2000,
task
);
Simple basic auth middleware for use with Express 4.x.
Kind: static method of qlik-utils
Returns: function
- Express 4 middleware requiring the given credentials
Param | Type | Description |
---|---|---|
users | Object |
list of usernames & passwords |
Example
app.use('/api-requiring-auth', utils.basicAuth([{user: 'username', pass: 'password'}]));
Remove object from an array on condition
Kind: static method of qlik-utils
Param |
---|
array |
callback |
Equivalent to setTimeout but returns a promise instead
Kind: static method of qlik-utils
Param | Description |
---|---|
timeout | the timeout in ms |
Example
utils.setTimeout2Promise(1000).then(function() {
console.log('hi');
});
utils.dynamicAppClone(options, templateAppId, scriptMarker, scriptReplace, scriptRegex, publishStreamId, task) ⇒ Promise
Duplicates a template app, updates its script, reloads it and publishes it
Kind: static method of qlik-utils
Param | Type | Description |
---|---|---|
options | options |
Uri to the Qlik Sense endpoint |
templateAppId | string |
id of the template application |
scriptMarker | string |
marker to be found in the script and replaced during the duplication |
scriptReplace | string |
replace value of the marker above |
scriptRegex | RegExp |
regex to track in the script trace |
publishStreamId | string |
id of the stream to publish into |
task | Task |
task that will trace the cloning progress |
Example
var task = new utils.task();
task.start();
task.bind(function(task) {
console.log(task.val, task.detail);
});
readFile(testQlikSensePfx).then(function(pfx) {
task.running('info', 'certificate loaded...');
return utils.dynamicAppClone({
restUri: 'http://10.20.30.40',
pfx: pfx,
'UserId': 'qlikservice',
'UserDirectory': '2008R2-0'
},
'3bcb8ed0-7ac5-4cd0-8913-37d1255d67c3',
'%Replace me!%',
'Employees.qvd',
/(.*) << (.*) ([0-9,]+) Lines fetched/g,
'aaec8d41-5201-43ab-809f-3063750dfafd',
task
);
});
This class enables you to handle tasks asynchronously.
Kind: inner class of qlik-utils
Creates a new task.
Kind: inner typedef of qlik-utils
Properties
Name | Type | Default | Description |
---|---|---|---|
UserDirectory | string |
"." |
the user directory of the Qlik Sense user that will make the API call |
UserId | string |
"qlikservice" |
the user id of the Qlik Sense user that will make the API call |
session | string |
the cookie of a valid Qlik Sense session | |
restUri | string |
an URI to a valid Qlik Sense endpoint | |
method | string |
"POST" |
the method of the HTTP request (GET/PUT/POST/DELETE) |
pfx | buffer |
the Qlik Sense pfx certificate (not required if a session is set) | |
passphrase | string |
the Qlik Sense certificate's passphrase (not required if a session is set) | |
timeout | int |
the timeout of the HTTP request |
Kind: inner typedef of qlik-utils
Properties
Name | Type | Description |
---|---|---|
UserId | string |
the user id to generate a ticket for |
UserDirectory | string |
the user directory of the user to generate a ticket for |
Attributes | Array.<string> |
the user attributes |
Kind: inner typedef of qlik-utils
Properties
Name | Type | Description |
---|---|---|
UserId | string |
the user id of the generated ticket |
UserDirectory | string |
the user directory of generated ticket |
Attributes | Array.<string> |
the user attributes |
Ticket | string |
the ticket |
TargetUri | string |
the target uri |