forked from kolo/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
60 lines (46 loc) · 977 Bytes
/
response.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
package xmlrpc
import (
"regexp"
)
var (
faultRx = regexp.MustCompile(`<fault>(\s|\S)+</fault>`)
)
type failedResponse struct {
Code interface{} `xmlrpc:"faultCode"`
Error string `xmlrpc:"faultString"`
HttpStatusCode int
}
func (r *failedResponse) err() error {
return &XmlRpcError{
Code: r.Code,
Err: r.Error,
HttpStatusCode: r.HttpStatusCode,
}
}
type Response struct {
data []byte
httpStatusCode int
}
func NewResponse(data []byte, httpStatusCode int) *Response {
return &Response{
data: data,
httpStatusCode: httpStatusCode,
}
}
func (r *Response) Failed() bool {
return faultRx.Match(r.data)
}
func (r *Response) Err() error {
failedResp := new(failedResponse)
if err := unmarshal(r.data, failedResp); err != nil {
return err
}
failedResp.HttpStatusCode = r.httpStatusCode
return failedResp.err()
}
func (r *Response) Unmarshal(v interface{}) error {
if err := unmarshal(r.data, v); err != nil {
return err
}
return nil
}