-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
106 lines (90 loc) · 2.06 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
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
package gwc
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"encoding/xml"
)
// Response is thin wrapper around http.Response that provides some
// convenient behavior.
type Response struct {
*http.Response
Error error
}
// BuildResponse creates new instance of response based on provided raw HTTP response.
func BuildResponse(rawResponse *http.Response, err error) *Response {
return &Response{
Response: rawResponse,
Error: err,
}
}
// SaveToFile writes response content to file with provided path.
func (r *Response) SaveToFile(filename string) error {
if r.Error != nil {
return r.Error
}
fd, err := os.Create(filename)
if err != nil {
return err
}
defer fd.Close()
defer r.Body.Close()
_, err = io.Copy(fd, r.Body)
if err != nil && err != io.EOF {
return err
}
return nil
}
// JSON decodes response body to provided structure from JSON format.
func (r *Response) JSON(userStruct interface{}) error {
if r.Error != nil {
return r.Error
}
defer r.Body.Close()
jsonDecoder := json.NewDecoder(r.Body)
err := jsonDecoder.Decode(&userStruct)
if err != nil && err != io.EOF {
return err
}
return nil
}
// XML decodes response body to provided structure from XML format.
func (r *Response) XML(userStruct interface{}) error {
if r.Error != nil {
return r.Error
}
defer r.Body.Close()
xmlDecoder := xml.NewDecoder(r.Body)
err := xmlDecoder.Decode(r.Body)
if err != nil && err != io.EOF {
return err
}
return nil
}
// Bytes returns raw bytes read from response body.
func (r *Response) Bytes() ([]byte, error) {
if r.Error != nil {
return nil, r.Error
}
defer r.Body.Close()
buff := bytes.NewBuffer([]byte{})
// if we got Content-Length set buffer size to it
if r.ContentLength > 0 {
buff.Grow(int(r.ContentLength))
}
_, err := io.Copy(buff, r.Body)
if err != nil && err != io.EOF {
return nil, err
}
return buff.Bytes(), nil
}
// String returns response body in string format.
func (r *Response) String() (string, error) {
bytes, err := r.Bytes()
if err != nil {
return "", err
}
return string(bytes), nil
}