-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.go
74 lines (63 loc) · 1.73 KB
/
class.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package librus_api_go
import (
"encoding/json"
"strconv"
"sync"
)
// GetClassFreeDaysTypes returns list of all reasons why class had a free day
func (l *Librus) GetClassFreeDaysTypes() ([]*Type, error) {
res, err := l.GetData("ClassFreeDays/Types")
if err != nil {
return nil, err
}
defer res.Body.Close()
typesResponse := new(TypesResponse)
if err := json.NewDecoder(res.Body).Decode(typesResponse); err != nil {
return nil, err
}
return typesResponse.Types, nil
}
// GetClassFreeDayTypeById returns reason why class had a free day
func (l *Librus) GetClassFreeDayTypeById(id int) (*Type, error) {
res, err := l.GetData("ClassFreeDays/Types/" + strconv.Itoa(id))
if err != nil {
return nil, err
}
defer res.Body.Close()
response := new(TypesResponse)
if err := json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Types[0], nil
}
// GetClassFreeDays returns list of all class free days
func (l *Librus) GetClassFreeDays() ([]*ClassFreeDay, error) {
res, err := l.GetData("ClassFreeDays")
if err != nil {
return nil, err
}
defer res.Body.Close()
classFreeDaysResponse := new(ClassFreeDaysResponse)
if err := json.NewDecoder(res.Body).Decode(classFreeDaysResponse); err != nil {
return nil, err
}
// get types
var wg sync.WaitGroup
freeDays := []*ClassFreeDay{}
for _, d := range classFreeDaysResponse.ClassFreeDays {
wg.Add(1)
freeDay := &ClassFreeDay{}
freeDay.ClassFreeDayDefault = d.ClassFreeDayDefault
go func(dayTypeID int) {
defer wg.Done()
freeDayType, err := l.GetClassFreeDayTypeById(dayTypeID)
if err != nil {
return
}
freeDay.Type = freeDayType
freeDays = append(freeDays, freeDay)
}(d.Type.ID)
}
wg.Wait()
return freeDays, nil
}