-
Notifications
You must be signed in to change notification settings - Fork 2
/
restclient.go
547 lines (478 loc) · 16.5 KB
/
restclient.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package restclient
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/go-playground/validator/v10"
"github.com/google/go-querystring/query"
"github.com/spkg/bom"
)
var validate *validator.Validate
var altMatch = regexp.MustCompile(`eq=([^=\|]+)`)
func init() {
validate = validator.New()
}
// FixupCallback - this is a method that will get called before every request
// so that you can, for instance, manipulate headers for auth purposes, for
// instance.
type FixupCallback func(req *http.Request) error
// ErrorResponseCallback - this allows you to hook into the error response,
// for response codes that are >= 400. If error returned here is nil,
// processing continues. Otherwise, the error returned is bubbled to caller.
type ErrorResponseCallback func(resp *http.Response) error
// Client - admin api struct
type Client struct {
Client *http.Client
rawValidatorErrors bool
// Specifying this allows you to modify headers, add
// auth tokens or signatures etc before the request is sent.
FixupCallback FixupCallback
// ErrorResponseCallback - allows you to specify custom behavior
// on responses that are >= 400 status code.
ErrorResponseCallback ErrorResponseCallback
// StripBOM - setting this to true gives you the option to strip
// byte order markings from certain responses.
StripBOM bool
// FormEncodedBody - setting this to true uses x-www-form-urlencoded.
// false (default) will do json encoding.
FormEncodedBody bool
// SkipValidate - setting this to true bypasses validator run.
SkipValidate bool
}
// ClientConfig - this configures an Client. This is meant to be easily
// serializable unserializable so that it can be used with yaml / toml
// etc for the purposes of config files.
//
// Specify CACertBundlePath to load a bundle from disk to override the default.
// Specify CACertBundle if you want embed the cacert bundle in PEM format.
// Specify one or the other if you want to override, or neither to use the
// default. If both are specified, CACertBundle is honored.
type ClientConfig struct {
ClientTimeout Duration
CACertBundlePath string
CACertBundle []byte
InsecureSkipVerify bool
// RawValidateErrors - If true, then no attempt to interpret validator errors will be made.
RawValidatorErrors bool
// SkipValidate - if true, this bypasses all input validation for request bodies.
SkipValidate bool
// StripBOM - if true, this strips the byte order markings which will otherwise bork the json decoder.
StripBOM bool
// FixupCallback - this is a method that will get called before every request
// so that you can, for instance, manipulate headers for auth purposes, for
// instance.
FixupCallback FixupCallback
}
// CustomDecoder - If a response struct implements this interface,
// calls the Decode() method instead of json.Unmarshal.
type CustomDecoder interface {
Decode(data io.Reader) error
}
// NewClient - Client factory method. - if transport is nil, build one
// using config data in cfg. This is optional, you can also initialize
// the following way:
//
// cl := &restclient.Client{Client: &http.Client{}}
func NewClient(cfg *ClientConfig, transport http.RoundTripper) (*Client, error) {
if cfg == nil {
cfg = defConfig()
}
c := &Client{
SkipValidate: cfg.SkipValidate,
rawValidatorErrors: cfg.RawValidatorErrors,
StripBOM: cfg.StripBOM,
FixupCallback: cfg.FixupCallback,
}
if transport == nil {
// Lifted from http package DefaultTransort.
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
tlsc := new(tls.Config)
tlsc.InsecureSkipVerify = cfg.InsecureSkipVerify
var (
cacerts []byte
err error
)
if len(cfg.CACertBundle) > 0 {
cacerts = cfg.CACertBundle
} else if cfg.CACertBundlePath != "" {
cacerts, err = ioutil.ReadFile(cfg.CACertBundlePath)
if err != nil {
return nil, fmt.Errorf("Cannot open ca cert bundle %s: %s", cfg.CACertBundlePath, err)
}
}
if len(cacerts) > 0 {
bundle := x509.NewCertPool()
ok := bundle.AppendCertsFromPEM(cacerts)
if !ok {
return nil, fmt.Errorf("Invalid cert bundle")
}
tlsc.RootCAs = bundle
tlsc.BuildNameToCertificate()
}
t.TLSClientConfig = tlsc
transport = t
}
c.Client = &http.Client{
Timeout: time.Duration(cfg.ClientTimeout),
Transport: transport,
}
return c, nil
}
// NewBaseClient - create a new BaseClient instance based off of the baseURL string.
func NewBaseClient(baseURL string, cfg *ClientConfig, transport http.RoundTripper) (*BaseClient, error) {
bURL, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
client, err := NewClient(cfg, transport)
if err != nil {
return nil, err
}
return &BaseClient{
Client: client,
BaseURL: bURL,
}, nil
}
func defConfig() *ClientConfig {
return &ClientConfig{
ClientTimeout: Duration(3 * time.Second),
}
}
// Get - makes an http GET request to baseURL with path appended, and queryStruct optionally
// parsed by go-querystring and validated with go-playground/validator.v9. Upon successful
// request, response is unmarshaled as json into responseBody, unless responseBody implements
// CustomDecoder, in which case Decode() is called.
func (cl *Client) Get(ctx context.Context, baseURL *url.URL, path string, queryStruct interface{}, responseBody interface{}) error {
_, err := cl.Req(ctx, baseURL, "GET", path, queryStruct, nil, responseBody)
return err
}
// Delete - makes an http DELETE request to baseURL with path appended, and queryStruct optionally
// parsed by go-querystring and validated with go-playground/validator.v9. Upon successful
// request, response is unmarshaled as json into responseBody, unless responseBody implements
// CustomDecoder, in which case Decode() is called.
func (cl *Client) Delete(ctx context.Context, baseURL *url.URL, path string, queryStruct interface{}, responseBody interface{}) error {
_, err := cl.Req(ctx, baseURL, "DELETE", path, queryStruct, nil, responseBody)
return err
}
// Post - makes an http POST request to baseURL with path appended, and queryStruct optionally
// parsed by go-querystring and validated with go-playground/validator.v9. requestBody is
// passed to go-playground/validator.v9 and is sent json-encoded as the body. Upon successful
// request, response is unmarshaled as json into responseBody, unless responseBody implements
// CustomDecoder, in which case Decode() is called.
func (cl *Client) Post(ctx context.Context, baseURL *url.URL, path string, queryStruct, requestBody interface{}, responseBody interface{}) error {
_, err := cl.Req(ctx, baseURL, "POST", path, queryStruct, requestBody, responseBody)
return err
}
// Put - makes an http PUT request to baseURL with path appended, and queryStruct optionally
// parsed by go-querystring and validated with go-playground/validator.v9. requestBody is
// passed to go-playground/validator.v9 and is sent json-encoded as the body. Upon successful
// request, response is unmarshaled as json into responseBody, unless responseBody implements
// CustomDecoder, in which case Decode() is called.
func (cl *Client) Put(ctx context.Context, baseURL *url.URL, path string, queryStruct, requestBody interface{}, responseBody interface{}) error {
_, err := cl.Req(ctx, baseURL, "PUT", path, queryStruct, requestBody, responseBody)
return err
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Ptr, reflect.Slice:
return v.IsNil()
default:
panic("Invalid interface type: " + v.Kind().String())
}
}
// Req - like the method-specific versions above, this is the general purpose.
// the *http.Response return value will either be nil or return with the Body
// closed and fully read. This is mainly useful for inspecting headers, status
// code etc.
func (cl *Client) Req(ctx context.Context, baseURL *url.URL, method, path string,
queryStruct, requestBody, responseBody interface{}) (*http.Response, error) {
return cl.ReqWithHeaders(ctx, baseURL, method, path, queryStruct, requestBody, responseBody, nil)
}
// ReqWithHeaders - this is Req allowing you to specify custom headers.
func (cl *Client) ReqWithHeaders(ctx context.Context, baseURL *url.URL, method, path string,
queryStruct, requestBody, responseBody interface{}, headers http.Header) (*http.Response, error) {
finurl := baseURL.String()
if path != "" {
path = strings.TrimLeft(path, "/")
finurl = baseURL.String() + "/" + path
}
if !isNil(queryStruct) {
if !cl.SkipValidate {
err := cl.validate(queryStruct)
if err != nil {
return nil, err
}
}
v, err := query.Values(queryStruct)
if err != nil {
return nil, err
}
qs := v.Encode()
if qs != "" {
if strings.Contains(finurl, "?") {
finurl = finurl + "&" + qs
} else {
finurl = finurl + "?" + qs
}
}
}
var bodyReader io.Reader
var contentLength int64
if !isNil(requestBody) {
if !cl.SkipValidate {
err := cl.validate(requestBody)
if err != nil {
return nil, err
}
}
if cl.FormEncodedBody {
v, err := query.Values(requestBody)
if err != nil {
return nil, err
}
rawBody := v.Encode()
contentLength = int64(len(rawBody))
bodyReader = strings.NewReader(rawBody)
} else {
bjson, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
bodyReader = bytes.NewReader(bjson)
contentLength = int64(len(bjson))
}
}
req, err := http.NewRequest(method, finurl, bodyReader)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
for k, h := range headers {
req.Header[k] = h
}
req.ContentLength = contentLength
if req.Header.Get("Content-Type") == "" {
if cl.FormEncodedBody {
req.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}
} else {
req.Header["Content-Type"] = []string{"application/json"}
}
}
if cl.FixupCallback != nil {
err = cl.FixupCallback(req)
if err != nil {
return nil, err
}
}
resp, err := cl.Client.Do(req)
if err != nil {
return nil, err
}
defer func() {
// Throw away any remainder of the body so pooling works.
io.Copy(ioutil.Discard, resp.Body)
_ = resp.Body.Close()
}()
if resp.StatusCode >= 400 {
if cl.ErrorResponseCallback != nil {
err = cl.ErrorResponseCallback(resp)
if err != nil {
return resp, err
}
} else {
body, _ := ioutil.ReadAll(resp.Body)
rs := &ResponseError{
Status: resp.Status,
StatusCode: resp.StatusCode,
ResponseBody: body,
Header: resp.Header,
}
return resp, rs
}
}
if isNil(responseBody) {
return resp, nil
}
var reader io.Reader = resp.Body
if cl.StripBOM {
reader = bom.NewReader(resp.Body)
}
if cd, ok := responseBody.(CustomDecoder); ok {
return resp, cd.Decode(reader)
}
return resp, json.NewDecoder(reader).Decode(responseBody)
}
// ValidationErrors - this is a thin wrapper around the validator
// ValidationErrors type. This makes a friendlier error message
// that attempts to interpret why validation failed and give
// a user friendly message.
type ValidationErrors struct {
// The original unmolested ValidationErrors from the validator package
OrigVE validator.ValidationErrors
parsedErrStr string
}
// Error - implement the Error interface.
func (ve ValidationErrors) Error() string {
return ve.parsedErrStr
}
// make sense of the validator error types
func (cl *Client) validate(i interface{}) error {
var err error
rbv := reflect.ValueOf(i)
rbvk := rbv.Kind()
if rbvk == reflect.Slice || (rbvk == reflect.Ptr && rbv.Elem().Kind() == reflect.Slice) {
if rbvk == reflect.Ptr {
rbv = rbv.Elem()
}
for i := 0; i < rbv.Len(); i++ {
err = validate.Struct(rbv.Index(i).Interface())
if err != nil {
break
}
}
} else {
err = validate.Struct(i)
}
if err != nil {
if cl.rawValidatorErrors {
return err
}
if verr, ok := err.(validator.ValidationErrors); ok {
var errs []string
for _, ferr := range verr {
if ferr.ActualTag() == "required" {
errs = append(errs,
fmt.Sprintf("Required field %s is missing or empty",
ferr.StructField(),
),
)
} else if matches := altMatch.FindAllStringSubmatch(ferr.ActualTag(), -1); len(matches) > 0 {
valids := make([]string, len(matches))
for i := 0; i < len(matches); i++ {
valids[i] = "\"" + matches[i][1] + "\""
}
errs = append(errs,
fmt.Sprintf("Field '%s' invalid value: '%s', valid values are: %s",
ferr.StructNamespace(),
ferr.Value(), // for now all are string - revise this if other types are needed
strings.Join(valids, ",")),
)
} else {
errs = append(errs, fmt.Sprintf("Field '%s' invalid value: '%#v', validation tag was %s",
ferr.StructNamespace(),
ferr.Value(),
ferr.ActualTag()))
}
}
return ValidationErrors{
OrigVE: verr,
parsedErrStr: fmt.Sprintf("Validation error: %s", strings.Join(errs, " ; ")),
}
}
}
return err
}
// Duration - this allows us to use a text representation of a duration and
// have it parse correctly. The go standard library time.Duration does not
// implement the TextUnmarshaller interface, so we have to do this workaround
// in order for json.Unmarshal or external parsers like toml.Decode to work
// with human friendly input.
type Duration time.Duration
// UnmarshalText - this implements the TextUnmarshaler interface
func (d *Duration) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
dur, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*d = Duration(dur)
return nil
}
// MarshalText - this implements TextMarshaler
func (d Duration) MarshalText() ([]byte, error) {
return []byte(time.Duration(d).String()), nil
}
// BaseClient - convenience wrapper for requests that all go to the same BaseURL.
type BaseClient struct {
Client *Client
BaseURL *url.URL
}
// Get - like Client.Get, except uses the BaseClient.BaseURL instead of needing to
// be passed in.
func (bc *BaseClient) Get(ctx context.Context, path string, queryStruct interface{}, responseBody interface{}) error {
_, err := bc.Client.Req(ctx, bc.BaseURL, "GET", path, queryStruct, nil, responseBody)
return err
}
// Delete - like Client.Delete, except uses BaseClient.BaseURL instead of needing to
// be passed in.
func (bc *BaseClient) Delete(ctx context.Context, path string, queryStruct interface{}, responseBody interface{}) error {
_, err := bc.Client.Req(ctx, bc.BaseURL, "DELETE", path, queryStruct, nil, responseBody)
return err
}
// Post - like Client.Post, except uses BaseClient.BaseURL instead of needing to
// be passed in.
func (bc *BaseClient) Post(ctx context.Context, path string, queryStruct, requestBody interface{}, responseBody interface{}) error {
_, err := bc.Client.Req(ctx, bc.BaseURL, "POST", path, queryStruct, requestBody, responseBody)
return err
}
// Put - like Client.Put, except uses BaseClient.BaseURL instead of needing to
// be passed in.
func (bc *BaseClient) Put(ctx context.Context, path string, queryStruct, requestBody interface{}, responseBody interface{}) error {
_, err := bc.Client.Req(ctx, bc.BaseURL, "PUT", path, queryStruct, requestBody, responseBody)
return err
}
// Req - like Client.Req, except uses BaseClient.BaseURL instead of needing to be
// passed in.
func (bc *BaseClient) Req(ctx context.Context, method, path string, queryStruct,
requestBody interface{}, responseBody interface{}) (*http.Response, error) {
return bc.Client.Req(ctx, bc.BaseURL, method, path, queryStruct, requestBody, responseBody)
}
// ReqWithHeaders - like client.ReqWithHeaders, except uses BaseClient.BaseURL instead of needing
// to be passed in.
func (bc *BaseClient) ReqWithHeaders(ctx context.Context, method, path string, queryStruct,
requestBody interface{}, responseBody interface{}, headers http.Header) (*http.Response, error) {
return bc.Client.ReqWithHeaders(ctx, bc.BaseURL, method, path, queryStruct, requestBody, responseBody, headers)
}
// ResponseError - this is an http response error type. returned on >=400 status code.
type ResponseError struct {
Status string
StatusCode int
ResponseBody []byte
Header http.Header
}
func (rs *ResponseError) Error() string {
return fmt.Sprintf("response returned error status %d: %s with response payload: %s",
rs.StatusCode,
rs.Status,
rs.ResponseBody,
)
}