-
Notifications
You must be signed in to change notification settings - Fork 0
/
zermelo-private-api.js
93 lines (82 loc) · 2.78 KB
/
zermelo-private-api.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
const https = require('https');
const fs = require('fs');
let subdomain = null;
let accessToken = null;
let branchOfSchool = null;
exports.setSchool = function(school) {
subdomain = school;
};
exports.setApiToken = function(token) {
accessToken = token;
};
exports.setBranchOfSchool = function(branch) {
branchOfSchool = branch;
};
let getStartTimestamp = function() {
return Math.floor((new Date().getTime()) / 1000);
};
let getEndTimestamp = function() {
return Math.floor((new Date().getTime() + 86400000) / 1000);
};
let getHostName = function() {
return subdomain+".zportal.nl";
};
let getPath = function(endPoint) {
return "/api/v3/"+endPoint+"?access_token="+encodeURIComponent(accessToken);
};
let getApiUrl = function(endPoint) {
return "https://"+getHostName()+getPath(endPoint);
};
exports.getUpcomingAppointments = function() {
return new Promise(function(resolve, reject) {
let req = https.request({
hostname: getHostName(),
port: 443,
path: getPath("appointments") + "&valid=true&start="+getStartTimestamp()+"&end="+getEndTimestamp()+"&branchOfSchool="+branchOfSchool+"&fields=id,appointmentInstance,start,end,startTimeSlot,endTimeSlot,subjects,teachers,locations,students,groups,lastModified,new,cancelled,teacherChanged,groupChanged,locationChanged,timeChanged,changeDescription",
method: 'GET'
}, function(res) {
let response = "";
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
try {
response = JSON.parse(response)["response"];
if (response["status"] == 200) {
fs.writeFile('zcache.json', JSON.stringify(response["data"]), 'utf8', function() {
resolve(response["data"]);
});
}
else {
reject(response["status"] + " " + response["message"] + " " + response["details"]);
}
}
catch(err) {
reject(err);
}
});
})
req.on('error', function(err) {
reject(err);
});
req.end();
});
};
exports.getLastUpcomingAppointments = function() {
return new Promise(function(resolve, reject) {
fs.readFile('zcache.json', 'utf8', function(err, data) {
if (err) {
resolve([]);
}
else {
try {
var json = JSON.parse(data);
resolve(json);
}
catch (error) {
resolve([]);
}
}
});
});
};