-
Notifications
You must be signed in to change notification settings - Fork 0
/
sis_api.go
228 lines (180 loc) · 5.34 KB
/
sis_api.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
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
// API for querying and updating a moodle server
//
// api := moodle.NewSisApi("https://moodle.example.com/moodle/", "user@example.com", "a0092ba9a9f5b45cdd2f01d049595bfe91")
//
// // Search moodle courses
// students, _ := api.GetStudents("kim smith")
// for _, i := range students {
// fmt.Printf("%s\n", i.GetDisplayName())
// }
//
//
package sisapi
import (
"encoding/json"
"errors"
"fmt"
"net/url"
)
// API Documentation
// https://docs.moodle.org/dev/Web_service_API_functions
type SisApi struct {
base string
email string
password string
token string
log SisLogger
fetch LookupUrl
}
func NewSisApi(base, email, password string) *SisApi {
return &SisApi{
base: base,
email: email,
password: password,
token: "",
log: &NilSisLogger{},
fetch: &DefaultLookupUrl{},
}
}
type Person struct {
Uuid string `json:",omitempty"`
StudentNumber string `json:",omitempty"`
FirstName string `json:",omitempty"`
PreferredFirstName string `json:",omitempty"`
MiddleName string `json:",omitempty"`
LastName string `json:",omitempty"`
Sex string `json:",omitempty"`
Title string `json:",omitempty"`
Error string `json:",omitempty"`
ErrorDetails string `json:",omitempty"`
}
type SisLogger interface {
Debug(message string, items ...interface{}) error
}
type NilSisLogger struct {
}
func (ml *NilSisLogger) Debug(message string, items ...interface{}) error {
return nil
}
func (m *SisApi) SetLogger(l SisLogger) {
m.log = l
}
func (m *SisApi) Authenticate() error {
url := fmt.Sprintf("%sapi/authenticate?email=%s&password=%s", m.base, url.QueryEscape(m.email), url.QueryEscape(m.password))
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return err
}
type AuthResponse struct {
Token string
Error string
}
var result AuthResponse
if err := json.Unmarshal([]byte(body), &result); err != nil {
return errors.New("Server returned unexpected response. " + err.Error())
}
if result.Error != "" {
return errors.New(result.Error)
}
m.token = result.Token
return nil
}
func (m *SisApi) GetPerson(uuid string) (*Person, error) {
url := fmt.Sprintf("%sapi/person?token=%s&uuid=%s", m.base, m.token, url.QueryEscape(uuid))
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return nil, err
}
var result Person
if err := json.Unmarshal([]byte(body), &result); err != nil {
return nil, errors.New("Server returned unexpected response. " + err.Error())
}
if result.Error != "" {
return nil, errors.New(result.Error)
}
return &result, nil
}
func (m *SisApi) SearchPeople(q string) ([]Person, error) {
url := fmt.Sprintf("%sapi/student.search?token=%s&q=%s", m.base, m.token, url.QueryEscape(q))
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return nil, err
}
var results []Person
if err := json.Unmarshal([]byte(body), &results); err != nil {
return nil, errors.New("Server returned unexpected response. " + err.Error())
}
return results[:], nil
}
// Get moodle account matching by email address.
func (m *SisApi) GetPersonByEmail(email string) (*Person, error) {
url := fmt.Sprintf("%sapi/person?token=%s&email=%s", m.base, m.token, url.QueryEscape(email))
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return nil, err
}
var result Person
if err := json.Unmarshal([]byte(body), &result); err != nil {
return nil, errors.New("Server returned unexpected response. " + err.Error())
}
if result.Error != "" {
return nil, errors.New(result.Error)
}
if result.Uuid == "" {
return nil, nil
}
return &result, nil
}
func (m *SisApi) RemovePersonFromGroup(personUuid, group string, intakeYear int, intakeSemester string) error {
url := fmt.Sprintf("%sapi/group.remove?token=%s&person=%s&group=%s&year=%d&semester=%s", m.base, m.token, personUuid, url.QueryEscape(group), intakeYear, intakeSemester)
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return err
}
type Response struct {
Success string
Error string
}
var result Response
if err := json.Unmarshal([]byte(body), &result); err != nil {
return errors.New("Server returned unexpected response. " + err.Error())
}
if result.Success != "" {
return nil
}
return errors.New(result.Error)
}
func (m *SisApi) AddPersonToGroup(personUuid string, group string, intakeYear int, intakeSemester string) error {
url := fmt.Sprintf("%sapi/group.add?token=%s&person=%s&group=%s&year=%d&semester=%s", m.base, m.token, personUuid, url.QueryEscape(group), intakeYear, intakeSemester)
m.log.Debug("Fetch: %s", url)
body, _, _, err := m.fetch.GetUrl(url)
if err != nil {
return err
}
type Response struct {
Success string
Error string
}
var result Response
if err := json.Unmarshal([]byte(body), &result); err != nil {
return errors.New("Server returned unexpected response. " + err.Error())
}
if result.Success != "" {
return nil
}
return errors.New(result.Error)
}
func (m *SisApi) SetUrlFetcher(fetch LookupUrl) {
m.fetch = fetch
}
type PrintSisLogger struct {
}
func (ml *PrintSisLogger) Debug(message string, items ...interface{}) error {
fmt.Printf(message+"\n", items...)
return nil
}