-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpclient.go
397 lines (318 loc) · 9.26 KB
/
httpclient.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Package httpclient provides the basic infrastructure for doing RESTish http requests
// an application specific client will be generated with the client-gen-go tool.
package httpclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"time"
"golang.org/x/time/rate"
yaml "gopkg.in/yaml.v2"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)
// Constants
const (
ContentTypeText = "text/plain"
ContentTypeJSON = "application/json"
ContentTypeYAML = "application/yaml"
)
// Variables
var (
ErrUnknownContentType = errors.New("unknown media type")
ErrTooManyRequest = errors.New("too many requests")
)
// Client provides ....
type Client struct {
// HTTP client used to communicate with the server
client *http.Client
// rate limiter
limiter *rate.Limiter
// Base URL for API requests.
BaseURL *url.URL
// ContentType is used as Content-Type and Accept in request headers.
ContentType string
// username/password for basic authentication
username string
password string
// custom http header(s)
header http.Header
Marshaler MarshalerFunc
Unmarshaler UnmarshalerFunc
RequestCallback RequestCallbackFunc
ResponseCallback ResponseCallbackFunc
}
// Opt are options for New.
type Opt func(*Client) error
// RequestCallbackFunc for custom pre-processing of requests
// possible use cases: custom error checking, dumping requests for debugging etc.
type RequestCallbackFunc func(*http.Request) *http.Request
// ResponseCallbackFunc for custom post-processing of responses
// possible use cases: custom error checking, dumping responses for debugging etc.
type ResponseCallbackFunc func(*http.Response) (*http.Response, error)
// MarshalerFunc for custom marshaling function
type MarshalerFunc func(io.Writer, interface{}, string) (string, error)
// UnmarshalerFunc for custom unmarshaling function
type UnmarshalerFunc func(io.Reader, interface{}, string) error
// QueryOptions adds query options opt to URL u
// opt has to be a struct tagged according to https://github.com/google/go-querystring
// e.g.:
// type options struct {
// Page int `url:"page,omitempty"`
// PerPage int `url:"per_page,omitempty"`
// Search string `url:"search,omitempty"`
// }
// opt := options{1, 10, "name=testHost"}
// ... will be added to URL u as "?page=1&per_page=10&search=name%3DtestHost"
func QueryOptions(u string, opt interface{}) (string, error) {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
return u, nil
}
origURL, err := url.Parse(u)
if err != nil {
return u, err
}
origValues := origURL.Query()
newValues, err := query.Values(opt)
if err != nil {
return u, err
}
for k, v := range newValues {
origValues[k] = v
}
origURL.RawQuery = origValues.Encode()
return origURL.String(), nil
}
// New returns a new client instance.
func New(baseURL string, opts ...Opt) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
c := &Client{
client: &http.Client{
Timeout: 30 * time.Second,
},
BaseURL: u,
ContentType: ContentTypeJSON,
Marshaler: marshal,
Unmarshaler: unmarshal,
RequestCallback: requestCallback,
ResponseCallback: responseCallback,
}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
// WithPassword is a client option for setting the password for basic authentication.
func WithPassword(p string) Opt {
return func(c *Client) error {
if p == "" {
return errors.New("password cannot be empty")
}
c.password = p
return nil
}
}
// WithUsername is a client option for setting the username.
func WithUsername(u string) Opt {
return func(c *Client) error {
if u == "" {
return errors.New("username cannot be empty")
}
c.username = u
return nil
}
}
// WithHTTPClient is a client option for setting another http client than the default one
func WithHTTPClient(c *http.Client) Opt {
return func(cli *Client) error {
cli.client = c
return nil
}
}
// WithRateLimiter see https://godoc.org/golang.org/x/time/rate
func WithRateLimiter(l *rate.Limiter) Opt {
return func(cli *Client) error {
cli.limiter = l
return nil
}
}
// WithContentType is a client option for setting the content type
func WithContentType(ct string) Opt {
return func(c *Client) error {
if ct == "" {
return errors.New("content type cannot be empty")
}
c.ContentType = ct
return nil
}
}
// WithHeader is a client option for setting custom http header(s) for each request
// Content-Type and Accept headers will be appended by the clients ContentType setting
// Authorization header is overwritten if WithUsername/WithPassowrd was used to setup the client
func WithHeader(header http.Header) Opt {
return func(c *Client) error {
c.header = header
return nil
}
}
// NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the
// BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the
// value pointed to by body will be encoded and included in as the request body.
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := c.BaseURL.ResolveReference(rel)
if c.Marshaler == nil {
panic("Marshaler is nil")
}
buf := new(bytes.Buffer)
contentType, err := c.Marshaler(buf, body, c.ContentType)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if c.header != nil {
req.Header = c.header.Clone()
}
if len(c.username) > 0 && len(c.password) > 0 {
req.SetBasicAuth(c.username, c.password)
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)
if c.RequestCallback == nil {
panic("RequestCallback is nil")
}
return c.RequestCallback(req), nil
}
// marshal is the default marshaler
func marshal(w io.Writer, v interface{}, mediaType string) (string, error) {
if v == nil {
return mediaType, nil
}
switch mediaType {
case ContentTypeJSON:
return ContentTypeJSON, MarshalJSON(w, v, mediaType)
case ContentTypeYAML:
return ContentTypeYAML, MarshalYAML(w, v, mediaType)
case ContentTypeText:
_, err := fmt.Fprint(w, v)
return ContentTypeText, err
default:
return mediaType, errors.Wrap(ErrUnknownContentType, mediaType)
}
}
// MarshalJSON marshal JSON
func MarshalJSON(w io.Writer, v interface{}, mediaType string) error {
return json.NewEncoder(w).Encode(v)
}
// MarshalYAML marshal JSON
func MarshalYAML(w io.Writer, v interface{}, mediaType string) error {
b, err := yaml.Marshal(v)
if err != nil {
return err
}
_, err = w.Write(b)
return err
}
// Do sends an API request and returns the API response. The API response will be decoded and stored in the value
// pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface,
// the raw response will be written to v, without attempting to decode it.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
// rate limit
if c.limiter != nil {
if err := c.limiter.Wait(ctx); err != nil {
return nil, ErrTooManyRequest
}
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return resp, err
}
defer func() {
if rerr := resp.Body.Close(); rerr == nil {
err = rerr
}
}()
if c.ResponseCallback == nil {
panic("ResponseCallback is nil")
}
resp, err = c.ResponseCallback(resp)
if err != nil {
return resp, err
}
if c.Unmarshaler == nil {
panic("Unmarshaler is nil")
}
err = c.Unmarshaler(resp.Body, v, c.ContentType)
return resp, err
}
// unmarshal is the default unmarshaler
func unmarshal(r io.Reader, v interface{}, mediaType string) error {
if v == nil {
return nil
}
// if v is a io.Writer copy the request body to v
if w, ok := v.(io.Writer); ok {
_, err := io.Copy(w, r)
return err
}
switch mediaType {
case ContentTypeJSON:
return UnmarshalJSON(r, v, mediaType)
case ContentTypeYAML:
return UnmarshalYAML(r, v, mediaType)
case ContentTypeText:
if x, ok := v.(*string); ok {
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(r); err != nil {
return errors.Wrap(err, "read into buffer")
}
*x = buf.String()
return nil
}
return errors.New("target type is not string")
default:
return errors.Wrap(ErrUnknownContentType, mediaType)
}
}
// UnmarshalJSON unmarshal JSON
func UnmarshalJSON(r io.Reader, v interface{}, mediaType string) error {
return json.NewDecoder(r).Decode(v)
}
// UnmarshalYAML unmarshal YAML
func UnmarshalYAML(r io.Reader, v interface{}, mediaType string) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
return yaml.Unmarshal(data, v)
}
// requestCallback returns the unmodified request
func requestCallback(r *http.Request) *http.Request {
return r
}
// responseCallback checks the API response for errors, and returns them if present. A response is considered an
// error if it has a status code outside the 200 range. API error responses are expected to have no response body.
func responseCallback(r *http.Response) (*http.Response, error) {
if c := r.StatusCode; c >= 200 && c <= 299 {
return r, nil
}
return r, errors.New(r.Status)
}