-
Notifications
You must be signed in to change notification settings - Fork 0
/
chinachu.js
52 lines (42 loc) · 996 Bytes
/
chinachu.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
const url = require('url');
const http = require('http');
const pathes = {
programs: '/api/schedule/programs.json',
reserves: '/api/reserves.json',
rules: '/api/rules.json'
};
class ChinachuAPI {
constructor(base) {
this.base = base;
}
getJSON(path) {
return new Promise((resolve, reject) => {
const url = new URL(path, this.base);
const req = http.request(url, (res) => {
res.setEncoding('utf8');
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', (res) => {
const json = JSON.parse(body);
resolve(json);
});
});
req.on('error', (e) => {
reject(e);
});
req.end();
});
}
getPrograms() {
return this.getJSON(pathes.programs);
}
getReserves() {
return this.getJSON(pathes.reserves);
}
getRules() {
return this.getJSON(pathes.rules);
}
};
exports.ChinachuAPI = ChinachuAPI;