-
Notifications
You must be signed in to change notification settings - Fork 0
/
liferay-service.js
64 lines (59 loc) · 1.57 KB
/
liferay-service.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
'use strict'
var liferay = require('./config.json').liferay;
var http = liferay.hostname.startsWith('https')
? require('https') : require('http');
function LiferayService(servicePath) {
this._options = {
hostname: liferay.hostname,
port: liferay.port,
auth: liferay.auth,
path: servicePath,
method: 'POST',
};
};
LiferayService.prototype._liferayRequest = function(jsonrpc) {
return new Promise((resolve, reject) => {
var req = http.request(this._options, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('HTTP : ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => body.push(chunk));
response.on('end', () => resolve(body.join('')));
});
req.on('error', (err) => {
reject(err);
});
req.write(JSON.stringify(jsonrpc));
//console.log(JSON.stringify(jsonrpc));
req.end();
});
};
LiferayService.prototype.add = function(entity) {
var jsonrpc = {
"method": this.addMethod,
"params": this.addParams(entity),
"id": Date.now(),
"jsonrpc": "2.0"
};
return this._liferayRequest(jsonrpc);
};
LiferayService.prototype.update = function(entity) {
var jsonrpc = {
"method": this.updateMethod,
"params": this.updateParams(entity),
"id": Date.now(),
"jsonrpc": "2.0"
};
return this._liferayRequest(jsonrpc);
};
LiferayService.prototype.delete = function(entity) {
var jsonrpc = {
"method": this.deleteMethod,
"params": this.deleteParams(entity),
"id": Date.now(),
"jsonrpc": "2.0"
};
return this._liferayRequest(jsonrpc);
};
module.exports = LiferayService;