-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
464 lines (354 loc) · 10.1 KB
/
request.go
File metadata and controls
464 lines (354 loc) · 10.1 KB
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
package isuperagent
import (
"context"
"crypto/tls"
"net/http"
"net/url"
"strings"
"time"
"github.com/charleslxh/isuperagent/bodyParser"
)
type Request interface {
SetMethod(method string, options ...interface{}) Request
GetMethod() string
Get(url string, options ...interface{}) Request
Post(url string, options ...interface{}) Request
Head(url string, options ...interface{}) Request
Put(url string, options ...interface{}) Request
Update(url string, options ...interface{}) Request
Delete(url string, options ...interface{}) Request
SetUrl(url string) Request
GetUrl() *URL
GetRawUrl() string
GetHeader(name string) string
SetHeader(name, value string) Request
GetHeaders() http.Header
SetHeaders(kv map[string]string) Request
SetContentType(contentType string) Request
GetQuery(name string) string
SetQuery(name string, value string) Request
GetQueries() url.Values
SetQueries(kv map[string]string) Request
SetBody(v interface{}) Request
GetBody() interface{}
GetBodyRaw() ([]byte, error)
SetTimeout(d time.Duration) Request
GetTimeout() time.Duration
SetRetry(times int) Request
GetRetry() int
SetInsecureSkipVerify(insecureSkipVerify bool) Request
GetInsecureSkipVerify() bool
SetTlsConfig(tlsConfig *tls.Config) Request
GetTlsConfig() *tls.Config
SetCa(caPath string) Request
GetCa() string
SetCert(certPath, keyPath string) Request
GetCert() (string, string)
BasicAuth(name, pass string) Request
GetUsername() string
GetPassword() string
SetContext(ctx context.Context) Request
Middleware(middleware ...Middleware) Request
Do() (Response, error)
}
type irequest struct {
Context context.Context
Method string
Url *URL
ContentType ContentType
Timeout time.Duration
Retry int
Headers http.Header
// Optionally override the trusted CA certificates.
// Default is to trust the well-known CAs curated by Mozilla
Ca string
// Cert chains in PEM format.
// One cert chain should be provided per private key
Cert string
// Private keys in PEM format.
// PEM allows the option of private keys being encrypted
Key string
// If false, the server certificate is verified against the list of supplied CAs.
// An 'error' event is emitted if verification fails; err.code contains the OpenSSL error code.
// Default: false.
InsecureSkipVerify bool
TlsConfig *tls.Config
Body interface{}
BodyRaw []byte
// Basic Auth
Username string
Password string
Middlewares []Middleware
}
const (
Method_GET = "GET"
Method_POST = "POST"
Method_HEAD = "HEAD"
Method_PUT = "PUT"
Method_UPDATE = "UPDATE"
Method_DELETE = "DELETE"
)
func NewRequest() Request {
return &irequest{Context: context.Background(), Url: NewURL(), Headers: http.Header{}}
}
func NewRequestWithContext(ctx context.Context) Request {
return &irequest{Context: ctx, Url: NewURL(), Headers: http.Header{}}
}
func (r *irequest) SetContext(ctx context.Context) Request {
r.Context = ctx
return r
}
// Set request options, method, url, body, header, query string
// The first argument is url, it is required.
// All of other arguments are not required, they can be set by other functions, such as Header(), Body() and so on.
// options definition as the following:
// 1. Set request url if first options exists, it must be string type, ignore is otherwise..
// 2. Set request body if second options exists.
// 3. Set request headers if third options exists, it must be map[string]string type, ignore is otherwise.
// 4. Set request queries if fourth options exists, it must be map[string]string type, ignore is otherwise.
func (r *irequest) SetMethod(method string, options ...interface{}) Request {
r.Method = strings.ToUpper(method)
if len(options) > 0 {
if v, ok := options[0].(string); ok {
r.SetUrl(v)
}
}
if len(options) > 1 {
r.SetBody(options[1])
}
if len(options) > 2 {
if v, ok := options[2].(map[string]string); ok {
r.SetHeaders(v)
}
}
if len(options) > 3 {
if v, ok := options[3].(map[string]string); ok {
r.SetQueries(v)
}
}
return r
}
func (r *irequest) GetMethod() string {
return r.Method
}
// Set request URL
func (r *irequest) SetUrl(uri string) Request {
urlObj, _ := url.Parse(uri)
// parse query
for k, vs := range urlObj.Query() {
for _, v := range vs {
r.SetQuery(k, v)
}
}
r.Url.URL = urlObj
return r
}
func (r *irequest) GetUrl() *URL {
return r.Url
}
func (r *irequest) GetRawUrl() string {
return r.Url.String()
}
// Set request Host
func (r *irequest) Host(host string) Request {
r.Url.Host = host
return r
}
func (r *irequest) GetHost() string {
return r.Url.Host
}
// Set request method to GET and request URL
// The options same as POST method.
// But body fields is nil.
func (r *irequest) Get(url string, options ...interface{}) Request {
options = append([]interface{}{url, nil}, options...)
r.SetMethod(Method_GET, url)
return r
}
// Set POST options, method, url, body, header, query string
// The first argument is url, it is required.
// All of other arguments are not required, they can be set by other functions, such as Header(), Body() and so on.
// options definition as the following:
// 1. Set request body if first options exists.
// 2. Set request headers if second options exists, it must be map[string]string type, ignore is otherwise.
// 3. Set request queries if third options exists, it must be map[string]string type, ignore is otherwise.
func (r *irequest) Post(url string, options ...interface{}) Request {
options = append([]interface{}{url}, options...)
r.SetMethod(Method_POST, options...)
return r
}
// Set request method to HEAD and request URL
// The options same as POST method.
// But body fields is nil.
func (r *irequest) Head(url string, options ...interface{}) Request {
options = append([]interface{}{url, nil}, options...)
r.SetMethod(Method_HEAD, url)
return r
}
// Set Put options, url, method, query string, body, header
// The options same as POST method.
func (r *irequest) Put(url string, options ...interface{}) Request {
options = append([]interface{}{url}, options...)
r.SetMethod(Method_PUT, options...)
return r
}
// Set Update options, url, method, query string, body, header
// The options same as POST method.
func (r *irequest) Update(url string, options ...interface{}) Request {
options = append([]interface{}{url}, options...)
r.SetMethod(Method_UPDATE, options...)
return r
}
// Set request method to HEAD and request URL
// The options same as POST method.
// But body fields is nil.
func (r *irequest) Delete(url string, options ...interface{}) Request {
options = append([]interface{}{url, nil}, options...)
r.SetMethod(Method_DELETE, url)
return r
}
func (r *irequest) SetHeader(name, value string) Request {
r.Headers.Add(name, value)
return r
}
func (r *irequest) GetHeader(name string) string {
return r.Headers.Get(name)
}
func (r *irequest) SetHeaders(kvs map[string]string) Request {
for k, v := range kvs {
r.SetHeader(k, v)
}
return r
}
func (r *irequest) GetHeaders() http.Header {
return r.Headers
}
func (r *irequest) SetQuery(name, value string) Request {
r.Url.Queries.Add(name, value)
return r
}
func (r *irequest) GetQuery(name string) string {
if v := r.Url.Queries.Get(name); v != "" {
return v
}
return r.Url.Query().Get(name)
}
func (r *irequest) SetQueries(kvs map[string]string) Request {
for k, v := range kvs {
r.SetQuery(k, v)
}
return r
}
func (r *irequest) GetQueries() url.Values {
return r.Url.Queries
}
func (r *irequest) SetContentType(contentType string) Request {
r.ContentType = ParseContentType(contentType)
return r
}
func (r *irequest) SetTimeout(d time.Duration) Request {
r.Timeout = d
return r
}
func (r *irequest) GetTimeout() time.Duration {
return r.Timeout
}
func (r *irequest) SetRetry(times int) Request {
r.Retry = times
return r
}
func (r *irequest) GetRetry() int {
return r.Retry
}
func (r *irequest) IsHttps() bool {
return "https" == r.Url.Scheme
}
func (r *irequest) Middleware(middleware ...Middleware) Request {
r.Middlewares = append(r.Middlewares, middleware...)
return r
}
func (r *irequest) SetBody(v interface{}) Request {
r.Body = v
return r
}
func (r *irequest) GetBody() interface{} {
return r.Body
}
func (r *irequest) GetBodyRaw() ([]byte, error) {
// generate request body
requestBody, err := bodyParser.Marshal(r.ContentType.MediaType, r.Body)
if err != nil {
return nil, err
}
return requestBody, nil
}
func (r *irequest) SetCert(certPath, keyPath string) Request {
r.Cert = certPath
r.Key = keyPath
return r
}
func (r *irequest) GetCert() (string, string) {
return r.Cert, r.Key
}
// Set server root certificate.
func (r *irequest) SetCa(caPath string) Request {
r.Ca = caPath
return r
}
func (r *irequest) GetCa() string {
return r.Ca
}
func (r *irequest) BasicAuth(user, pass string) Request {
r.Username = user
r.Password = pass
return r
}
// Set username for basic auth.
func (r *irequest) SetUsername(username string) Request {
r.Username = username
return r
}
func (r *irequest) GetUsername() string {
return r.Username
}
// Set password for basic auth.
func (r *irequest) SetPassword(password string) Request {
r.Password = password
return r
}
func (r *irequest) GetPassword() string {
return r.Password
}
// Ignore verify the self-signed certificate.
// If InsecureSkipVerify = false, and the server's certificate is self-signed,
// The request connection will failed with error:
// x509: certificate signed by unknown authority.
// So you can set to true if you don't care about server's certificate.
func (r *irequest) SetInsecureSkipVerify(insecureSkipVerify bool) Request {
r.InsecureSkipVerify = insecureSkipVerify
return r
}
func (r *irequest) GetInsecureSkipVerify() bool {
return r.InsecureSkipVerify
}
// Set SSL config, see tls.Config
func (r *irequest) SetTlsConfig(tlsConfig *tls.Config) Request {
r.TlsConfig = tlsConfig
return r
}
func (r *irequest) GetTlsConfig() *tls.Config {
return r.TlsConfig
}
func (r *irequest) Do() (Response, error) {
m, err := NewMiddleware("request_exec")
if err != nil {
return nil, err
}
ctx := NewContext(r.Context, r, nil)
middleware := append(r.Middlewares, m)
err = Compose(ctx, middleware)()
if err != nil {
return nil, err
}
return ctx.GetRes(), nil
}