/
api.js
489 lines (478 loc) · 16.6 KB
/
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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
const axios = require('axios');
const cookieSupport = require('axios-cookiejar-support');
const { CookieJar } = require('tough-cookie');
const jar = new CookieJar();
const client = cookieSupport.wrapper(axios.create({ jar }));
const API_URL = 'https://synergia.librus.pl/gateway/api/2.0';
const MSG_URL = 'https://synergia.librus.pl';
class LibrusAPI {
constructor() {
this.host = API_URL;
this.msg_host = MSG_URL;
this.headers = {};
this.session = client;
}
async activateApiAccess() {
try {
const tokenInfoResponse = await this.session.get(`${this.host}/Auth/TokenInfo`, { timeout: 5000, headers: this.headers });
const identifier = tokenInfoResponse.data.UserIdentifier;
const userInfoResponse = await this.session.get(`${this.host}/Auth/UserInfo/${identifier}`, { timeout: 5000 });
// this.headers = jar.getCookiesSync(`${this.host}/Auth/UserInfo/${identifier}`).join('; ');
return userInfoResponse.status === 200;
} catch (error) {
console.error(error);
return { code: 0, text: null };
}
}
async mkToken(login, pass) {
try {
const authUrl = 'https://api.librus.pl/OAuth/Authorization?client_id=46&response_type=code&scope=mydata';
const final_authUrl = 'https://api.librus.pl/OAuth/Authorization?client_id=46';
const grandUrl = 'https://api.librus.pl/OAuth/Authorization/Grant?client_id=46';
await this.session.get(authUrl); //Use the client instance
const formData = new URLSearchParams();
formData.append('action', 'login');
formData.append('login', login);
formData.append('pass', pass);
await this.session.post(final_authUrl, formData); //Use the client instance
const grantResponse = await this.session.get(grandUrl); //Use the client instance
// this.headers = jar.getCookiesSync(final_authUrl).join('; '); //Use jar directly
const ok = grantResponse.status === 200;
let res = false;
if (ok) {
res = await this.activateApiAccess();
}
return res;
} catch (error) {
return error.response.data;
}
}
/* Grades API */
async getGrades() {
try {
const response = await this.session.get(`${this.host}/Grades`)
return response.data;
} catch (error) {
console.log("[LibrusAPI: Grades] School doesn't support this function");
throw error;
}
}
async getGradesAverages() {
const response = await this.session.get(`${this.host}/Grades/Averages`)
return response.data;
}
async getGradesCategoriesAverages() {
const response = await this.session.get(`${this.host}/Grades/CategoriesAverages`)
return response.data;
}
async getGradesCategories(id) {
const response = await this.session.get(`${this.host}/Grades/Categories/${String(id)}`)
return response.data;
}
async getGradesComments(id) {
const response = await this.session.get(`${this.host}/Grades/Comments${String(id)}`)
return response.data;
}
async getGradesScales() {
const response = await this.session.get(`${this.host}/Grades/Scales`)
return response.data;
}
async getGradesTypes() {
const response = await this.session.get(`${this.host}/Grades/Types`)
return response.data;
}
async getGradesUnpreparednessPerSemesterAndSubject() {
const response = await this.session.get(`${this.host}/Grades/UnpreparednessPerSemesterAndSubject`)
return response.data;
}
/* To-Do TextGrades */
/* Point Grades API */
async getPointGrades() {
try {
const response = await this.session.get(`${this.host}/PointGrades`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGrades] School doesn't support this function")
}
}
async getPointGradesCategories() {
try {
const response = await this.session.get(`${this.host}/PointGrades/Categories`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGradesCategories] School doesn't support this function")
}
}
async getPointGradesDictionaries() {
try {
const response = await this.session.get(`${this.host}/PointGrades/Dictionaries`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGradesDictionaries] School doesn't support this function")
}
}
async getPointGradesAverages() {
try {
const response = await this.session.get(`${this.host}/PointGrades/Averages`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGradesAverages] School doesn't support this function")
}
}
async getPointGradesStudentsAverages() {
try {
const response = await this.session.get(`${this.host}/PointGrades/StudentsAverages`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGradesAverages] School doesn't support this function")
}
}
async getPointGradesCategoriesAverages() {
try {
const response = await this.session.get(`${this.host}/PointGrades/CategoriesAverages`)
return response.data
} catch (error) {
console.log("[LibrusAPI: PointGradesAverages] School doesn't support this function")
}
}
/* Behaviour Grades API */
async getBehaviourGrades() {
const response_Behaviour = await this.session.get(`${this.host}/BehaviourGrades`)
return response_Behaviour.data
}
async getBehaviourGradesTypes() {
try {
const response = await this.session.get(`${this.host}/BehaviourGrades/Types`)
return response.data
} catch (error) {
console.log("[LibrusAPI: BehaviourGradesTypes] School doesn't support this function")
}
}
async getBehaviourGradesPoints() {
try {
const response = await this.session.get(`${this.host}/BehaviourGrades/Points`)
return response.data
} catch (error) {
console.log("[LibrusAPI: BehaviourGradesPoints] School doesn't support this function")
}
}
async getBehaviourGradesPointsCategories() {
try {
const response = await this.session.get(`${this.host}/BehaviourGrades/Points/Categories`)
return response.data
} catch (error) {
console.log("[LibrusAPI: BehaviourGradesPointsCategories] School doesn't support this function")
}
}
async getBehaviourGradesPointsComments() {
try {
const response = await this.session.get(`${this.host}/BehaviourGrades/Points/Comments`)
return response.data
} catch (error) {
console.log("[LibrusAPI: BehaviourGradesPointsCategories] School doesn't support this function")
}
}
async getBehaviourGradesSystemProposal() {
try {
const response = await this.session.get(`${this.host}/BehaviourGrades/SystemProposal`)
return response.data
} catch (error) {
console.log("[LibrusAPI: BehaviourGradesSystemProposal] School doesn't support this function")
}
}
/* Lessons API */
async getLessons(id="") {
const response = await this.session.get(`${this.host}/Lessons/${String(id)}`)
return response.data;
}
async getAttendances() {
const response = await this.session.get(`${this.host}/Attendances`)
return response.data;
}
async getAttendancesTypes() {
const response = await this.session.get(`${this.host}/Attendances/Types`)
return response.data;
}
async getAttendancesLessonsStatistics(id) {
const response = await this.session.get(`${this.host}/Attendances/LessonsStatistics/${id}`)
return response.data;
}
async getAttendancesFilledByTeacher(id = "") {
try {
const response = await this.session.get(`${this.host}/Attendances/FilledByTeacher/${id}`)
return response.data
} catch (error) {
console.log("[LibrusAPI: AttendancesFilledByTeacher] School doesn't support this function")
}
}
async getCalendar(id="") {
try {
const response = await this.session.get(`${this.host}/Calendars/${id}`);
return response.data;
} catch (error) {
console.log("[LibrusAPI: Calendar] School doesn't support this function");
throw error;
}
}
async getCalendarSubstitutions(id="") {
try {
const response = await this.session.get(`${this.host}/Calendars/Substitutions/${id}`);
return response.data;
} catch (error) {
console.log("[LibrusAPI: CalendarSubstitutions] School doesn't support this function");
throw error;
}
}
async getHomeWorks(){
try {
const response = await this.session.get(`${this.host}/HomeWorks`);
return response.data;
} catch (error) {
console.log("[LibrusAPI: HomeWorks] School doesn't support this function");
throw error;
}
}
async getHomeWorksCategories(id=""){
try {
const response = await this.session.get(`${this.host}/HomeWorks/Categories/${id}`);
return response.data;
} catch (error) {
console.log("[LibrusAPI: HomeWorks] School doesn't support this function");
throw error;
}
}
async getTimetables() {
try {
const response = await this.session.get(`${this.host}/Timetables`);
return response.data;
} catch (error) {
console.log("[LibrusAPI: Timetables] School doesn't support this function");
throw error;
}
}
async getTimetablesDate(arg) {
try {
const response = await this.session.get(`${this.host}/Timetables?${arg}`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: TimetablesDate] School doesn't support this function", error);
throw error;
}
}
async getTimetablesNext() {
try {
const response = await this.session.get(`${this.host}/Timetables`);
const nextWeek = response.data["Pages"]["Next"].split("?")[1];
return await this.getTimetablesDate(nextWeek);
} catch (error) {
console.error("[LibrusAPI: TimetablesNext] School doesn't support this function", error);
throw error;
}
}
async getTimetablesPrev() {
try {
const response = await this.session.get(`${this.host}/Timetables`);
const previousWeek = response.data["Pages"]["Prev"].split("?")[1];
return await this.getTimetablesDate(previousWeek);
} catch (error) {
console.error("[LibrusAPI: TimetablesPrev] School doesn't support this function", error);
throw error;
}
}
async getSubstitutions() {
try {
const response = await this.session.get(`${this.host}/Substitutions`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Substitutions] School doesn't support this function", error);
throw error;
}
}
async getSubjects(id="") {
try {
const response = await this.session.get(`${this.host}/Subjects/${String(id)}`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Subjects] School doesn't support this function", error);
throw error;
}
}
async getTeacherFreeDays() {
try {
const response = await this.session.get(`${this.host}/TeacherFreeDays`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: TeacherFreeDays] School doesn't support this function", error);
throw error;
}
}
async getLuckyNumber() {
try {
const response = await this.session.get(`${this.host}/LuckyNumbers`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: LuckyNumber] School doesn't support this function", error);
throw error;
}
}
/* School API */
async getSchool() {
try {
const response = await this.session.get(`${this.host}/Schools`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: School] School doesn't support this function", error);
throw error;
}
}
async getClassrooms() {
try {
const response = await this.session.get(`${this.host}/Classrooms`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Classrooms] School doesn't support this function", error);
throw error;
}
}
async getVirtualClasses() {
try {
const response = await this.session.get(`${this.host}/VirtualClasses`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: VirtualClasses] School doesn't support this function", error);
throw error;
}
}
async getSchoolNotices() {
try {
const response = await this.session.get(`${this.host}/SchoolNotices`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: SchoolNotices] School doesn't support this function", error);
throw error;
}
}
async getRealizationsTypesOfDays() {
try {
const response = await this.session.get(`${this.host}/Realizations/TypesOfDays`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: RealizationsTypesOfDays] School doesn't support this function", error);
throw error;
}
}
async getRealizationsTypesOfClasses() {
try {
const response = await this.session.get(`${this.host}/Realizations/TypesOfClasses`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: RealizationsTypesOfClasses] School doesn't support this function", error);
throw error;
}
}
/* Class API */
async getClassesFreeDays() {
try {
const response = await this.session.get(`${this.host}/ClassFreeDays`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: ClassFreeDays] School doesn't support this function", error);
throw error;
}
}
async getClasses() {
try {
const response = await this.session.get(`${this.host}/Classes`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Classes] School doesn't support this function", error);
throw error;
}
}
async getClassesCrossedOutStudents() {
try {
const response = await this.session.get(`${this.host}/Classes/CrossedOutStudents`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: ClassesCrossedOutStudents] School doesn't support this function", error);
throw error;
}
}
async getHelp() {
try {
const response = await this.session.get(`${this.host}/Help`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Help] School doesn't support this function", error);
throw error;
}
}
async getEndpoints() {
try {
const response = await this.session.get(`${this.host}/Root`);
// return response.data;
console.log(response)
} catch (error) {
console.error("[LibrusAPI: Root] School doesn't support this function", error);
throw error;
}
}
async getWhatsNew() {
try {
const response = await this.session.get(`${this.host}/WhatsNew`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: WhatsNew] School doesn't support this function", error);
throw error;
}
}
async getNotificationCenter() {
try {
const response = await this.session.get(`${this.host}/NotificationCenter`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: NotificationCenter] School doesn't support this function", error);
throw error;
}
}
async getMe() {
try {
const response = await this.session.get(`${this.host}/Me`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Me] School doesn't support this function", error);
throw error;
}
}
async getColors() {
try {
const response = await this.session.get(`${this.host}/Colors`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Colors] School doesn't support this function", error);
throw error;
}
}
async getUsers(id="") {
try {
const response = await this.session.get(`${this.host}/Users/${String(id)}`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: Users] School doesn't support this function", error);
throw error;
}
}
async getAPI(endpoint) {
try {
const response = await this.session.get(`${this.host}/${endpoint}`);
return response.data;
} catch (error) {
console.error("[LibrusAPI: getAPI] Invalid Endpoint: " + endpoint, error);
throw error;
}
}
returnApiURL(){
return API_URL;
}
}
module.exports = { LibrusAPI };