-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrequests.go
415 lines (348 loc) · 8.66 KB
/
requests.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
package requests
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"sync"
)
const (
ContentType = "Content-Type"
ApplicationJSON = "application/json"
ApplicationFormUrlencoded = "application/x-www-form-urlencoded"
)
// RequestInterceptor 请求拦截器
// 返回不为nil,即有错误会终止后续执行
type RequestInterceptor func(request *http.Request) error
// requestInterceptorChain 请求拦截链
type requestInterceptorChain struct {
mutex *sync.RWMutex
interceptors []RequestInterceptor
}
// defaultRequestInterceptorChain 默认的请求拦截链实例
var defaultRequestInterceptorChain = &requestInterceptorChain{
mutex: new(sync.RWMutex),
interceptors: make([]RequestInterceptor, 0),
}
// Client 封装了http的参数等信息
type Client struct {
// 自定义Client
client *http.Client
url string
method string
header http.Header
params url.Values
form url.Values
json interface{}
multipart FileForm
}
// FileForm form参数和文件参数
type FileForm struct {
Value url.Values
File map[string]string
}
// Result http响应结果
type Result struct {
Resp *http.Response
Err error
}
// Get http `GET` 请求
func Get(url string) *Client {
return newClient(url, http.MethodGet, nil)
}
// Post http `POST` 请求
func Post(url string) *Client {
return newClient(url, http.MethodPost, nil)
}
// Put http `PUT` 请求
func Put(url string) *Client {
return newClient(url, http.MethodPut, nil)
}
// Delete http `DELETE` 请求
func Delete(url string) *Client {
return newClient(url, http.MethodDelete, nil)
}
// Request 用于自定义请求方式,比如`HEAD`、`PATCH`、`OPTIONS`、`TRACE`
// client参数用于替换DefaultClient,如果为nil则会使用默认的
func Request(url, method string, client *http.Client) *Client {
return newClient(url, method, client)
}
// Params http请求中url参数
func (c *Client) Params(params url.Values) *Client {
for k, v := range params {
c.params[k] = v
}
return c
}
// Header http请求头
func (c *Client) Header(k, v string) *Client {
c.header.Set(k, v)
return c
}
// Headers http请求头
func (c *Client) Headers(header http.Header) *Client {
for k, v := range header {
c.header[k] = v
}
return c
}
// Form 表单提交参数
func (c *Client) Form(form url.Values) *Client {
c.header.Set(ContentType, ApplicationFormUrlencoded)
c.form = form
return c
}
// Json json提交参数
// 如果是string,则默认当作是json字符串;否则会序列化为json字节数组,再发送
func (c *Client) Json(json interface{}) *Client {
c.header.Set(ContentType, ApplicationJSON)
c.json = json
return c
}
// Multipart form-data提交参数
func (c *Client) Multipart(multipart FileForm) *Client {
c.multipart = multipart
return c
}
// Send 发送http请求
func (c *Client) Send() *Result {
var result *Result
// 处理query string
if c.params != nil && len(c.params) != 0 {
// 如果url中已经有query string参数,则只需要&拼接剩下的即可
encoded := c.params.Encode()
if strings.Index(c.url, "?") == -1 {
c.url += "?" + encoded
} else {
c.url += "&" + encoded
}
}
// 根据不同的Content-Type设置不同的http body
contentType := c.header.Get(ContentType)
if c.multipart.Value != nil || c.multipart.File != nil {
result = c.createMultipartForm()
} else if strings.HasPrefix(contentType, ApplicationJSON) {
result = c.createJson()
} else if strings.HasPrefix(contentType, ApplicationFormUrlencoded) {
result = c.createForm()
} else {
// 不是以上类型,就不设置http body
result = c.createEmptyBody()
}
return result
}
// createMultipartForm 创建form-data的请求
func (c *Client) createMultipartForm() *Result {
var result = new(Result)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// 设置文件字节
for name, filename := range c.multipart.File {
file, err := os.Open(filename)
if err != nil {
result.Err = err
return result
}
part, err := writer.CreateFormFile(name, filename)
if err != nil {
result.Err = err
return result
}
// todo 这里的io.Copy实现,会把file文件都读取到内存里面,然后当做一个buffer传给NewRequest。对于大文件来说会占用很多内存
_, err = io.Copy(part, file)
if err != nil {
result.Err = err
return result
}
err = file.Close()
if err != nil {
result.Err = err
return result
}
}
// 设置field
for name, values := range c.multipart.Value {
for _, value := range values {
_ = writer.WriteField(name, value)
}
}
err := writer.Close()
if err != nil {
result.Err = err
return result
}
req, err := http.NewRequest(c.method, c.url, body)
req.Header = c.header
req.Header.Set(ContentType, writer.FormDataContentType())
c.doSend(req, result)
return result
}
// createForm 创建application/json请求
func (c *Client) createJson() *Result {
var result = new(Result)
b, err := json.Marshal(c.json)
if err != nil {
result.Err = err
return result
}
req, err := http.NewRequest(c.method, c.url, bytes.NewReader(b))
if err != nil {
result.Err = err
return result
}
req.Header = c.header
c.doSend(req, result)
return result
}
// createForm 创建application/x-www-form-urlencoded请求
func (c *Client) createForm() *Result {
var result = new(Result)
form := c.form.Encode()
req, err := http.NewRequest(c.method, c.url, strings.NewReader(form))
if err != nil {
result.Err = err
return result
}
req.Header = c.header
c.doSend(req, result)
return result
}
// createEmptyBody 没有内容的body
func (c *Client) createEmptyBody() *Result {
var result = new(Result)
req, err := http.NewRequest(c.method, c.url, nil)
if err != nil {
result.Err = err
return result
}
req.Header = c.header
c.doSend(req, result)
return result
}
// doSend 发送请求
func (c *Client) doSend(req *http.Request, result *Result) {
// 调用拦截器,遇到错误就退出
if err := c.beforeSend(req); err != nil {
result.Err = err
return
}
// 发送请求
result.Resp, result.Err = c.client.Do(req)
}
// beforeSend 发送请求前,调用拦截器
func (c *Client) beforeSend(req *http.Request) error {
mutex := defaultRequestInterceptorChain.mutex
mutex.RLock()
defer mutex.RUnlock()
// 遍历调用拦截器
for _, interceptor := range defaultRequestInterceptorChain.interceptors {
err := interceptor(req)
if err != nil {
return err
}
}
return nil
}
// StatusOk 判断http响应码是否为200
func (r *Result) StatusOk() *Result {
if r.Err != nil {
return r
}
if r.Resp.StatusCode != http.StatusOK {
r.Err = errors.New("status code is not 200")
return r
}
return r
}
// Status2xx 判断http响应码是否为2xx
func (r *Result) Status2xx() *Result {
if r.Err != nil {
return r
}
if r.Resp.StatusCode < http.StatusOK || r.Resp.StatusCode >= http.StatusMultipleChoices {
r.Err = errors.New("status code is not match [200, 300)")
return r
}
return r
}
// Raw 获取http响应内容,返回字节数组
func (r *Result) Raw() ([]byte, error) {
if r.Err != nil {
return nil, r.Err
}
b, err := ioutil.ReadAll(r.Resp.Body)
if err != nil {
r.Err = err
return nil, r.Err
}
defer r.Resp.Body.Close()
return b, r.Err
}
// Text 获取http响应内容,返回字符串
func (r *Result) Text() (string, error) {
b, err := r.Raw()
if err != nil {
r.Err = err
return "", r.Err
}
return string(b), nil
}
// Json 获取http响应内容,返回json
func (r *Result) Json(v interface{}) error {
b, err := r.Raw()
if err != nil {
r.Err = err
return r.Err
}
return json.Unmarshal(b, v)
}
// Save 获取http响应内容,保存为文件
func (r *Result) Save(name string) error {
if r.Err != nil {
return r.Err
}
f, err := os.Create(name)
if err != nil {
r.Err = err
return r.Err
}
defer f.Close()
_, err = io.Copy(f, r.Resp.Body)
if err != nil {
r.Err = err
return r.Err
}
defer r.Resp.Body.Close()
return nil
}
// newClient 创建Client
func newClient(u string, method string, client *http.Client) *Client {
// client为nil则使用默认的DefaultClient
if client == nil {
client = http.DefaultClient
}
return &Client{
client: client,
url: u,
method: method,
header: make(http.Header),
params: make(url.Values),
form: make(url.Values),
}
}
// AddRequestInterceptors 添加请求拦截器
func AddRequestInterceptors(interceptors ...RequestInterceptor) {
mutex := defaultRequestInterceptorChain.mutex
mutex.Lock()
defer mutex.Unlock()
// 添加到拦截器链
for _, interceptor := range interceptors {
defaultRequestInterceptorChain.interceptors = append(defaultRequestInterceptorChain.interceptors, interceptor)
}
}