forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
534 lines (449 loc) · 14.2 KB
/
client.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
package client
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"golang.org/x/time/rate"
)
// A Client is an HTTP client. Initialize with the New package-level function.
type Client struct {
HTTPClient *http.Client
// BaseURL prepends to all requests.
BaseURL string
// A list of persistent request options.
PersistentRequestOptions []RequestOption
// Optional rate limiter instance initialized by the RateLimit method.
rateLimiter *rate.Limiter
// Optional handlers that are being fired before and after each new request.
requestHandlers []RequestHandler
// store it here for future use.
keepAlive bool
}
// New returns a new Iris HTTP Client.
// Available options:
// - BaseURL
// - Timeout
// - PersistentRequestOptions
// - RateLimit
//
// Look the Client.Do/JSON/... methods to send requests and
// ReadXXX methods to read responses.
//
// The default content type to send and receive data is JSON.
func New(opts ...Option) *Client {
c := &Client{
HTTPClient: &http.Client{},
PersistentRequestOptions: defaultRequestOptions,
requestHandlers: defaultRequestHandlers,
}
for _, opt := range opts {
opt(c)
}
if transport, ok := c.HTTPClient.Transport.(*http.Transport); ok {
c.keepAlive = !transport.DisableKeepAlives
}
return c
}
// RegisterRequestHandler registers one or more request handlers
// to be ran before and after of each new request.
//
// Request handler's BeginRequest method run after each request constructed
// and right before sent to the server.
//
// Request handler's EndRequest method run after response each received
// and right before methods return back to the caller.
//
// Any request handlers MUST be set right after the Client's initialization.
func (c *Client) RegisterRequestHandler(reqHandlers ...RequestHandler) {
reqHandlersToRegister := make([]RequestHandler, 0, len(reqHandlers))
for _, h := range reqHandlers {
if h == nil {
continue
}
reqHandlersToRegister = append(reqHandlersToRegister, h)
}
c.requestHandlers = append(c.requestHandlers, reqHandlersToRegister...)
}
func (c *Client) emitBeginRequest(ctx context.Context, req *http.Request) error {
if len(c.requestHandlers) == 0 {
return nil
}
for _, h := range c.requestHandlers {
if hErr := h.BeginRequest(ctx, req); hErr != nil {
return hErr
}
}
return nil
}
func (c *Client) emitEndRequest(ctx context.Context, resp *http.Response, err error) error {
if len(c.requestHandlers) == 0 {
return nil
}
for _, h := range c.requestHandlers {
if hErr := h.EndRequest(ctx, resp, err); hErr != nil {
return hErr
}
}
return err
}
// RequestOption declares the type of option one can pass
// to the Do methods(JSON, Form, ReadJSON...).
// Request options run before request constructed.
type RequestOption = func(*http.Request) error
// We always add the following request headers, unless they're removed by custom ones.
var defaultRequestOptions = []RequestOption{
RequestHeader(false, acceptKey, contentTypeJSON),
}
// RequestHeader adds or sets (if overridePrev is true) a header to the request.
func RequestHeader(overridePrev bool, key string, values ...string) RequestOption {
key = http.CanonicalHeaderKey(key)
return func(req *http.Request) error {
if overridePrev { // upsert.
req.Header[key] = values
} else { // just insert.
req.Header[key] = append(req.Header[key], values...)
}
return nil
}
}
// RequestAuthorization sets an Authorization request header.
// Note that we could do the same with a Transport RoundDrip too.
func RequestAuthorization(value string) RequestOption {
return RequestHeader(true, "Authorization", value)
}
// RequestAuthorizationBearer sets an Authorization: Bearer $token request header.
func RequestAuthorizationBearer(accessToken string) RequestOption {
headerValue := "Bearer " + accessToken
return RequestAuthorization(headerValue)
}
// RequestQuery adds a set of URL query parameters to the request.
func RequestQuery(query url.Values) RequestOption {
return func(req *http.Request) error {
q := req.URL.Query()
for k, v := range query {
q[k] = v
}
req.URL.RawQuery = q.Encode()
return nil
}
}
// RequestParam sets a single URL query parameter to the request.
func RequestParam(key string, values ...string) RequestOption {
return RequestQuery(url.Values{
key: values,
})
}
// Do sends an HTTP request and returns an HTTP response.
//
// The payload can be:
// - io.Reader
// - raw []byte
// - JSON raw message
// - string
// - struct (JSON).
//
// If method is empty then it defaults to "GET".
// The final variadic, optional input argument sets
// the custom request options to use before the request.
//
// Any HTTP returned error will be of type APIError
// or a timeout error if the given context was canceled.
func (c *Client) Do(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (*http.Response, error) {
if ctx == nil {
ctx = context.Background()
}
if c.rateLimiter != nil {
if err := c.rateLimiter.Wait(ctx); err != nil {
return nil, err
}
}
// Method defaults to GET.
if method == "" {
method = http.MethodGet
}
// Find the payload, if any.
var body io.Reader
if payload != nil {
switch v := payload.(type) {
case io.Reader:
body = v
case []byte:
body = bytes.NewBuffer(v)
case json.RawMessage:
body = bytes.NewBuffer(v)
case string:
body = strings.NewReader(v)
case url.Values:
body = strings.NewReader(v.Encode())
default:
w := new(bytes.Buffer)
// We assume it's a struct, we wont make use of reflection to find out though.
err := json.NewEncoder(w).Encode(v)
if err != nil {
return nil, err
}
body = w
}
}
if c.BaseURL != "" {
urlpath = c.BaseURL + urlpath // note that we don't do any special checks here, the caller is responsible.
}
// Initialize the request.
req, err := http.NewRequestWithContext(ctx, method, urlpath, body)
if err != nil {
return nil, err
}
// We separate the error for the default options for now.
for i, opt := range c.PersistentRequestOptions {
if opt == nil {
continue
}
if err = opt(req); err != nil {
return nil, fmt.Errorf("client.Do: default request option[%d]: %w", i, err)
}
}
// Apply any custom request options (e.g. content type, accept headers, query...)
for _, opt := range opts {
if opt == nil {
continue
}
if err = opt(req); err != nil {
return nil, err
}
}
if err = c.emitBeginRequest(ctx, req); err != nil {
return nil, err
}
// Caller is responsible for closing the response body.
// Also note that the gzip compression is handled automatically nowadays.
resp, respErr := c.HTTPClient.Do(req)
if err = c.emitEndRequest(ctx, resp, respErr); err != nil {
return nil, err
}
return resp, respErr
}
// DrainResponseBody drains response body and close it, allowing the transport to reuse TCP connections.
// It's automatically called on Client.ReadXXX methods on the end.
func (c *Client) DrainResponseBody(resp *http.Response) {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
const (
acceptKey = "Accept"
contentTypeKey = "Content-Type"
contentLengthKey = "Content-Length"
contentTypePlainText = "plain/text"
contentTypeJSON = "application/json"
contentTypeFormURLEncoded = "application/x-www-form-urlencoded"
)
// JSON writes data as JSON to the server.
func (c *Client) JSON(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (*http.Response, error) {
opts = append(opts, RequestHeader(true, contentTypeKey, contentTypeJSON))
return c.Do(ctx, method, urlpath, payload, opts...)
}
// JSON writes form data to the server.
func (c *Client) Form(ctx context.Context, method, urlpath string, formValues url.Values, opts ...RequestOption) (*http.Response, error) {
payload := formValues.Encode()
opts = append(opts,
RequestHeader(true, contentTypeKey, contentTypeFormURLEncoded),
RequestHeader(true, contentLengthKey, strconv.Itoa(len(payload))),
)
return c.Do(ctx, method, urlpath, payload, opts...)
}
// Uploader holds the necessary information for upload requests.
//
// Look the Client.NewUploader method.
type Uploader struct {
client *Client
body *bytes.Buffer
Writer *multipart.Writer
}
// AddFileSource adds a form field to the uploader with the given key.
func (u *Uploader) AddField(key, value string) error {
f, err := u.Writer.CreateFormField(key)
if err != nil {
return err
}
_, err = io.Copy(f, strings.NewReader(value))
return err
}
// AddFileSource adds a form file to the uploader with the given key.
func (u *Uploader) AddFileSource(key, filename string, source io.Reader) error {
f, err := u.Writer.CreateFormFile(key, filename)
if err != nil {
return err
}
_, err = io.Copy(f, source)
return err
}
// AddFile adds a local form file to the uploader with the given key.
func (u *Uploader) AddFile(key, filename string) error {
source, err := os.Open(filename)
if err != nil {
return err
}
return u.AddFileSource(key, filename, source)
}
// Uploads sends local data to the server.
func (u *Uploader) Upload(ctx context.Context, method, urlpath string, opts ...RequestOption) (*http.Response, error) {
err := u.Writer.Close()
if err != nil {
return nil, err
}
payload := bytes.NewReader(u.body.Bytes())
opts = append(opts, RequestHeader(true, contentTypeKey, u.Writer.FormDataContentType()))
return u.client.Do(ctx, method, urlpath, payload, opts...)
}
// NewUploader returns a structure which is responsible for sending
// file and form data to the server.
func (c *Client) NewUploader() *Uploader {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
return &Uploader{
client: c,
body: body,
Writer: writer,
}
}
// ReadJSON binds "dest" to the response's body.
// After this call, the response body reader is closed.
func (c *Client) ReadJSON(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption) error {
if payload != nil {
opts = append(opts, RequestHeader(true, contentTypeKey, contentTypeJSON))
}
resp, err := c.Do(ctx, method, urlpath, payload, opts...)
if err != nil {
return err
}
defer c.DrainResponseBody(resp)
if resp.StatusCode >= http.StatusBadRequest {
return ExtractError(resp)
}
// DBUG
// b, _ := io.ReadAll(resp.Body)
// println(string(b))
// return json.Unmarshal(b, &dest)
return json.NewDecoder(resp.Body).Decode(&dest)
}
// ReadPlain like ReadJSON but it accepts a pointer to a string or byte slice or integer
// and it reads the body as plain text.
func (c *Client) ReadPlain(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption) error {
resp, err := c.Do(ctx, method, urlpath, payload, opts...)
if err != nil {
return err
}
defer c.DrainResponseBody(resp)
if resp.StatusCode >= http.StatusBadRequest {
return ExtractError(resp)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
switch ptr := dest.(type) {
case *[]byte:
*ptr = body
return nil
case *string:
*ptr = string(body)
return nil
case *int:
*ptr, err = strconv.Atoi(string(body))
return err
default:
return fmt.Errorf("unsupported response body type: %T", ptr)
}
}
// GetPlainUnquote reads the response body as raw text and tries to unquote it,
// useful when the remote server sends a single key as a value but due to backend mistake
// it sends it as JSON (quoted) instead of plain text.
func (c *Client) GetPlainUnquote(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (string, error) {
var bodyStr string
if err := c.ReadPlain(ctx, &bodyStr, method, urlpath, payload, opts...); err != nil {
return "", err
}
s, err := strconv.Unquote(bodyStr)
if err == nil {
bodyStr = s
}
return bodyStr, nil
}
// WriteTo reads the response and then copies its data to the "dest" writer.
// If the "dest" is a type of HTTP response writer then it writes the
// content-type and content-length of the original request.
//
// Returns the amount of bytes written to "dest".
func (c *Client) WriteTo(ctx context.Context, dest io.Writer, method, urlpath string, payload interface{}, opts ...RequestOption) (int64, error) {
if payload != nil {
opts = append(opts, RequestHeader(true, contentTypeKey, contentTypeJSON))
}
resp, err := c.Do(ctx, method, urlpath, payload, opts...)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if w, ok := dest.(http.ResponseWriter); ok {
// Copy the content type and content-length.
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
if resp.ContentLength > 0 {
w.Header().Set("Content-Length", strconv.FormatInt(resp.ContentLength, 10))
}
}
return io.Copy(dest, resp.Body)
}
// BindResponse consumes the response's body and binds the result to the "dest" pointer,
// closing the response's body is up to the caller.
//
// The "dest" will be binded based on the response's content type header.
// Note that this is strict in order to catch bad actioners fast,
// e.g. it wont try to read plain text if not specified on
// the response headers and the dest is a *string.
func BindResponse(resp *http.Response, dest interface{}) (err error) {
contentType := trimHeader(resp.Header.Get(contentTypeKey))
switch contentType {
case contentTypeJSON: // the most common scenario on successful responses.
return json.NewDecoder(resp.Body).Decode(&dest)
case contentTypePlainText:
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
switch v := dest.(type) {
case *string:
*v = string(b)
case *[]byte:
*v = b
default:
return fmt.Errorf("plain text response should accept a *string or a *[]byte")
}
default:
acceptContentType := trimHeader(resp.Request.Header.Get(acceptKey))
msg := ""
if acceptContentType == contentType {
// Here we make a special case, if the content type
// was explicitly set by the request but we cannot handle it.
msg = fmt.Sprintf("current implementation can not handle the received (and accepted) mime type: %s", contentType)
} else {
msg = fmt.Sprintf("unexpected mime type received: %s", contentType)
}
err = errors.New(msg)
}
return
}
func trimHeader(v string) string {
for i, char := range v {
if char == ' ' || char == ';' {
return v[:i]
}
}
return v
}