/
svue_api.go
169 lines (130 loc) · 4.23 KB
/
svue_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
package govue
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"
)
type SVUEResponse struct {
XMLName xml.Name `xml:"Envelope"`
Result string `xml:"Body>ProcessWebServiceRequestResponse>ProcessWebServiceRequestResult"`
}
type SVUESignInResponse struct {
XMLName xml.Name `xml:"ChildList"`
Students []*Student `xml:"Child"`
}
const (
soapAction = "http://edupoint.com/webservices/ProcessWebServiceRequest"
signInRequestBody = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ProcessWebServiceRequest xmlns="http://edupoint.com/webservices/">
<userID>%s</userID>
<password>%s</password>
<skipLoginLog>0</skipLoginLog>
<parent>0</parent>
<webServiceHandleName>PXPWebServices</webServiceHandleName>
<methodName>ChildList</methodName>
<paramStr/>
</ProcessWebServiceRequest>
</soap:Body>
</soap:Envelope>`
getGradesRequestBody = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ProcessWebServiceRequest xmlns="http://edupoint.com/webservices/">
<userID>%s</userID>
<password>%s</password>
<skipLoginLog>1</skipLoginLog>
<parent>0</parent>
<webServiceHandleName>PXPWebServices</webServiceHandleName>
<methodName>Gradebook</methodName>
%s
</ProcessWebServiceRequest>
</soap:Body>
</soap:Envelope>`
getGradesParamStr = `<paramStr><Parms><ChildIntID>0</ChildIntID></Parms></paramStr>`
getGradesParamStrGradePeriod = `<paramStr><Parms><ChildIntID>0</ChildIntID><ReportPeriod>%d</ReportPeriod></Parms></paramStr>`
)
func SignInStudent(username, password, endpoint string) (*Student, error) {
escapedAuth, err := escapeStringsForXml(username, password)
if err != nil {
return nil, err
}
username = escapedAuth[0]
password = escapedAuth[1]
signInBody := fmt.Sprintf(signInRequestBody, username, password)
sResp, err := callApi(strings.NewReader(signInBody), endpoint)
if err != nil {
return nil, err
}
return decodeStudentSignIn(sResp)
}
func GetStudentGrades(username, password, endpoint string) (*Gradebook, error) {
return GetStudentGradesForGradingPeriod(username, password, endpoint, -1)
}
func GetStudentGradesForGradingPeriod(username, password, endpoint string, gradingPeriodIndex int) (*Gradebook, error) {
var paramStr string
if gradingPeriodIndex < 0 {
paramStr = getGradesParamStr
} else {
paramStr = fmt.Sprintf(getGradesParamStrGradePeriod, gradingPeriodIndex)
}
escapedAuth, err := escapeStringsForXml(username, password)
if err != nil {
return nil, err
}
username = escapedAuth[0]
password = escapedAuth[1]
gradesBody := fmt.Sprintf(getGradesRequestBody, username, password, paramStr)
sResp, err := callApi(strings.NewReader(gradesBody), endpoint)
if err != nil {
return nil, err
}
return decodeStudentGrades(sResp)
}
func callApi(body io.Reader, endpoint string) (*SVUEResponse, error) {
req, err := newSVueRequest(body, endpoint)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return decodeSVUEResponse(buf)
}
func newSVueRequest(body io.Reader, endpoint string) (*http.Request, error) {
req, err := http.NewRequest("POST", endpoint, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "text/xml; charset=utf-8")
req.Header.Set("SOAPAction", soapAction)
return req, nil
}
func escapeStringsForXml(ss ...string) ([]string, error) {
strs := make([]string, 0, len(ss))
for _, s := range ss {
s, err := escapeXmlText(s)
if err != nil {
return nil, err
}
strs = append(strs, s)
}
return strs, nil
}
func escapeXmlText(s string) (string, error) {
buffer := new(bytes.Buffer)
err := xml.EscapeText(buffer, []byte(s))
if err != nil {
return "", err
}
return buffer.String(), nil
}