-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_options.go
137 lines (120 loc) · 2.77 KB
/
request_options.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
package quick
import (
"net/http"
"net/url"
"strings"
"time"
)
// OptionFunc request option func
type OptionFunc func(*Request)
// OptionHeader set request header
func OptionHeader(v interface{}) OptionFunc {
hd := make(http.Header)
switch t := v.(type) {
case http.Header:
hd = t
case map[string]string:
for k, v := range t {
hd.Set(k, v)
}
case []string:
for _, v := range t {
arr := strings.Split(v, ":")
if len(arr) == 2 {
hd.Set(strings.TrimSpace(arr[0]), strings.TrimSpace(arr[1]))
} else if len(arr) == 1 {
hd.Set(strings.TrimSpace(arr[0]), "")
}
}
default:
panic("Header: parameter types are not supported")
}
return func(req *Request) {
req.SetHeader(hd)
}
}
// OptionHeaderSingle set an http header to request
func OptionHeaderSingle(k, v string) OptionFunc {
return func(req *Request) {
req.SetHeaderSingle(k, v)
}
}
// OptionQueryString request query string for get
func OptionQueryString(v interface{}) OptionFunc {
return func(req *Request) {
req.SetQueryString(v)
}
}
// OptionBody request body for post
func OptionBody(v interface{}) OptionFunc {
return func(req *Request) {
req.SetBody(v)
}
}
// OptionBodyFormData request body for post (FormData)
func OptionBodyFormData(v interface{}) OptionFunc {
return func(req *Request) {
req.SetBodyFormData(v)
}
}
// OptionBodyXWwwFormUrlencoded set request body x-www-form-urlencoded
func OptionBodyXWwwFormUrlencoded(v interface{}) OptionFunc {
return func(req *Request) {
req.SetBodyXWwwFormUrlencoded(v)
}
}
// OptionBodyJSON request body for post
func OptionBodyJSON(v interface{}) OptionFunc {
return func(req *Request) {
req.SetBodyJson(v)
}
}
// OptionBodyXML request body for post
func OptionBodyXML(v interface{}) OptionFunc {
return func(req *Request) {
req.SetBodyXML(v)
}
}
// OptionBasicAuth HTTP Basic Authentication
func OptionBasicAuth(username, password string) OptionFunc {
return func(req *Request) {
req.SetBasicAuth(username, password)
}
}
// OptionProxy set proxy for request
func OptionProxy(v interface{}) OptionFunc {
switch t := v.(type) {
case string:
return func(req *Request) {
req.SetProxyUrl(t)
}
case *url.URL:
return func(req *Request) {
req.Proxy = t
}
case url.URL:
return func(req *Request) {
req.Proxy = &t
}
default:
panic("Proxy: parameter types are not supported")
}
}
// OptionTimeout set timeout to request
func OptionTimeout(v time.Duration) OptionFunc {
return func(req *Request) {
req.Timeout = v
}
}
// OptionRedirectNum set redirect num to request
func OptionRedirectNum(num int) OptionFunc {
return func(req *Request) {
req.RedirectNum = num
}
}
// OptionCookies set cookies to request
func OptionCookies(cookies Cookies) OptionFunc {
return func(req *Request) {
req.Cookies = cookies
}
}