-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcxutil.js
37 lines (33 loc) · 1.13 KB
/
vcxutil.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
const https = require('https');
const btoa = require('btoa');
require('dotenv').config();
const vcxutil = {};
// Function: To create basic authentication header using EnableX APP ID and APP KEY
vcxutil.getBasicAuthToken = function getBasicAuthToken() {
return btoa(`${process.env.ENABLEX_APP_ID}:${process.env.ENABLEX_APP_KEY}`);
};
// Function: To connect to Enablex Server API Service
vcxutil.connectServer = function connectServer(options, data, callback) {
console.log(`REQ URI:- ${options.method} ${options.host}:${options.port}${options.path}`);
console.log(`REQ PARAM:- ${data}`);
const request = https.request(options, (res) => {
res.on('data', (chunk) => {
console.log(`RESPONSE DATA:- ${chunk}`);
console.log(JSON.parse(chunk).result);
if (JSON.parse(chunk).result === 0) {
callback('success', JSON.parse(chunk));
} else {
callback('error', JSON.parse(chunk));
}
});
});
request.on('error', (err) => {
console.log(`RESPONSE ERROR:- ${JSON.stringify(err)}`);
});
if (data == null) {
request.end();
} else {
request.end(data);
}
};
module.exports = vcxutil;