-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (61 loc) · 1.94 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
'use strict';
var request = require('request'),
xml2js = require('./utils/xml2js'),
log4js = require('log4js');
//TODO: read tge configuraion from a file on a relative config/ dir.
log4js.configure({
appenders: [{
type: 'console'
}, {
type: 'file',
filename: 'logs/soap-cli-simple.log',
category: 'soap-cli-simple'
}]
});
var logger = log4js.getLogger('soap-cli-simple');
function envelope(operation, message, options) {
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns="' + options.namespace + '" ' +
(options.namespaces ? options.namespaces.join(' ') : '') + '>' +
(options.header ? '<env:Header>' + options.header + '</env:Header>' : '') +
'<env:Body>' + xml2js.buildObject(message) + '</env:Body></env:Envelope>';
logger.info('Request');
logger.info(xml);
return xml;
}
function headers(action, length) {
return {
'SOAPAction': action,
'Content-Type': 'text/xml;charset=UTF-8',
'Content-Length': length,
'Accept-Encoding': 'gzip',
'Accept': '*/*'
};
}
module.exports = function(endpoint, operation, action, message, options) {
var xml = envelope(operation, message, options);
return new Promise(
function(resolve, reject) {
request.post({
uri: endpoint,
body: xml,
gzip: true,
headers: headers(action, xml.length),
rejectUnauthorized: options.rejectUnauthorized,
secureProtocol: options.secureProtocol
}, function(error, response, body) {
if (error) {
logger.error('Response');
logger.error(error);
reject(error);
} else {
logger.info('Response');
logger.info(body);
xml2js.parseString(body).then(resolve).catch(reject);
}
});
});
};