/
session.go
344 lines (292 loc) · 9.23 KB
/
session.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
package requests
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"io"
"mime/multipart"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
const (
defaultUA = "jacexh/requests - a go client for human"
)
type (
// Any 可用于query/headers/data/files传参
Any map[string]any
Params struct {
Query Any
Data Any
Json interface{}
Files Any
Body []byte
Headers Any
}
Session struct {
client *http.Client
userAgent string
globalHeaders Any
beforeHooks []BeforeRequestHook
afterHooks []AfterRequestHook
}
// BeforeRequestHook 会在调用`Client.Do(*http.Request)`前调用,此时已经完成了parama的自动装填
BeforeRequestHook func(*http.Request, []byte)
// AfterRequestHook 会在完成`Client.Do(*http.Request)`后立即调用
AfterRequestHook func(*http.Response, error)
// ResponseRender 用于对response的反序列化
Unmarshaller func([]byte) error
)
func NewSession(opts ...Option) *Session {
jar, _ := cookiejar.New(nil)
s := &Session{
client: &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 1000,
IdleConnTimeout: 60 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Jar: jar,
Timeout: 30 * time.Second,
},
userAgent: defaultUA,
globalHeaders: make(Any),
beforeHooks: make([]BeforeRequestHook, 0),
afterHooks: make([]AfterRequestHook, 0),
}
s.Apply(opts...)
return s
}
func (s *Session) Apply(opts ...Option) {
for _, opt := range opts {
opt(s)
}
}
func (s *Session) renderBody(params Params, w io.Writer) (string, error) {
var err error
var contentType string
body:
switch {
case params.Body != nil:
_, err = w.Write(params.Body)
if err != nil {
break body
}
case params.Json != nil:
data, err := json.Marshal(params.Json)
if err != nil {
break body
}
contentType = "application/json"
if _, err = w.Write(data); err != nil {
break body
}
case params.Files != nil:
writer := multipart.NewWriter(w)
for k, v := range params.Data {
str, err := ToString(v)
if err != nil {
return "", err
}
if err := writer.WriteField(k, str); err != nil {
break body
}
}
for field, fp := range params.Files {
str, err := ToString(fp)
if err != nil {
return "", err
}
file, err := os.Open(str)
if err != nil {
break body
}
defer file.Close()
part, err := writer.CreateFormFile(field, filepath.Base(file.Name()))
if err != nil {
break body
}
_, err = io.Copy(part, file)
if err != nil {
break body
}
}
if err := writer.Close(); err != nil {
break body
}
contentType = writer.FormDataContentType()
case params.Data != nil:
values := url.Values{}
for k, v := range params.Data {
sv, err := ToString(v)
if err != nil {
return "", err
}
values.Set(k, sv)
}
_, err = w.Write([]byte(values.Encode()))
contentType = "application/x-www-form-urlencoded"
}
return contentType, err
}
func (s *Session) Request(method, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
buff := GetBuffer()
defer PutBuffer(buff)
req, err := s.Prepare(context.Background(), method, path, params, buff)
if err != nil {
return nil, nil, err
}
return s.Send(req, unmarshaller)
}
func (s *Session) Prepare(ctx context.Context, method, path string, params Params, buff *bytes.Buffer) (*http.Request, error) {
var err error
var autoContentType string
var req *http.Request
autoContentType, err = s.renderBody(params, buff)
if err != nil {
return nil, err
}
req, err = http.NewRequestWithContext(ctx, strings.ToUpper(method), path, buff)
if err != nil {
return nil, err
}
// begin to set headers
for k, v := range s.globalHeaders {
sv, err := ToString(v)
if err != nil {
return nil, err
}
req.Header.Set(k, sv)
}
req.Header.Set("User-Agent", s.userAgent)
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", autoContentType)
}
if params.Headers != nil {
for k, v := range params.Headers {
sv, err := ToString(v)
if err != nil {
return nil, err
}
req.Header.Set(k, sv)
}
}
if params.Query != nil {
query := req.URL.Query()
for k, v := range params.Query {
sv, err := ToString(v)
if err != nil {
return nil, err
}
query.Set(k, sv)
}
req.URL.RawQuery = query.Encode()
}
for _, hook := range s.beforeHooks {
hook(req, buff.Bytes())
}
return req, err
}
func (s *Session) Send(req *http.Request, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
buf := GetBuffer()
defer PutBuffer(buf)
var err error
var data []byte
res, err := s.client.Do(req)
for _, hook := range s.afterHooks {
hook(res, err)
}
if err != nil {
return nil, nil, err
}
_, err = io.Copy(buf, res.Body)
if err != nil {
return res, nil, err
}
_ = res.Body.Close()
data = buf.Bytes()
if unmarshaller != nil {
err = unmarshaller(data)
}
return res, data, err
}
func (s *Session) RequestWithContext(ctx context.Context, method, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
buff := GetBuffer()
defer PutBuffer(buff)
req, err := s.Prepare(ctx, method, path, params, buff)
if err != nil {
return nil, nil, err
}
res, data, err := s.Send(req, unmarshaller)
return res, data, err
}
func (s *Session) Get(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodGet, path, params, unmarshaller)
}
func (s *Session) GetWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodGet, path, params, unmarshaller)
}
func (s *Session) Post(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodPost, path, params, unmarshaller)
}
func (s *Session) PostWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodPost, path, params, unmarshaller)
}
func (s *Session) Put(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodPut, path, params, unmarshaller)
}
func (s *Session) PutWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodPut, path, params, unmarshaller)
}
func (s *Session) Patch(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodPatch, path, params, unmarshaller)
}
func (s *Session) PatchWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodPatch, path, params, unmarshaller)
}
func (s *Session) Delete(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodDelete, path, params, unmarshaller)
}
func (s *Session) DeleteWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodDelete, path, params, unmarshaller)
}
func (s *Session) Head(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodHead, path, params, unmarshaller)
}
func (s *Session) HeadWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodHead, path, params, unmarshaller)
}
func (s *Session) Connect(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodConnect, path, params, unmarshaller)
}
func (s *Session) ConnectWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodConnect, path, params, unmarshaller)
}
func (s *Session) Options(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodOptions, path, params, unmarshaller)
}
func (s *Session) OptionWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodOptions, path, params, unmarshaller)
}
func (s *Session) Trace(path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.Request(http.MethodTrace, path, params, unmarshaller)
}
func (s *Session) TraceWithContext(ctx context.Context, path string, params Params, unmarshaller Unmarshaller) (*http.Response, []byte, error) {
return s.RequestWithContext(ctx, http.MethodTrace, path, params, unmarshaller)
}