-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbakaInteract.js
78 lines (67 loc) · 2.81 KB
/
bakaInteract.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
//pro dokumentaci knihovny viz bakaInteract.md na githubu
// vrátí access token k zadanému účtu
async function getAccessToken(schoolUrl, inputUser, inputPassword, refreshToken = "") {
var token = ""
//pokusí se sehnat uložený refresh token
refreshToken = localStorage.getItem("refreshToken")
//nastaví data na odeslání podle toho, jestli sehnal refresh token
if (refreshToken != "" && refreshToken != null && refreshToken != "undefined") {
var bodyToSend =`client_id=ANDR&grant_type=refresh_token&refresh_token=${refreshToken}`
} else {
var bodyToSend = `client_id=ANDR&grant_type=password&username=${inputUser}&password=${inputPassword}`
}
//odešle žádost o acces token
var response = await fetch(`${schoolUrl}/api/login`, {
method: "POST",
headers: {"Content-Type": "application/x-www-form-urlencoded"},
body: bodyToSend
})
json = await response.json()
//error handling
//error špatného hesla, často způsoben zavoláním funkce pouze s url školy a neplatným refresh tokenem
if (json.error_description == 'Špatný login nebo heslo') {throw new Error("Špatný login nebo heslo")}
//pokud selže s tím, že za to mohl neplatný čí vypršený refresh token, pošle žádost znovu za pomocí hesel (pokud jsou dostupná)
else if (json.error_description == 'The specified refresh token is invalid.') {
response = await fetch(`${schoolUrl}/api/login`, {
method: "POST",
headers: {"Content-Type": "application/x-www-form-urlencoded"},
body: `client_id=ANDR&grant_type=password&username=${inputUser}&password=${inputPassword}`
})
json = await response.json()
}
token = json.access_token
localStorage.setItem("refreshToken",await json.refresh_token)
return token
}
// vrátí rozvrh v json formátu
async function retrieveTimetable(schoolUrl, accessToken, weeksIntoFuture=0) {
var dateInIsoFormat = new Date(+Date.now() + (604800000 * weeksIntoFuture)).toISOString().split('T')[0]
response = await fetch(`${schoolUrl}/api/3/timetable/actual?date=${dateInIsoFormat}`, {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Bearer ${accessToken}`
}
})
return await response.json()
}
async function retrieveGrades(schoolUrl, accessToken) {
response = await fetch(`${schoolUrl}/api/3/marks`, {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Bearer ${accessToken}`
}
})
return await response.json()
}
async function retrieveAbsences(schoolUrl, accessToken) {
response = await fetch(`${schoolUrl}/api/3/absence/student`, {
method: "GET",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Bearer ${accessToken}`
}
})
return await response.json()
}