This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
/
options.go
284 lines (247 loc) · 5.57 KB
/
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
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
package requests
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"io"
"net"
"net/http"
"net/url"
"time"
)
// Options request
type Options struct {
Method string
URL string
Path []string
Params map[string]any
body any
Header http.Header
Cookies []http.Cookie
Timeout time.Duration
MaxConns int
TraceLv int
TraceLimit int
Verify bool
Logf func(ctx context.Context, stat Stat)
Stream func(int64, []byte) error
RequestEach []func(context.Context, *http.Request) error
ResponseEach []func(context.Context, *http.Response) error
// session used
LocalAddr net.Addr
Hosts map[string][]string // 内部host文件
Proxy func(*http.Request) (*url.URL, error)
}
// Option func
type Option func(*Options)
// NewOptions new request
func newOptions(opts ...Option) Options {
opt := Options{
Method: "GET",
Params: make(map[string]any),
Header: make(http.Header),
Timeout: 30 * time.Second,
MaxConns: 100,
Hosts: make(map[string][]string),
Proxy: http.ProxyFromEnvironment,
TraceLimit: 1024,
}
for _, o := range opts {
o(&opt)
}
return opt
}
// Method http method
var (
MethodGet = Method("GET")
MethodPost = Method("POST")
)
// MaxConns set max connections
func MaxConns(conn int) Option {
return func(o *Options) {
o.MaxConns = conn
}
}
// Method set method
func Method(method string) Option {
return func(o *Options) {
o.Method = method
}
}
// URL set url
func URL(url string) Option {
return func(o *Options) {
o.URL = url
}
}
// Path set path
func Path(path string) Option {
return func(o *Options) {
o.Path = append(o.Path, path)
}
}
// Params add query args
func Params(query map[string]any) Option {
return func(o *Options) {
for k, v := range query {
o.Params[k] = v
}
}
}
// Param params
func Param(k string, v any) Option {
return func(o *Options) {
o.Params[k] = v
}
}
// Body request body
func Body(body any) Option {
return func(o *Options) {
o.body = body
}
}
// Gzip request gzip compressed
func Gzip(body any) Option {
reader, err := makeBody(body)
if err != nil {
panic(err)
}
var buf bytes.Buffer
w := gzip.NewWriter(&buf)
defer w.Close()
if _, err := io.Copy(w, reader); err != nil {
panic(err)
}
return func(o *Options) {
o.body = &buf
o.Header.Add("Accept-Encoding", "gzip")
o.Header.Add("Content-Encoding", "gzip")
}
}
// Form set form, content-type is
func Form(form url.Values) Option {
return func(o *Options) {
o.Header.Add("content-type", "application/x-www-form-urlencoded")
o.body = form
}
}
// Header header
func Header(k, v string) Option {
return func(o *Options) {
o.Header.Add(k, v)
}
}
// Headers headers
func Headers(kv map[string]string) Option {
return func(o *Options) {
for k, v := range kv {
o.Header.Add(k, v)
}
}
}
// Cookie cookie
func Cookie(cookie http.Cookie) Option {
return func(o *Options) {
o.Cookies = append(o.Cookies, cookie)
}
}
// Cookies cookies
func Cookies(cookies ...http.Cookie) Option {
return func(o *Options) {
o.Cookies = append(o.Cookies, cookies...)
}
}
// BasicAuth base auth
func BasicAuth(user, pass string) Option {
return Header("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(user+":"+pass)))
}
// Timeout client timeout duration
func Timeout(timeout time.Duration) Option {
return func(o *Options) {
o.Timeout = timeout
}
}
// TraceLv Trace
func TraceLv(v int, limit ...int) Option {
return func(o *Options) {
o.TraceLv = v
if len(limit) != 0 {
o.TraceLimit = limit[0]
}
}
}
// Verify verify
func Verify(verify bool) Option {
return func(o *Options) {
o.Verify = verify
}
}
func Logf(f func(context.Context, Stat)) Option {
return func(o *Options) {
o.Logf = f
}
}
func LocalAddr(addr net.Addr) Option {
return func(o *Options) {
o.LocalAddr = addr
}
}
func Stream(stream func(int64, []byte) error) Option {
return func(o *Options) {
o.Stream = stream
}
}
func RequestEach(each ...func(context.Context, *http.Request) error) Option {
return func(o *Options) {
o.RequestEach = each
}
}
func ResponseEach(each ...func(context.Context, *http.Response) error) Option {
return func(o *Options) {
o.ResponseEach = each
}
}
// Hosts 自定义Host配置,参数只能在session级别生效,格式:<host:port>
// 如果存在proxy服务,只能解析代理服务,不能解析url地址
func Hosts(hosts map[string][]string) Option {
return func(o *Options) {
o.Hosts = hosts
}
}
// Proxy set proxy addr
// os.Setenv("HTTP_PROXY", "http://127.0.0.1:9743")
// os.Setenv("HTTPS_PROXY", "https://127.0.0.1:9743")
// https://stackoverflow.com/questions/14661511/setting-up-proxy-for-http-client
func Proxy(addr string) Option {
return func(o *Options) {
if proxyURL, err := url.Parse(addr); err == nil {
o.Proxy = http.ProxyURL(proxyURL)
}
}
}
// Copy options
func (opt Options) Copy() Options {
options := newOptions()
options.Method = opt.Method
options.URL = opt.URL
options.Path = append(options.Path, opt.Path...)
options.Cookies = append(options.Cookies, opt.Cookies...)
options.body = opt.body
options.Timeout = opt.Timeout
options.MaxConns = opt.MaxConns
options.TraceLv = opt.TraceLv
options.Verify = opt.Verify
options.Logf = opt.Logf
options.LocalAddr = opt.LocalAddr
options.Stream = opt.Stream
for k, v := range opt.Params {
options.Params[k] = v
}
for k, v := range opt.Header {
options.Header.Add(k, v[0])
}
options.RequestEach = append(options.RequestEach, opt.RequestEach...)
options.ResponseEach = append(options.ResponseEach, opt.ResponseEach...)
return options
}