-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
288 lines (248 loc) · 6.86 KB
/
request.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package httpc
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
)
// ErrInvalidEncodeFn is an error that is returned when calling the Request Do and the
// encode function is not set.
var ErrInvalidEncodeFn = errors.New("no encode fn provided for body")
// ResponseErrorFn is a response error function that can be used to provide
// behavior when a response fails to "Do".
type ResponseErrorFn func(error) error
type kvPair struct {
key string
value string
}
// Request is built up to create an http request.
type Request struct {
Method, Addr string
doer Doer
body interface{}
headers []kvPair
params []kvPair
authFn AuthFn
encodeFn EncodeFn
decodeFn DecodeFn
onErrorFn DecodeFn
responseErrFn ResponseErrorFn
notFoundFns []StatusFn
existsFns []StatusFn
retryStatusFns []StatusFn
successFns []StatusFn
backoff BackoffOptFn
}
// Auth sets the authorization for hte request, overriding the authFn set
// by the client.
func (r *Request) Auth(authFn AuthFn) *Request {
r.authFn = authFn
return r
}
// Backoff sets the backoff of the Request.
func (r *Request) Backoff(b BackoffOptFn) *Request {
r.backoff = b
return r
}
// Body sets the body of the Request.
func (r *Request) Body(v interface{}) *Request {
r.body = v
return r
}
// ContentType sets the content type for the outgoing request.
func (r *Request) ContentType(cType string) *Request {
r.headers = append(r.headers, kvPair{key: "Content-Type", value: cType})
return r
}
// Decode sets the decoder func for the Request.
func (r *Request) Decode(fn DecodeFn) *Request {
r.decodeFn = fn
return r
}
// DecodeJSON is a shorthand for decoding JSON response body.
func (r *Request) DecodeJSON(v interface{}) *Request {
return r.Decode(JSONDecode(v))
}
// Exists appends a exists func to the Request.
func (r *Request) Exists(fn StatusFn) *Request {
r.existsFns = append(r.existsFns, fn)
return r
}
// Header adds a header to the request.
func (r *Request) Header(key, value string) *Request {
r.headers = append(r.headers, kvPair{key: key, value: value})
return r
}
// Headers allows a user to set multiple query params at one time. Following
// the same rules as Header. If a key is provided without a value, it will
// not be added to the request. If it is desired, pass in "" to add a header
// with no value.
func (r *Request) Headers(key, value string, pairs ...string) *Request {
hReq := r.Header(key, value)
for index := 0; index < len(pairs)/2; index++ {
i := index * 2
pair := pairs[i : i+2]
if len(pair) != 2 {
return hReq
}
hReq = r.Header(pair[0], pair[1])
}
return hReq
}
// OnError provides a decode hook to decode a responses body. Applied
// when the response's status code does not match the expected.
func (r *Request) OnError(fn DecodeFn) *Request {
r.onErrorFn = fn
return r
}
// NotFound appends a not found func to the Request.
func (r *Request) NotFound(fn StatusFn) *Request {
r.notFoundFns = append(r.notFoundFns, fn)
return r
}
// QueryParam allows a user to set query params on their request. This can be
// called numerous times. Will add keys for each value that is passed in here.
// In the case of duplicate query param values, the last pair that is entered
// will be set and the former will not be available.
func (r *Request) QueryParam(key, value string) *Request {
r.params = append(r.params, kvPair{key: key, value: value})
return r
}
// QueryParams allows a user to set multiple query params at one time. Following
// the same rules as QueryParam. If a key is provided without a value, it will
// not be added to the request. If it is desired, pass in "" to add a query param
// with no value.
func (r *Request) QueryParams(key, value string, pairs ...string) *Request {
paramed := r.QueryParam(key, value)
for index := 0; index < len(pairs)/2; index++ {
i := index * 2
pair := pairs[i : i+2]
if len(pair) != 2 {
return paramed
}
paramed = r.QueryParam(pair[0], pair[1])
}
return paramed
}
// Retry sets the retry policy(s) on the request.
func (r *Request) Retry(fn RetryFn) *Request {
return fn(r)
}
// Success appends a success func to the Request.
func (r *Request) Success(fn StatusFn) *Request {
r.successFns = append(r.successFns, fn)
return r
}
// Do makes the http request and applies the backoff.
func (r *Request) Do(ctx context.Context) error {
return retry(ctx, r.do, r.backoff)
}
func (r *Request) do(ctx context.Context) error {
var body io.Reader
if r.body != nil {
if r.encodeFn == nil {
return ErrInvalidEncodeFn
}
encodedBody, err := r.encodeFn(r.body)
if err != nil {
return NewClientErr(Err(err))
}
body = encodedBody
}
req, err := http.NewRequest(r.Method, r.Addr, body)
if err != nil {
return NewClientErr(Err(err))
}
req = req.WithContext(ctx)
if len(r.headers) > 0 {
for _, pair := range r.headers {
req.Header.Set(pair.key, pair.value)
}
}
if len(r.params) > 0 {
params := req.URL.Query()
for _, kv := range r.params {
params.Add(kv.key, kv.value)
}
req.URL.RawQuery = params.Encode()
}
if r.authFn != nil {
req = r.authFn(req)
}
resp, err := r.doer.Do(req)
if err != nil {
return r.responseErr(resp, err)
return NewClientErr(Err(err), Resp(resp))
}
defer func() {
drain(resp.Body)
}()
status := resp.StatusCode
if !statusMatches(status, r.successFns) {
opts := append([]ErrOptFn{Resp(resp)}, r.statusErrOpts(status)...)
if r.onErrorFn != nil {
var buf bytes.Buffer
tee := io.TeeReader(resp.Body, &buf)
if err := r.onErrorFn(tee); err != nil {
opts = append(opts, Err(err))
}
resp.Body = ioutil.NopCloser(&buf)
}
return NewClientErr(opts...)
}
if r.decodeFn == nil {
return nil
}
if err := r.decodeFn(resp.Body); err != nil {
opts := []ErrOptFn{Err(err), Resp(resp)}
if isRetryErr(err) {
opts = append(opts, Retry())
}
return NewClientErr(opts...)
}
return nil
}
func (r *Request) statusErrOpts(status int) []ErrOptFn {
var opts []ErrOptFn
if statusMatches(status, r.retryStatusFns) {
opts = append(opts, Retry())
}
if statusMatches(status, r.notFoundFns) {
opts = append(opts, NotFound())
}
if statusMatches(status, r.existsFns) {
opts = append(opts, Exists())
}
return opts
}
func (r *Request) responseErr(resp *http.Response, err error) error {
if r.responseErrFn != nil {
err = r.responseErrFn(err)
}
opts := []ErrOptFn{Err(err), Resp(resp)}
if isRetryErr(err) {
opts = append(opts, Retry())
}
return NewClientErr(opts...)
}
// drain reads everything from the ReadCloser and closes it
func drain(r io.ReadCloser) error {
var msgs []string
if _, err := io.Copy(ioutil.Discard, r); err != nil {
msgs = append(msgs, err.Error())
}
if err := r.Close(); err != nil {
msgs = append(msgs, err.Error())
}
return errors.New(strings.Join(msgs, "; "))
}
func isRetryErr(err error) bool {
if err == nil {
return false
}
r, ok := err.(retrier)
return ok && r.Retry()
}