-
Notifications
You must be signed in to change notification settings - Fork 4
/
response.go
205 lines (168 loc) · 3.89 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package request
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/go-http-utils/headers"
)
// GetIndex searches value from []interface{} by index
func GetIndex(v interface{}, index int) interface{} {
switch v.(type) {
case []interface{}:
res := v.([]interface{})
if len(res) > index {
return res[index]
}
return nil
case *[]interface{}:
res := v.(*[]interface{})
return GetIndex(*res, index)
default:
return nil
}
}
// GetPath searches value from map[string]interface{} by path
func GetPath(v interface{}, branch ...string) interface{} {
switch v.(type) {
case map[string]interface{}:
res := v.(map[string]interface{})
switch len(branch) {
case 0:
return nil // should return nil when no branch
case 1:
return res[branch[0]]
default:
return GetPath(res[branch[0]], branch[1:]...)
}
case *map[string]interface{}:
res := v.(*map[string]interface{})
return GetPath(*res, branch...)
default:
return nil
}
}
// Response represents the response from a HTTP request.
type Response struct {
*http.Response
raw *bytes.Buffer
content []byte
}
// Raw returns the raw bytes body of the response.
func (r *Response) Raw() ([]byte, error) {
if r.raw != nil {
return r.raw.Bytes(), nil
}
b, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
return nil, err
}
r.raw = bytes.NewBuffer(b)
return b, nil
}
// Content returns the content of the response body, it will handle
// the compression.
func (r *Response) Content() ([]byte, error) {
if r.content != nil {
return r.content, nil
}
rawBytes, err := r.Raw()
if err != nil {
return nil, err
}
var reader io.ReadCloser
switch r.Header.Get(headers.ContentEncoding) {
case "gzip":
if reader, err = gzip.NewReader(bytes.NewBuffer(r.raw.Bytes())); err != nil {
return nil, err
}
case "deflate":
// deflate should be zlib
// http://www.gzip.org/zlib/zlib_faq.html#faq38
if reader, err = zlib.NewReader(bytes.NewBuffer(r.raw.Bytes())); err != nil {
// try RFC 1951 deflate
// http: //www.open-open.com/lib/view/open1460866410410.html
reader = flate.NewReader(bytes.NewBuffer(r.raw.Bytes()))
}
}
if reader == nil {
r.content = rawBytes
return rawBytes, nil
}
defer reader.Close()
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
r.content = b
return b, nil
}
// JSON returns the reponse body with JSON format.
func (r *Response) JSON(v ...interface{}) (interface{}, error) {
b, err := r.Content()
if err != nil {
return nil, err
}
if !strings.HasPrefix(r.Header.Get(headers.ContentType), "application/json") {
err := r.Status
if len(b) > 0 {
err = string(b)
}
return nil, errors.New(err)
}
var res interface{}
if len(v) > 0 {
res = v[0]
} else {
res = new(map[string]interface{})
}
if err = json.Unmarshal(b, res); err != nil {
return nil, err
}
if !r.OK() {
return res, ErrStatusNotOk
}
return res, nil
}
// Text returns the reponse body with text format.
func (r *Response) Text() (string, error) {
b, err := r.Content()
if err != nil {
return "", err
}
if !r.OK() {
return string(b), ErrStatusNotOk
}
return string(b), nil
}
// URL returns url of the final request.
func (r *Response) URL() (*url.URL, error) {
u := r.Request.URL
if r.StatusCode == http.StatusMovedPermanently ||
r.StatusCode == http.StatusFound ||
r.StatusCode == http.StatusSeeOther ||
r.StatusCode == http.StatusTemporaryRedirect {
location, err := r.Location()
if err != nil {
return nil, err
}
u = u.ResolveReference(location)
}
return u, nil
}
// Reason returns the status text of the response status code.
func (r *Response) Reason() string {
return http.StatusText(r.StatusCode)
}
// OK returns whether the reponse status code is less than 400.
func (r *Response) OK() bool {
return r.StatusCode < 400
}