-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (88 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
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
var http = require('http');
var authBody = {
"jsonrpc": "2.0",
"id": 1,
"auth": "",
"method": "user.login",
"params": {
"user": "",
"password": ""
}
}
var authBodyJSON = JSON.stringify(authBody);
var request = http.request({
method: 'POST',
hostname: 'example_url',
timeout: 5000,
path: '/api_jsonrpc.php',
headers: {
"Accept": "application/json",
"Content-Type" : "application/json"
},
params: authBody
}, function(response) {
var buffer = "";
response.on('data', (chunk) => {
buffer = buffer.concat(chunk);
});
response.on('end', (chunk) => {
if(typeof chunk === "string") {
buffer = buffer.concat(chunk);
}
requestHostgroupGetObjects(buffer);
});
});
request.write(authBodyJSON);
request.end();
function requestHostgroupGetObjects (stuff) {
var session = new Session(stuff);
session.rpcRequest('hostgroup.getobjects', {}).then((response)=>{
console.log(response);
});
}
class Session {
constructor(userLoginResponseJSON){
var parsed = JSON.parse(userLoginResponseJSON);
this.token = parsed.result;
this.sequence = parsed.id;
}
rpcRequest(method, params) {
return new Promise((resolve, reject) => {
var payload = {
"jsonrpc": "2.0",
"id": ++this.sequence,
"auth": this.token,
"method": method,
"params": params
}
var payloadJSON = JSON.stringify(payload);
var request = http.request({
method: 'POST',
hostname: '',
timeout: 5000,
path: '/api_jsonrpc.php',
headers: {
"Accept": "application/json",
"Content-Type" : "application/json"
},
params: payload
}, function(response) {
var buffer = "";
response.on('data', (chunk) => {
buffer = buffer.concat(chunk);
});
response.on('end', (chunk) => {
if(typeof chunk === "string") {
buffer = buffer.concat(chunk);
}
resolve(buffer);
});
response.on('error', (err) => {
reject(err);
});
});
request.write(payloadJSON);
request.end();
});
}
}