/
fetch.go
490 lines (418 loc) · 12.1 KB
/
fetch.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
package fetch
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/beanscc/fetch/binding"
"github.com/beanscc/fetch/body"
"github.com/beanscc/fetch/util"
)
// Fetch ...
type Fetch struct {
// client 的基础 url, 若 baseURL 带有部分 path (eg: host:port/path/), 请在 path 后跟上 "/"
// baseURL 和 URLPath 的相对/绝对关系,请参考url.ResolveReference()
//
// - baseURL: host/v1/api/
// - 若 Get(ctx, "user/profile"),则实际请求的是 host/v1/api/user/profile
// - 若 Get(ctx, "/user/profile"),则实际请求的是 host/user/profile
// - 若 Get(ctx, "/v2/api/user/profile"),则实际请求的是 host/v2/api/user/profile
// - 若 Get(ctx, "../order/detail"),则实际请求的是 host/v1/order/detail
// - baseURL: host/v1/api
// - 若 Get(ctx, "user/profile"),则实际请求的是 host/v1/user/profile
// - 若 Get(ctx, "/user/profile"),则实际请求的是 host/user/profile
// - 若 Get(ctx, "/v2/api/user/profile"),则实际请求的是 host/v2/api/user/profile
// - 若 Get(ctx, "api/user/profile"),则实际请求的是 host/v1/api/user/profile
// - 若 Get(ctx, "../order/detail"),则实际请求的是 host/order/detail
baseURL string
client *http.Client // client
interceptors []Interceptor // 拦截器
chainInterceptor Interceptor // 链式拦截器,由注册的拦截器合并而来
req *request // once req
debug bool // debug 输出请求和响应的详细信息,一般用于调试期间
err error // error
ctx context.Context // ctx
timeout time.Duration // timeout duration
bind map[string]binding.Binding // 设置 bind 的实现对象
}
// New return new Fetch
func New(baseURL string, options ...Option) *Fetch {
f := &Fetch{
client: http.DefaultClient,
baseURL: baseURL,
interceptors: make([]Interceptor, 0),
chainInterceptor: chainInterceptor(),
req: newRequest(),
debug: false,
err: nil,
ctx: context.Background(),
bind: map[string]binding.Binding{
"json": &binding.JSON{},
"xml": &binding.XML{},
},
}
return f.WithOptions(options...)
}
// clone return a clone Fetch
func (f *Fetch) clone() *Fetch {
nf := new(Fetch)
*nf = *f
// reset
nf.req = newRequest()
nf.err = nil
nf.ctx = context.Background()
return nf
}
// withContext return new Fetch with ctx
func (f *Fetch) withContext(ctx context.Context) *Fetch {
if ctx == nil {
panic("fetch: nil context")
}
nf := f.clone()
nf.ctx = ctx
return nf
}
// Context return context
func (f *Fetch) Context() context.Context {
if f.ctx == nil {
return context.Background()
}
return f.ctx
}
// WithOptions 返回一个设置了新 option 的 *Fetch 对象
func (f *Fetch) WithOptions(options ...Option) *Fetch {
if len(options) == 0 {
return f
}
nf := f.clone()
for _, option := range options {
option.Apply(nf)
}
return nf
}
// Get get 请求
func (f *Fetch) Get(ctx context.Context, path string, params ...interface{}) *Fetch {
return f.Method(ctx, http.MethodGet, path, params...)
}
// Post post 请求
func (f *Fetch) Post(ctx context.Context, path string, params ...interface{}) *Fetch {
return f.Method(ctx, http.MethodPost, path, params...)
}
// Put put 请求
func (f *Fetch) Put(ctx context.Context, path string, params ...interface{}) *Fetch {
return f.Method(ctx, http.MethodPut, path, params...)
}
// Delete del 请求
func (f *Fetch) Delete(ctx context.Context, path string, params ...interface{}) *Fetch {
return f.Method(ctx, http.MethodDelete, path, params...)
}
// Head 请求
func (f *Fetch) Head(ctx context.Context, path string, params ...interface{}) *Fetch {
return f.Method(ctx, http.MethodHead, path, params...)
}
func (f *Fetch) Method(ctx context.Context, method string, path string, params ...interface{}) *Fetch {
if f.err != nil {
return f
}
nf := f.withContext(ctx)
nf.req.Method = method
if len(params) > 0 {
paths := strings.Split(path, "/")
ii := 0
for _, v := range params {
for ; ii < len(paths); ii++ {
if strings.HasPrefix(paths[ii], ":") {
paths[ii] = util.ToString(v)
ii++
break
}
}
}
path = strings.Join(paths, "/")
}
nf.req.URL, nf.err = util.ResolveReferenceURL(nf.baseURL, path)
return nf
}
// Query 设置查询参数
// args 支持 key-val 对,或 map[string]interface{},或者 key-val 对和map[string]interface{}交替组合
// Query("k1", 1, "k2", 2, map[string]interface{}{"k3": "v3"})
func (f *Fetch) Query(args ...interface{}) *Fetch {
if f.err != nil {
return f
}
if len(args) > 0 {
if f.req.Method == "" {
f.err = errors.New("fetch.Query: empty method, please use Get()/Post() etc. or Method() to set method")
return f
}
q := f.req.URL.Query()
for i := 0; i < len(args); {
if m, ok := args[i].(map[string]interface{}); ok {
for k, v := range m {
q.Set(k, util.ToString(v))
}
i++
continue
}
if i == len(args)-1 {
f.err = errors.New("fetch.Query: args must be key-val pair or map[string]interface{}")
return f
}
// key-val pair
key, val := args[i], args[i+1]
if keyStr, ok := key.(string); ok {
q.Set(keyStr, util.ToString(val))
} else {
f.err = fmt.Errorf("fetch.Query: args key-val parir key[%v] must be string type", key)
return f
}
i += 2
}
f.req.URL.RawQuery = q.Encode()
}
return f
}
// AddHeader 添加 http header
// args 支持 key-val 对,或 map[string]interface{},或者 key-val 对和map[string]interface{}交替组合
func (f *Fetch) AddHeader(args ...interface{}) *Fetch {
if f.err != nil {
return f
}
if len(args) > 0 {
for i := 0; i < len(args); {
if m, ok := args[i].(map[string]interface{}); ok {
for k, v := range m {
f.req.Header.Add(k, util.ToString(v))
}
i++
continue
}
if i == len(args)-1 {
f.err = errors.New("fetch.AddHeader: args must be key-val pair or map[string]interface{}")
return f
}
// key-val pair
key, val := args[i], args[i+1]
if keyStr, ok := key.(string); ok {
f.req.Header.Add(keyStr, util.ToString(val))
} else {
f.err = fmt.Errorf("fetch.AddHeader: args key-val parir key[%v] must be string type", key)
return f
}
i += 2
}
}
return f
}
// SetHeader 设置 http header
// args 支持 key-val 对,或 map[string]interface{},或者 key-val 对和map[string]interface{}交替组合
func (f *Fetch) SetHeader(args ...interface{}) *Fetch {
if f.err != nil {
return f
}
if len(args) > 0 {
for i := 0; i < len(args); {
if m, ok := args[i].(map[string]interface{}); ok {
for k, v := range m {
f.req.Header.Set(k, util.ToString(v))
}
i++
continue
}
if i == len(args)-1 {
f.err = errors.New("fetch.SetHeader: args must be key-val pair or map[string]interface{}")
return f
}
// key-val pair
key, val := args[i], args[i+1]
if keyStr, ok := key.(string); ok {
f.req.Header.Set(keyStr, util.ToString(val))
} else {
f.err = fmt.Errorf("fetch.SetHeader: args key-val parir key[%v] must be string type", key)
return f
}
i += 2
}
}
return f
}
func (f *Fetch) AddCookie(cs ...*http.Cookie) *Fetch {
for _, c := range cs {
f.req.AddCookie(c)
}
return f
}
func (f *Fetch) SetBasicAuth(username, password string) *Fetch {
f.req.SetBasicAuth(username, password)
return f
}
func (f *Fetch) buildRequest() (*http.Request, error) {
if f.err != nil {
return nil, f.err
}
if f.req.Method == "" {
f.err = errors.New("fetch: empty method")
return nil, f.err
}
if f.req.URL.String() == "" {
f.err = errors.New("fetch: empty url")
return nil, f.err
}
// build req
req, err := http.NewRequest(f.req.Method, f.req.URL.String(), f.req.body)
if err != nil {
f.err = err
return nil, err
}
req = req.WithContext(f.Context())
// clone header
req.Header = f.cloneHeader(f.req.Header)
return req, nil
}
func (f *Fetch) cloneHeader(h http.Header) http.Header {
if h == nil {
return nil
}
// Find total number of values.
nv := 0
for _, vv := range h {
nv += len(vv)
}
sv := make([]string, nv) // shared backing array for headers' values
h2 := make(http.Header, len(h))
for k, vv := range h {
n := copy(sv, vv)
h2[k] = sv[:n:n]
sv = sv[n:]
}
return h2
}
// ================== set request body ==================
// Body 设置请求的 body 消息体
func (f *Fetch) Body(b body.Body) *Fetch {
if b != nil {
bb, err := b.Body()
if err != nil {
f.err = err
return f
}
f.req.body = bb // set body not Body
f.req.Header.Set(body.HeaderContentType, b.ContentType())
}
return f
}
// JSON 发送 application/json 格式消息
// p 支持 string/[]byte/其他类型按 json.Marshal 编码
func (f *Fetch) JSON(data interface{}) *Fetch {
return f.Body(body.NewJSON(data))
}
// XML 发送 application/xml 格式消息
// p 支持 string/[]byte/其他类型按 xml.Marshal 编码
func (f *Fetch) XML(data interface{}) *Fetch {
return f.Body(body.NewXML(data))
}
// Form 发送 x-www-form-urlencoded 格式消息
// data 支持 map[string]interface{} 或 url.Values
func (f *Fetch) Form(data interface{}) *Fetch {
var b body.Body
switch data.(type) {
case map[string]interface{}:
b = body.NewFormFromMap(data.(map[string]interface{}))
case url.Values:
b = body.NewForm(data.(url.Values))
default:
b = body.NewErr(errors.New("fetch.Form: unsupported data type"))
}
return f.Body(b)
}
// MultipartForm 发送 multipart/form-data 格式消息
// data 支持 map[string]interface{} 或 url.Values
func (f *Fetch) MultipartForm(data interface{}, fs ...body.File) *Fetch {
var b body.Body
switch data.(type) {
case map[string]interface{}:
b = body.NewMultipartFormFromMap(data.(map[string]interface{}), fs...)
case url.Values:
b = body.NewMultipartForm(data.(url.Values), fs...)
default:
b = body.NewErr(errors.New("fetch.MultipartForm: unsupported data type"))
}
return f.Body(b)
}
// ================== set request body end ==================
// ================== bind body ==================
// Bind 按已注册 bind 类型,解析 http 响应
func (f *Fetch) Bind(bind binding.Binding, v interface{}) error {
b, ok := f.bind[bind.Name()]
if !ok {
return fmt.Errorf("fetch.Bind: unknown bind[%s]", bind.Name())
}
resp, respBody, err := f.Resp()
if err != nil {
return err
}
if resp == nil {
return errors.New("fetch.Bind: nil http.Response")
}
return b.Bind(resp, respBody, v)
}
// BindJSON bind http.Body with json
func (f *Fetch) BindJSON(v interface{}) error {
return f.Bind(&binding.JSON{}, v)
}
// BindXML bind http.Body with xml
func (f *Fetch) BindXML(v interface{}) error {
return f.Bind(&binding.XML{}, v)
}
// ================== bind body end ==================
// do 构造并执行 http 请求
func (f *Fetch) do() *response {
if f.err != nil {
return newErrResp(f.err)
}
// timeout must before buildRequest
if f.timeout > 0 {
var cancel context.CancelFunc
f.ctx, cancel = context.WithTimeout(f.Context(), f.timeout)
defer cancel()
}
req, err := f.buildRequest()
if err != nil {
return newErrResp(err)
}
// 定义 handle
handler := func(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
if f.debug { // debug req
_ = dumpRequest(req, true)
}
resp, err := f.client.Do(req)
if err != nil {
return resp, nil, err
}
defer resp.Body.Close()
if f.debug { // debug resp
_ = dumpResponse(resp, true)
}
var b []byte
b, resp.Body, err = util.DrainBody(resp.Body)
return resp, b, err
}
resp, respBody, err := f.chainInterceptor(f.Context(), req, handler)
return &response{resp: resp, body: respBody, err: err}
}
// Resp return http.Response, resp body, err
func (f *Fetch) Resp() (*http.Response, []byte, error) {
res := f.do()
return res.resp, res.body, res.err
}
// Bytes 返回http响应body消息体
func (f *Fetch) Bytes() ([]byte, error) {
return f.do().Bytes()
}
// Text 返回http响应body消息体
func (f *Fetch) Text() (string, error) {
return f.do().Text()
}