-
Notifications
You must be signed in to change notification settings - Fork 0
/
student.go
58 lines (48 loc) · 1.25 KB
/
student.go
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
package librus_api_go
import (
"encoding/json"
"strconv"
)
// GetUserInfo calls https://api.librus.pl/2.0/Me and returns info from this endpoint
func (l *Librus) GetUserInfo() (*LibrusMe, error) {
res, err := l.GetData("Me")
if err != nil {
return nil, err
}
defer res.Body.Close()
// get user info
userInfo := new(UserInfoResponse)
err = json.NewDecoder(res.Body).Decode(userInfo)
if err != nil {
return nil, err
}
return userInfo.LibrusMe, nil
}
// GetUserRealName returns real first and last name of user
func (l *Librus) GetUserRealName() (string, error) {
userInfo, err := l.GetUserInfo()
if err != nil {
return "", err
}
return userInfo.User.FirstName + " " + userInfo.User.LastName, nil
}
// GetUserClass returns details of the class to which user belongs
func (l *Librus) GetUserClass() (*ClassDetails, error) {
userInfo, err := l.GetUserInfo()
if err != nil {
return nil, err
}
// get class details response
res, err := l.GetData("Classes/" + strconv.Itoa(userInfo.Class.ID))
if err != nil {
return nil, err
}
defer res.Body.Close()
// get class details
classDetails := new(ClassDetailsResponse)
err = json.NewDecoder(res.Body).Decode(classDetails)
if err != nil {
return nil, err
}
return classDetails.ClassDetails, nil
}