-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
64 lines (55 loc) · 1.29 KB
/
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
60
61
62
63
64
package http
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
)
// Response represents the response from an HTTP request.
type Response struct {
err error
resp *http.Response
body []byte
}
// Err returns the response err.
func (r *Response) Err() error {
return r.err
}
// Response returns the response.
func (r *Response) Response() *http.Response {
return r.resp
}
// Val returns the response body as []byte.
func (r *Response) Val() []byte {
if r.err == nil && r.body == nil {
r.body, r.err = ioutil.ReadAll(r.resp.Body)
defer r.resp.Body.Close()
}
return r.body
}
// String returns the response body as string.
func (r *Response) String() string {
return string(r.Val())
}
// Result returns the response val and err.
func (r *Response) Result() ([]byte, error) {
return r.Val(), r.err
}
// JsonUnmarshal parses the JSON-encoded data and stores the result
// in the value pointed to by v.
func (r *Response) JsonUnmarshal(v interface{}) error {
data := r.Val()
if r.err != nil {
return r.err
}
return json.Unmarshal(data, v)
}
// XmlUnmarshal parses the XML-encoded data and stores the result in
// the value pointed to by v.
func (r *Response) XmlUnmarshal(v interface{}) error {
data := r.Val()
if r.err != nil {
return r.err
}
return xml.Unmarshal(data, v)
}