-
Notifications
You must be signed in to change notification settings - Fork 4
/
rpcWrapper.js
176 lines (152 loc) · 4.67 KB
/
rpcWrapper.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* ------------------------------------------------------------------------------
*
* WebUntis API helper
*
* by Christopher Stelzmüller (tuesd4y)
* 09.02.2017 (tuesd4y) :: created
* 12.02.2017 (tuesd4y) :: updated
*
* ---------------------------------------------------------------------------- */
"use strict";
const _ = require("lodash"),
https = require("https"),
extend = require("util")._extend;
let _loggedIn = false;
function httpRequest(params, postData) {
return new Promise(function(resolve, reject) {
let req = https.request(params, function(res) {
// reject on bad status
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
// res.setEncoding("utf8");
// accumulate data
let body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
// resolve on end
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
// reject on request error
req.on('error', function(err) {
// This is not a "Second reject", just a different sort of failure
reject(err);
});
if (postData) {
req.write(postData);
}
// IMPORTANT
req.end();
});
}
let config = {
url: null,
userName: null,
password: null,
servername: null,
};
let info = {};
function generateParams(method, params) {
return {
"id":"ID",
"method":`${method}`,
"jsonrpc":"2.0",
params:params
};
}
const baseUrl = "/WebUntis/jsonrpc.do";
const authMethod = "authenticate";
// let schoolname = $('#school').val(), username = $('#username').val(), password = $('#password').val();
function url(schoolname) {
return `${baseUrl}?school=${_.replace(schoolname, / /g, "+")}`;
}
function setupWithObject(options) {
if(options.servername) config.servername = options.servername;
if(options.schoolName) config.url = url(options.schoolName);
if(options.username) config.userName = options.username;
if(options.password) config.password = options.password;
return connect();
}
function setup(username, password, schoolName, servername) {
config.userName = username;
config.password = password;
config.url = url(schoolName);
config.servername = servername;
return connect();
}
function rpc(method, params, cb) {
return new Promise((resolve, reject) => {
if(_loggedIn || method === authMethod) {
let body = JSON.stringify(generateParams(method, params));
let headers = {
"Content-Type": "application/json",
"Content-Length": `${body.length}`
};
let u = config.url;
if (info.sessionId && info.sessionId !== null) {
headers["Cookie"] = `JSESSIONID=${info.sessionId}`;
u = baseUrl;
}
let concatHostname = config.servername + ".webuntis.com";
let options = {
method: "POST",
hostname: concatHostname,
path: u,
headers: headers,
};
return httpRequest(options, body)
.then(cb)
} else {
return reject("not authenticated");
}
});
}
function connect() {
return new Promise((resolve, reject) => {
rpc(authMethod, {
user: config.userName,
client: "MY CLIENT",
password: config.password
},
(data) => {
try {
let result = data.result;
info = extend(info, result);
if (info.sessionId) {
_loggedIn = true;
console.log("connected!");
resolve(info)
}
else reject("no sessionId returned")
}
catch(error) {
reject(error);
}
});
});
}
function disconnect() {
return new Promise((resolve, reject) => {rpc("logout", {}, data => {
_loggedIn = false;
console.log("disconnected!");
resolve(data)
});
})
}
module.exports = {
info: info,
rpc: rpc,
connect: connect,
setup: setup,
setupWithObject: setupWithObject,
loggedIn: _loggedIn,
logOut: disconnect,
};