-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpc.go
520 lines (430 loc) · 13.8 KB
/
httpc.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
package httpc
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers/legacy"
jsoniter "github.com/json-iterator/go"
"github.com/labstack/echo"
)
var defaultacceptedResponseCodes = []int{
http.StatusOK,
http.StatusCreated,
http.StatusAccepted,
}
var defaultretryErrFn = func(resp *http.Response, err error) bool {
return err != nil
}
// Params is an alias for a map of string key / value pairs
type Params = map[string]string
// Intervals is an alias for a list of durations / time intervals
type Intervals = []time.Duration
// Request represents a generic web request for quick execution, providing access
// to method, URL parameters, headers, the body and an optional 1st class function
// used to parse the result
type Request struct {
method string
uri string
host string
timeout time.Duration
queryParams Params
headers Params
parseFn func(resp *http.Response) error
errorFn func(resp *http.Response) error
bodyEncoder Encoder
body []byte
openAPIValidationFileData []byte
delay time.Duration
retryIntervals Intervals
retryErrFn func(resp *http.Response, err error) bool
acceptedResponseCodes []int
client *http.Client
httpClientFunc func(c *http.Client)
httpRequestFunc func(c *http.Request) error
httpAuthFunc func(c *http.Request)
}
// New instantiates a new http client
func New(method, uri string) *Request {
// Instantiate a new NectIdent service using default options
return NewWithClient(method, uri, nil)
}
// NewWithClient instantiates a new httpc request with custom http client
// This eases testing and client interception
func NewWithClient(method, uri string, hc *http.Client) *Request {
r := &Request{
method: method,
uri: uri,
acceptedResponseCodes: defaultacceptedResponseCodes,
client: hc,
}
if r.client == nil {
r.client = &http.Client{Transport: defaultTransport.Clone()}
}
return r
}
// GetMethod returns the method of the request
func (r *Request) GetMethod() string {
return r.method
}
// GetURI returns the URI of the request
func (r *Request) GetURI() string {
return r.uri
}
// GetBody returns the body of the request
func (r *Request) GetBody() []byte {
return r.body
}
// HostName sets an explicit hostname for the client call
func (r *Request) HostName(host string) *Request {
r.host = host
return r
}
// Timeout sets timeout for the client call
func (r *Request) Timeout(timeout time.Duration) *Request {
r.timeout = timeout
return r
}
// RetryBackOff sets back-off intervals and attempts the call multiple times
func (r *Request) RetryBackOff(intervals Intervals) *Request {
r.retryIntervals = intervals
return r
}
// RetryBackOffErrFn sets an assessment function to decide wether an error
// or status code is deemed as a reason to retry the call
func (r *Request) RetryBackOffErrFn(fn func(resp *http.Response, err error) bool) *Request {
r.retryErrFn = fn
return r
}
// SkipCertificateVerification will accept any SSL certificate
func (r *Request) SkipCertificateVerification() *Request {
r.client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = true
return r
}
// ClientCertificates sets client certificates from memory
func (r *Request) ClientCertificates(clientCert, clientKey, caCert []byte) (*Request, error) {
tlsConfig, err := setupClientCertificateFromBytes(clientCert, clientKey, caCert, r.client.Transport.(*http.Transport).TLSClientConfig)
if err != nil {
return r, err
}
r.client.Transport.(*http.Transport).TLSClientConfig = tlsConfig
return r, nil
}
// ClientCertificatesFromFiles sets client certificates from files
func (r *Request) ClientCertificatesFromFiles(certFile, keyFile, caFile string) (*Request, error) {
clientCert, clientKey, caCert, err := readClientCertificateFiles(certFile, keyFile, caFile)
if err != nil {
return r, err
}
return r.ClientCertificates(clientCert, clientKey, caCert)
}
// ClientCertificatesFromInstance sets the client certificates from a cert instance
func (r *Request) ClientCertificatesFromInstance(clientCertWithKey tls.Certificate, caChain []*x509.Certificate) (*Request, error) {
tlsConfig, err := setupClientCertificate(clientCertWithKey, caChain, r.client.Transport.(*http.Transport).TLSClientConfig)
if err != nil {
return r, err
}
r.client.Transport.(*http.Transport).TLSClientConfig = tlsConfig
return r, nil
}
// QueryParams sets the query parameters for the client call
func (r *Request) QueryParams(queryParams Params) *Request {
r.queryParams = queryParams
return r
}
// Headers sets the headers for the client call
func (r *Request) Headers(headers Params) *Request {
r.headers = headers
return r
}
// Body sets the body for the client call
func (r *Request) Body(body []byte) *Request {
r.body = body
return r
}
// Encode encodes and sets the body for the client call using an arbitrary encoder
func (r *Request) Encode(encoder Encoder) *Request {
r.bodyEncoder = encoder
return r
}
// EncodeJSON encodes and sets the body for the client call using JSON encoding
func (r *Request) EncodeJSON(v interface{}) *Request {
r.bodyEncoder = JSONEncoder{v}
return r
}
// EncodeYAML encodes and sets the body for the client call using YAML encoding
func (r *Request) EncodeYAML(v interface{}) *Request {
r.bodyEncoder = YAMLEncoder{v}
return r
}
// EncodeXML encodes and sets the body for the client call using XML encoding
func (r *Request) EncodeXML(v interface{}) *Request {
r.bodyEncoder = XMLEncoder{v}
return r
}
// AuthBasic sets parameters to perform basic authentication
func (r *Request) AuthBasic(user, password string) *Request {
r.httpAuthFunc = func(c *http.Request) {
c.SetBasicAuth(user, password)
}
return r
}
// ParseFn sets a generic parsing function for the result of the client call
func (r *Request) ParseFn(parseFn func(resp *http.Response) error) *Request {
r.parseFn = parseFn
return r
}
// ParseJSON parses the result of the client call as JSON
func (r *Request) ParseJSON(v interface{}) *Request {
r.parseFn = ParseJSON(v)
return r
}
// ParseYAML parses the result of the client call as YAML
func (r *Request) ParseYAML(v interface{}) *Request {
r.parseFn = ParseYAML(v)
return r
}
// ParseXML parses the result of the client call as XML
func (r *Request) ParseXML(v interface{}) *Request {
r.parseFn = ParseXML(v)
return r
}
// ErrorFn sets a parsing function for results not handled by ParseFn
func (r *Request) ErrorFn(errorFn func(resp *http.Response) error) *Request {
r.errorFn = errorFn
return r
}
// OpenAPIValidationFileData sets an OpenAPI validation file for the client call
// using a byte slice (containing the raw JSON file data)
func (r *Request) OpenAPIValidationFileData(fileData []byte) *Request {
r.openAPIValidationFileData = fileData
return r
}
// Delay sets an artificial delay for the client call
func (r *Request) Delay(delay time.Duration) *Request {
r.delay = delay
return r
}
// ModifyHTTPClient executes any function / allows setting parameters of the
// underlying HTTP client before the actual request is made
func (r *Request) ModifyHTTPClient(fn func(c *http.Client)) *Request {
r.httpClientFunc = fn
return r
}
// ModifyRequest allows the caller to call any methods or other functions on the
// http.Request prior to execution of the call
func (r *Request) ModifyRequest(fn func(req *http.Request) error) *Request {
r.httpRequestFunc = fn
return r
}
// Transport forces a specific transport for the HTTP client (e.g. http.DefaultTransport
// in order to support standard gock flows)
func (r *Request) Transport(transport http.RoundTripper) *Request {
r.client.Transport = transport
return r
}
// AcceptedResponseCodes defines a set of accepted HTTP response codes for the
// client call
func (r *Request) AcceptedResponseCodes(acceptedResponseCodes []int) *Request {
r.acceptedResponseCodes = acceptedResponseCodes
return r
}
// Run executes a request
func (r *Request) Run() error {
return r.RunWithContext(context.Background())
}
// RunWithContext executes a request using a specific context
func (r *Request) RunWithContext(ctx context.Context) error {
// Initialize new http.Request
req, err := http.NewRequestWithContext(ctx, r.method, r.uri, nil)
if err != nil {
return fmt.Errorf("error creating request: %s", err)
}
// Notify the server that the connection should be closed after completion of
// the request
req.Close = false
// If requested, set authentication
if r.httpAuthFunc != nil {
r.httpAuthFunc(req)
}
// If requested, parse the requst body using the specified encoder
if r.bodyEncoder != nil {
if len(r.body) > 0 {
return fmt.Errorf("cannot use both body encoding and raw body content")
}
r.body, err = r.bodyEncoder.Encode()
if err != nil {
return fmt.Errorf("error encoding body: %s", err)
}
req.Header.Set("Content-Type", r.bodyEncoder.ContentType())
}
// If a body was provided, assign it to the request
r.setBody(req)
// If URL parameters were provided, assign them to the request
if r.queryParams != nil {
q := req.URL.Query()
for key, val := range r.queryParams {
q.Set(key, val)
}
req.URL.RawQuery = q.Encode()
}
// If headers were provided, assign them to the request
if r.headers != nil {
for key, val := range r.headers {
req.Header.Set(key, val)
}
}
if r.httpClientFunc != nil {
r.httpClientFunc(r.client)
}
// If an explicit host override was provided it, set it
if r.host != "" {
req.Host = r.host
}
// Perform validation agaions OpenAPI specification, if requested
var requestValidationInput *openapi3filter.RequestValidationInput
if r.openAPIValidationFileData != nil {
swaggerFileData, err := openapi3.NewLoader().LoadFromData(r.openAPIValidationFileData)
if err != nil {
return err
}
router, err := legacy.NewRouter(swaggerFileData)
if err != nil {
return err
}
route, pathParams, err := router.FindRoute(req)
if err != nil {
return err
}
requestValidationInput = &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
}
if err := openapi3filter.ValidateRequest(ctx, requestValidationInput); err != nil {
return err
}
}
if r.httpRequestFunc != nil {
err := r.httpRequestFunc(req)
if err != nil {
return err
}
}
if r.retryErrFn == nil {
r.retryErrFn = defaultretryErrFn
}
// Perform the actual request
var resp *http.Response
if r.timeout > 0 {
timeoutCtx, timeoutCancel := context.WithTimeout(req.Context(), r.timeout)
defer timeoutCancel()
req = req.WithContext(timeoutCtx)
}
resp, err = r.client.Do(req)
for i := 0; r.retryErrFn(resp, err) && i < len(r.retryIntervals); i++ {
time.Sleep(r.retryIntervals[i])
r.setBody(req)
resp, err = r.client.Do(req)
}
if r.retryErrFn(resp, err) {
return err
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()
// Perform validation agaions OpenAPI specification, if requested
if r.openAPIValidationFileData != nil {
responseValidationInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: requestValidationInput,
Status: resp.StatusCode,
Header: resp.Header,
Body: resp.Body,
}
if resp.Body != nil {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(data))
responseValidationInput.Body = ioutil.NopCloser(bytes.NewBuffer(data))
}
// Validate response
ctx := context.TODO()
if err := openapi3filter.ValidateResponse(ctx, responseValidationInput); err != nil {
return err
}
}
// Check if the query was successful
if len(r.acceptedResponseCodes) == 0 {
return fmt.Errorf("no accepted HTTP response codes set, considering request to be failed (Got %d)", resp.StatusCode)
}
if !isAnyOf(resp.StatusCode, r.acceptedResponseCodes) {
if r.errorFn != nil {
return r.errorFn(resp)
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, resp.Body); err != nil {
return fmt.Errorf("failed to load body into buffer for error handling: %s", err)
}
// Attempt to decode a generic JSON error from the response body
var extraErr echo.HTTPError
if err := jsoniter.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&extraErr); err == nil {
return fmt.Errorf("%s [%.512s]", resp.Status, fmt.Sprintf("code=%d, message=%v", extraErr.Code, extraErr.Message))
}
// Attempt to decode the response body directly
return fmt.Errorf("%s [body=%.512s]", resp.Status, buf.String())
}
// If a parsing function was provided, execute it
if r.parseFn != nil {
return r.parseFn(resp)
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
func (r *Request) setBody(req *http.Request) {
if len(r.body) > 0 {
// If a delay was requested, assign a delayed reader
if r.delay > 0 {
req.Body = ioutil.NopCloser(newDelayedReader(bytes.NewBuffer(r.body), r.delay))
} else {
req.Body = ioutil.NopCloser(bytes.NewBuffer(r.body))
}
// Pass content length to enforce non-chunked http request.
// Since data is completly in mem it's useless anyways.
// Also needed to mitigate a bug in PHP...
req.ContentLength = int64(len(r.body))
}
}
type delayReader struct {
reader io.Reader
wasDelayed bool
delay time.Duration
}
func newDelayedReader(reader io.Reader, delay time.Duration) *delayReader {
return &delayReader{reader: reader, delay: delay}
}
func (a *delayReader) Read(p []byte) (int, error) {
if !a.wasDelayed {
time.Sleep(a.delay)
a.wasDelayed = true
}
return a.reader.Read(p)
}
func isAnyOf(val int, ref []int) bool {
for _, v := range ref {
if v == val {
return true
}
}
return false
}