/
decode_xml.go
172 lines (144 loc) · 3.29 KB
/
decode_xml.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
package govue
import (
"bytes"
"encoding/xml"
"io"
"strings"
)
type SVUERespError struct {
XMLName xml.Name `xml:"RT_ERROR"`
Message string `xml:"ERROR_MESSAGE,attr"`
}
// Codes are:
// 0: StudentVue Server Error
// 1: Unexpected Error
// 2: Invalid Credentials
// 3: Response Decoding Error
type SVUEError struct {
OrigError error
Code int
}
const (
SVueServerError = iota
UnexpectedError
InvalidCredentialsError
DecodingError
)
const (
sVueServerErrorMsg = "An error has occurred with the StudentVue server."
unexpectedErrorMsg = "An unexpected error has occurred."
invalidCredentialsErrorMsg = "The username and/or password is invalid."
decodingErrorMsg = "An internal error has occurred."
)
func (s SVUEError) Error() string {
switch s.Code {
case SVueServerError:
return sVueServerErrorMsg
case UnexpectedError:
return unexpectedErrorMsg
case InvalidCredentialsError:
return invalidCredentialsErrorMsg
case DecodingError:
return decodingErrorMsg
default:
return unexpectedErrorMsg
}
}
func decodeSVUEResponse(body *bytes.Buffer) (*SVUEResponse, error) {
sVueResp := new(SVUEResponse)
if err := xml.Unmarshal(body.Bytes(), sVueResp); err != nil {
return nil, SVUEError{
OrigError: err,
Code: DecodingError,
}
}
return sVueResp, nil
}
func decodeStudentSignIn(sVueResp *SVUEResponse) (*Student, error) {
resp := new(SVUESignInResponse)
d, err := respIsOk(sVueResp, "ChildList")
if err != nil {
return nil, err
}
if err = d.Decode(resp); err != nil {
return nil, SVUEError{
OrigError: err,
Code: DecodingError,
}
}
return resp.Students[0], nil
}
func decodeStudentGrades(sVueResp *SVUEResponse) (*Gradebook, error) {
gb := new(Gradebook)
d, err := respIsOk(sVueResp, "Gradebook")
if err != nil {
return nil, err
}
if err = d.Decode(gb); err != nil {
return nil, SVUEError{
OrigError: err,
Code: DecodingError,
}
}
for _, c := range gb.Courses {
if len(c.Marks) < 1 {
c.CurrentMark = &CourseMark{}
} else {
c.CurrentMark = c.Marks[0]
}
}
return gb, nil
}
func respIsOk(sVueResp *SVUEResponse, expectedElement string) (*xml.Decoder, error) {
d := xml.NewDecoder(strings.NewReader(sVueResp.Result))
TokenLoop:
for {
t, err := d.Token()
if err == io.EOF {
return nil, SVUEError{
OrigError: err,
Code: DecodingError,
}
}
if _t, ok := t.(xml.StartElement); ok {
switch _t.Name.Local {
case expectedElement:
break TokenLoop
case "RT_ERROR":
return nil, decodeRespError(sVueResp)
default:
continue TokenLoop
}
} else {
continue
}
}
return xml.NewDecoder(strings.NewReader(sVueResp.Result)), nil
}
func decodeRespError(sVueResp *SVUEResponse) error {
sErr := new(SVUERespError)
err := xml.Unmarshal([]byte(sVueResp.Result), sErr)
if err != nil {
return SVUEError{
OrigError: err,
Code: DecodingError,
}
}
switch {
case strings.Contains(sErr.Message, "The user name or password is incorrect."):
return SVUEError{
OrigError: nil,
Code: InvalidCredentialsError,
}
case strings.Contains(sErr.Message, "Invalid user id or password"):
return SVUEError{
OrigError: nil,
Code: InvalidCredentialsError,
}
default:
return SVUEError{
OrigError: nil,
Code: UnexpectedError,
}
}
}