-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_common.go
More file actions
352 lines (301 loc) · 7.87 KB
/
http_common.go
File metadata and controls
352 lines (301 loc) · 7.87 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
// Copyright 2025 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpclient
import (
"io"
"net/http"
"net/url"
)
type (
// Encoder is used to encode the data by the content type to dst.
Encoder func(dst io.Writer, contentType string, data any) error
// Handler is used to handle the response.
Handler func(dst any, resp *http.Response) error
)
type respHandler struct {
All Handler
H1xx Handler
H2xx Handler
H3xx Handler
H4xx Handler
H5xx Handler
Default Handler
}
type common[T any] struct {
target *T
hook Hook
query url.Values
header http.Header
onresp func(*Response)
handler respHandler
encoder Encoder
client Doer
ignore404 bool
clonehook bool
clonequery bool
cloneheader bool
}
func newCommon[T any](target *T) common[T] {
c := common[T]{
target: target,
header: make(http.Header, 4),
query: make(url.Values, 4),
}
c.SetHTTPClient(http.DefaultClient)
c.SetBodyEncoder(EncodeData)
c.OnResponse(logOnResponse)
return c
}
func copyCommon(dst *common[Request], src *common[Client]) {
dst.hook = src.hook
dst.query = src.query
dst.header = src.header
dst.client = src.client
dst.onresp = src.onresp
dst.handler = src.handler
dst.encoder = src.encoder
dst.ignore404 = src.ignore404
dst.clonehook = true
dst.clonequery = true
dst.cloneheader = true
}
func (c common[T]) clone(target *T) common[T] {
_c := c
_c.hook = cloneHook(c.hook)
_c.query = cloneQuery(c.query)
_c.header = c.header.Clone()
_c.target = target
return _c
}
func (c *common[T]) cloneQuery() {
if c.clonequery {
c.query = cloneQuery(c.query)
c.clonequery = false
}
}
func (c *common[T]) cloneHeader() {
if c.cloneheader {
c.header = c.header.Clone()
c.cloneheader = false
}
}
// Ignore404 sets whether to ignore the status code 404.
// If true, the 4xx response handler won't be called
// when the http server returns 404.
//
// Default: false
func (c *common[T]) Ignore404(ignore bool) *T {
c.ignore404 = ignore
return c.target
}
// OnResponse sets a callback function to wrap the response,
// which can be used to log the request and response result.
//
// For the default, it will log the method, url, status code and cost duration.
func (c *common[T]) OnResponse(f func(*Response)) *T {
c.onresp = f
return c.target
}
// GetHTTPClient returns the inner http client.
func (c *common[T]) GetHTTPClient() Doer {
return c.client
}
// SetHTTPClient resets the http client.
func (c *common[T]) SetHTTPClient(client Doer) *T {
c.client = client
return c.target
}
// SetHook resets the request hook.
func (c *common[T]) SetHook(hook Hook) *T {
c.clonehook = false
c.hook = hook
return c.target
}
// AddHook appends the request hook.
func (c *common[T]) AddHook(hook Hook) *T {
if hook == nil {
panic("AddHook: the hook must not be nil")
}
switch hooks := c.hook.(type) {
case nil:
c.hook = hook
case Hooks:
if c.clonehook {
_hooks := make(Hooks, 0, len(hooks)+1)
_hooks = append(_hooks, hooks...)
_hooks = append(_hooks, hook)
c.SetHook(_hooks)
} else {
c.hook = append(hooks, hook)
}
default:
c.SetHook(Hooks{c.hook, hook})
}
return c.target
}
// AddQueries adds the request queries.
func (c *common[T]) AddQueries(queries url.Values) *T {
if len(queries) == 0 {
return c.target
}
c.cloneQuery()
for key, values := range queries {
c.query[key] = values
}
return c.target
}
// AddQueryMap adds the request queries as a map type.
func (c *common[T]) AddQueryMap(queries map[string]string) *T {
if len(queries) == 0 {
return c.target
}
c.cloneQuery()
for key, value := range queries {
c.query.Add(key, value)
}
return c.target
}
// AddQuery appends the value for the query key.
func (c *common[T]) AddQuery(key, value string) *T {
c.cloneQuery()
c.query.Add(key, value)
return c.target
}
// SetQuery sets the query key to the value.
func (c *common[T]) SetQuery(key, value string) *T {
c.cloneQuery()
c.query.Set(key, value)
return c.target
}
// Header returns the inner header.
func (c *common[T]) Header() http.Header {
return c.header
}
// AddHeaders adds the request headers.
func (c *common[T]) AddHeaders(headers http.Header) *T {
if len(headers) == 0 {
return c.target
}
c.cloneQuery()
for key, values := range headers {
c.header[key] = values
}
return c.target
}
// AddHeaderMap adds the request headers as a map type.
func (c *common[T]) AddHeaderMap(headers map[string]string) *T {
if len(headers) == 0 {
return c.target
}
c.cloneQuery()
for key, value := range headers {
c.header.Add(key, value)
}
return c.target
}
// AddHeader adds the default request header as "key: value".
func (c *common[T]) AddHeader(key, value string) *T {
c.cloneQuery()
c.header.Add(key, value)
return c.target
}
// SetHeader sets the default request header as "key: value".
func (c *common[T]) SetHeader(key, value string) *T {
c.cloneQuery()
c.header.Set(key, value)
return c.target
}
// SetContentType sets the default Content-Type, which is equal to
// SetHeader("Content-Type", ct).
//
// Default: "application/json; charset=UTF-8"
func (c *common[T]) SetContentType(ct string) *T {
return c.SetHeader("Content-Type", ct)
}
// SetAccepts resets the accepted types of the response body to accepts.
func (c *common[T]) SetAccepts(accepts ...string) *T {
if len(accepts) == 0 {
return c.target
}
c.cloneHeader()
c.header["Accept"] = accepts
return c.target
}
// AddAccept adds the accepted types of the response body, which is equal to
// AddHeader("Accept", contentType).
func (c *common[T]) AddAccept(contentType string) *T {
return c.AddHeader("Accept", contentType)
}
// SetBodyEncoder sets the encoder to encode the request body.
//
// Default: EncodeData.
func (c *common[T]) SetBodyEncoder(encoder Encoder) *T {
c.encoder = encoder
return c.target
}
// ClearAllResponseHandlers clears all the set response handlers.
func (c *common[T]) ClearAllResponseHandlers() *T {
c.handler = respHandler{}
return c.target
}
// SetResponseHandler sets the handler of the response, which hides
// all the XXX handlers if set, but you can set it to nil to cancel it.
//
// Default: nil
func (c *common[T]) SetResponseHandler(handler Handler) *T {
c.handler.All = handler
return c.target
}
// SetResponseHandler1xx sets the handler of the response status code 1xx.
//
// Default: nil
func (c *common[T]) SetResponseHandler1xx(handler Handler) *T {
c.handler.H1xx = handler
return c.target
}
// SetResponseHandler2xx sets the handler of the response status code 2xx.
//
// Default: DecodeResponseBody
func (c *common[T]) SetResponseHandler2xx(handler Handler) *T {
c.handler.H2xx = handler
return c.target
}
// SetResponseHandler3xx sets the handler of the response status code 3xx.
//
// Default: nil
func (c *common[T]) SetResponseHandler3xx(handler Handler) *T {
c.handler.H3xx = handler
return c.target
}
// SetResponseHandler4xx sets the handler of the response status code 4xx.
//
// Default: nil
func (c *common[T]) SetResponseHandler4xx(handler Handler) *T {
c.handler.H4xx = handler
return c.target
}
// SetResponseHandler5xx sets the handler of the response status code 5xx.
//
// Default: nil
func (c *common[T]) SetResponseHandler5xx(handler Handler) *T {
c.handler.H5xx = handler
return c.target
}
// SetResponseHandlerDefault sets the default handler of the response.
//
// Default: ReadResponseBodyAsError
func (c *common[T]) SetResponseHandlerDefault(handler Handler) *T {
c.handler.Default = handler
return c.target
}