|
|
@@ -1,48 +1,301 @@
|
|
1
|
1
|
package factory
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
|
4
|
+ "encoding/base64"
|
|
4
|
5
|
"fmt"
|
|
5
|
6
|
"net/http"
|
|
|
7
|
+ "sync"
|
|
6
|
8
|
"time"
|
|
7
|
9
|
|
|
8
|
10
|
"git.x2erp.com/qdy/go-base/config"
|
|
|
11
|
+ "github.com/go-resty/resty/v2"
|
|
9
|
12
|
)
|
|
10
|
13
|
|
|
|
14
|
+// HTTPClient 简化的HTTP客户端
|
|
|
15
|
+type HTTPClient struct {
|
|
|
16
|
+ client *resty.Client
|
|
|
17
|
+}
|
|
|
18
|
+
|
|
11
|
19
|
// HTTPFactory HTTP客户端工厂
|
|
12
|
20
|
type HTTPFactory struct {
|
|
13
|
21
|
config config.IConfig
|
|
14
|
|
- client *http.Client
|
|
|
22
|
+ client *resty.Client
|
|
|
23
|
+}
|
|
|
24
|
+
|
|
|
25
|
+var (
|
|
|
26
|
+ httpFactoryInstance *HTTPFactory
|
|
|
27
|
+ httpFactoryOnce sync.Once
|
|
|
28
|
+ httpFactoryInitErr error
|
|
|
29
|
+)
|
|
|
30
|
+
|
|
|
31
|
+// GetHTTPFactory 获取HTTP工厂单例实例
|
|
|
32
|
+func GetHTTPFactory() (*HTTPFactory, error) {
|
|
|
33
|
+ httpFactoryOnce.Do(func() {
|
|
|
34
|
+ cfg := config.GetConfig()
|
|
|
35
|
+
|
|
|
36
|
+ if err := config.GetInitError(); err != nil {
|
|
|
37
|
+ httpFactoryInitErr = fmt.Errorf("failed to load config: %v", err)
|
|
|
38
|
+ return
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ httpConfig := cfg.GetHTTP()
|
|
|
42
|
+
|
|
|
43
|
+ // 创建共享的resty客户端(线程安全)
|
|
|
44
|
+ client := resty.New()
|
|
|
45
|
+ client.SetTimeout(time.Duration(httpConfig.Timeout) * time.Second)
|
|
|
46
|
+ client.SetTransport(&http.Transport{
|
|
|
47
|
+ MaxIdleConns: httpConfig.MaxIdleConns,
|
|
|
48
|
+ MaxIdleConnsPerHost: httpConfig.MaxIdleConnsPerHost,
|
|
|
49
|
+ IdleConnTimeout: time.Duration(httpConfig.IdleConnTimeout) * time.Second,
|
|
|
50
|
+ MaxConnsPerHost: httpConfig.MaxConnsPerHost,
|
|
|
51
|
+ DisableCompression: httpConfig.DisableCompression,
|
|
|
52
|
+ DisableKeepAlives: httpConfig.DisableKeepAlives,
|
|
|
53
|
+ })
|
|
|
54
|
+
|
|
|
55
|
+ // 设置默认headers
|
|
|
56
|
+ client.SetHeaders(map[string]string{
|
|
|
57
|
+ "Content-Type": "application/json",
|
|
|
58
|
+ "Accept": "application/json",
|
|
|
59
|
+ })
|
|
|
60
|
+
|
|
|
61
|
+ httpFactoryInstance = &HTTPFactory{
|
|
|
62
|
+ config: cfg,
|
|
|
63
|
+ client: client,
|
|
|
64
|
+ }
|
|
|
65
|
+ })
|
|
|
66
|
+
|
|
|
67
|
+ return httpFactoryInstance, httpFactoryInitErr
|
|
|
68
|
+}
|
|
|
69
|
+
|
|
|
70
|
+// CreateClient 创建HTTP客户端包装器(共享底层resty.Client)
|
|
|
71
|
+func (f *HTTPFactory) CreateClient() *HTTPClient {
|
|
|
72
|
+ return &HTTPClient{
|
|
|
73
|
+ client: f.client, // 共享同一个线程安全的客户端
|
|
|
74
|
+ }
|
|
|
75
|
+}
|
|
|
76
|
+
|
|
|
77
|
+// CreateClient 全局便捷函数:创建HTTP客户端
|
|
|
78
|
+func CreateClient() (*HTTPClient, error) {
|
|
|
79
|
+ factory, err := GetHTTPFactory()
|
|
|
80
|
+ if err != nil {
|
|
|
81
|
+ return nil, err
|
|
|
82
|
+ }
|
|
|
83
|
+ return factory.CreateClient(), nil
|
|
|
84
|
+}
|
|
|
85
|
+
|
|
|
86
|
+// GetInitError 获取初始化错误(如果有)
|
|
|
87
|
+func GetInitError() error {
|
|
|
88
|
+ return httpFactoryInitErr
|
|
|
89
|
+}
|
|
|
90
|
+
|
|
|
91
|
+// ==================== HTTPClient 方法 ====================
|
|
|
92
|
+
|
|
|
93
|
+// SetAuthToken 设置认证token(请求级别)
|
|
|
94
|
+func (c *HTTPClient) SetAuthToken(token string) *HTTPClient {
|
|
|
95
|
+ // 注意:这里设置的是客户端级别的token,会影响所有使用该客户端的请求
|
|
|
96
|
+ // 对于多用户场景,应该在请求级别设置
|
|
|
97
|
+ c.client.SetAuthToken(token)
|
|
|
98
|
+ return c
|
|
15
|
99
|
}
|
|
16
|
100
|
|
|
17
|
|
-// NewHTTPFactory 创建HTTP客户端工厂
|
|
18
|
|
-func NewHTTPFactory() (*HTTPFactory, error) {
|
|
19
|
|
- cfg := config.GetConfig()
|
|
|
101
|
+// SetBearerToken 设置Bearer Token认证头(请求级别)
|
|
|
102
|
+func (c *HTTPClient) SetBearerToken(token string) *HTTPClient {
|
|
|
103
|
+ if token != "" {
|
|
|
104
|
+ c.client.SetHeader("Authorization", "Bearer "+token)
|
|
|
105
|
+ } else {
|
|
|
106
|
+ c.client.Header.Del("Authorization")
|
|
|
107
|
+ }
|
|
|
108
|
+ return c
|
|
|
109
|
+}
|
|
20
|
110
|
|
|
21
|
|
- if err := config.GetInitError(); err != nil {
|
|
22
|
|
- return nil, fmt.Errorf("failed to load config: %v", err)
|
|
|
111
|
+// SetBasicAuth 设置Basic认证头(请求级别)
|
|
|
112
|
+func (c *HTTPClient) SetBasicAuth(username, password string) *HTTPClient {
|
|
|
113
|
+ if username != "" && password != "" {
|
|
|
114
|
+ auth := username + ":" + password
|
|
|
115
|
+ token := base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
116
|
+ c.client.SetHeader("Authorization", "Basic "+token)
|
|
|
117
|
+ } else {
|
|
|
118
|
+ c.client.Header.Del("Authorization")
|
|
23
|
119
|
}
|
|
|
120
|
+ return c
|
|
|
121
|
+}
|
|
|
122
|
+
|
|
|
123
|
+// SetJSONContentType 设置Content-Type为application/json
|
|
|
124
|
+func (c *HTTPClient) SetJSONContentType() *HTTPClient {
|
|
|
125
|
+ c.client.SetHeader("Content-Type", "application/json")
|
|
|
126
|
+ return c
|
|
|
127
|
+}
|
|
|
128
|
+
|
|
|
129
|
+// SetHeader 设置请求头
|
|
|
130
|
+func (c *HTTPClient) SetHeader(key, value string) *HTTPClient {
|
|
|
131
|
+ c.client.SetHeader(key, value)
|
|
|
132
|
+ return c
|
|
|
133
|
+}
|
|
|
134
|
+
|
|
|
135
|
+// SetHeaders 批量设置请求头
|
|
|
136
|
+func (c *HTTPClient) SetHeaders(headers map[string]string) *HTTPClient {
|
|
|
137
|
+ c.client.SetHeaders(headers)
|
|
|
138
|
+ return c
|
|
|
139
|
+}
|
|
24
|
140
|
|
|
25
|
|
- client := &http.Client{
|
|
26
|
|
- Timeout: 30 * time.Second,
|
|
27
|
|
- Transport: &http.Transport{
|
|
28
|
|
- MaxIdleConns: 100,
|
|
29
|
|
- MaxIdleConnsPerHost: 10,
|
|
30
|
|
- IdleConnTimeout: 90 * time.Second,
|
|
31
|
|
- },
|
|
|
141
|
+// SetAuthHeaders 设置认证相关的headers
|
|
|
142
|
+func (c *HTTPClient) SetAuthHeaders(token string) *HTTPClient {
|
|
|
143
|
+ return c.SetJSONContentType().SetBearerToken(token)
|
|
|
144
|
+}
|
|
|
145
|
+
|
|
|
146
|
+// SetBasicAuthHeaders 设置Basic认证相关的headers
|
|
|
147
|
+func (c *HTTPClient) SetBasicAuthHeaders(username, password string) *HTTPClient {
|
|
|
148
|
+ return c.SetJSONContentType().SetBasicAuth(username, password)
|
|
|
149
|
+}
|
|
|
150
|
+
|
|
|
151
|
+// Post 发送POST请求(在请求级别设置认证信息,避免多用户相互影响)
|
|
|
152
|
+func (c *HTTPClient) Post(url string, data interface{}) (*resty.Response, error) {
|
|
|
153
|
+ return c.client.R().SetBody(data).Post(url)
|
|
|
154
|
+}
|
|
|
155
|
+
|
|
|
156
|
+// PostWithResult 发送POST请求并解析结果
|
|
|
157
|
+func (c *HTTPClient) PostWithResult(url string, data interface{}, result interface{}) (*resty.Response, error) {
|
|
|
158
|
+ return c.client.R().SetBody(data).SetResult(result).Post(url)
|
|
|
159
|
+}
|
|
|
160
|
+
|
|
|
161
|
+// PostWithAuth 发送带Bearer Token的POST请求(推荐用于多用户场景)
|
|
|
162
|
+func (c *HTTPClient) PostWithAuth(url string, data interface{}, token string, result interface{}) (*resty.Response, error) {
|
|
|
163
|
+ req := c.client.R().SetBody(data)
|
|
|
164
|
+ if token != "" {
|
|
|
165
|
+ req.SetHeader("Authorization", "Bearer "+token)
|
|
32
|
166
|
}
|
|
|
167
|
+ if result != nil {
|
|
|
168
|
+ req.SetResult(result)
|
|
|
169
|
+ }
|
|
|
170
|
+ return req.Post(url)
|
|
|
171
|
+}
|
|
33
|
172
|
|
|
34
|
|
- return &HTTPFactory{
|
|
35
|
|
- config: cfg,
|
|
36
|
|
- client: client,
|
|
37
|
|
- }, nil
|
|
|
173
|
+// Get 发送GET请求
|
|
|
174
|
+func (c *HTTPClient) Get(url string) (*resty.Response, error) {
|
|
|
175
|
+ return c.client.R().Get(url)
|
|
38
|
176
|
}
|
|
39
|
177
|
|
|
40
|
|
-// CreateHTTPClient 创建HTTP客户端
|
|
41
|
|
-func (f *HTTPFactory) CreateHTTPClient() *http.Client {
|
|
42
|
|
- return f.client
|
|
|
178
|
+// GetWithResult 发送GET请求并解析结果
|
|
|
179
|
+func (c *HTTPClient) GetWithResult(url string, result interface{}) (*resty.Response, error) {
|
|
|
180
|
+ return c.client.R().SetResult(result).Get(url)
|
|
43
|
181
|
}
|
|
44
|
182
|
|
|
45
|
|
-// GetConfig 获取配置信息
|
|
46
|
|
-func (f *HTTPFactory) GetConfig() config.IConfig {
|
|
47
|
|
- return f.config
|
|
|
183
|
+// GetWithAuth 发送带Bearer Token的GET请求(推荐用于多用户场景)
|
|
|
184
|
+func (c *HTTPClient) GetWithAuth(url string, token string, result interface{}) (*resty.Response, error) {
|
|
|
185
|
+ req := c.client.R()
|
|
|
186
|
+ if token != "" {
|
|
|
187
|
+ req.SetHeader("Authorization", "Bearer "+token)
|
|
|
188
|
+ }
|
|
|
189
|
+ if result != nil {
|
|
|
190
|
+ req.SetResult(result)
|
|
|
191
|
+ }
|
|
|
192
|
+ return req.Get(url)
|
|
|
193
|
+}
|
|
|
194
|
+
|
|
|
195
|
+// Put 发送PUT请求
|
|
|
196
|
+func (c *HTTPClient) Put(url string, data interface{}) (*resty.Response, error) {
|
|
|
197
|
+ return c.client.R().SetBody(data).Put(url)
|
|
|
198
|
+}
|
|
|
199
|
+
|
|
|
200
|
+// PutWithResult 发送PUT请求并解析结果
|
|
|
201
|
+func (c *HTTPClient) PutWithResult(url string, data interface{}, result interface{}) (*resty.Response, error) {
|
|
|
202
|
+ return c.client.R().SetBody(data).SetResult(result).Put(url)
|
|
|
203
|
+}
|
|
|
204
|
+
|
|
|
205
|
+// Delete 发送DELETE请求
|
|
|
206
|
+func (c *HTTPClient) Delete(url string) (*resty.Response, error) {
|
|
|
207
|
+ return c.client.R().Delete(url)
|
|
|
208
|
+}
|
|
|
209
|
+
|
|
|
210
|
+// DeleteWithResult 发送DELETE请求并解析结果
|
|
|
211
|
+func (c *HTTPClient) DeleteWithResult(url string, result interface{}) (*resty.Response, error) {
|
|
|
212
|
+ return c.client.R().SetResult(result).Delete(url)
|
|
|
213
|
+}
|
|
|
214
|
+
|
|
|
215
|
+// ==================== 便捷函数(修正版) ====================
|
|
|
216
|
+
|
|
|
217
|
+// PostJSON 便捷函数:直接发送POST请求
|
|
|
218
|
+func PostJSON(url string, data interface{}, result interface{}) (*resty.Response, error) {
|
|
|
219
|
+ client, err := CreateClient()
|
|
|
220
|
+ if err != nil {
|
|
|
221
|
+ return nil, err
|
|
|
222
|
+ }
|
|
|
223
|
+
|
|
|
224
|
+ if result != nil {
|
|
|
225
|
+ return client.PostWithResult(url, data, result)
|
|
|
226
|
+ }
|
|
|
227
|
+ return client.Post(url, data)
|
|
|
228
|
+}
|
|
|
229
|
+
|
|
|
230
|
+// PostJSONWithBearerToken 便捷函数:使用Bearer Token发送POST请求
|
|
|
231
|
+func PostJSONWithBearerToken(token, url string, data interface{}, result interface{}) (*resty.Response, error) {
|
|
|
232
|
+ client, err := CreateClient()
|
|
|
233
|
+ if err != nil {
|
|
|
234
|
+ return nil, err
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ return client.PostWithAuth(url, data, token, result)
|
|
|
238
|
+}
|
|
|
239
|
+
|
|
|
240
|
+// PostJSONWithBasicAuth 便捷函数:使用Basic认证发送POST请求
|
|
|
241
|
+func PostJSONWithBasicAuth(username, password, url string, data interface{}, result interface{}) (*resty.Response, error) {
|
|
|
242
|
+ client, err := CreateClient()
|
|
|
243
|
+ if err != nil {
|
|
|
244
|
+ return nil, err
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ // 为这个特定请求设置Basic Auth
|
|
|
248
|
+ req := client.client.R().SetBody(data)
|
|
|
249
|
+ if username != "" && password != "" {
|
|
|
250
|
+ auth := username + ":" + password
|
|
|
251
|
+ token := base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
252
|
+ req.SetHeader("Authorization", "Basic "+token)
|
|
|
253
|
+ }
|
|
|
254
|
+ if result != nil {
|
|
|
255
|
+ req.SetResult(result)
|
|
|
256
|
+ }
|
|
|
257
|
+ return req.Post(url)
|
|
|
258
|
+}
|
|
|
259
|
+
|
|
|
260
|
+// GetJSON 便捷函数:直接发送GET请求
|
|
|
261
|
+func GetJSON(url string, result interface{}) (*resty.Response, error) {
|
|
|
262
|
+ client, err := CreateClient()
|
|
|
263
|
+ if err != nil {
|
|
|
264
|
+ return nil, err
|
|
|
265
|
+ }
|
|
|
266
|
+
|
|
|
267
|
+ if result != nil {
|
|
|
268
|
+ return client.GetWithResult(url, result)
|
|
|
269
|
+ }
|
|
|
270
|
+ return client.Get(url)
|
|
|
271
|
+}
|
|
|
272
|
+
|
|
|
273
|
+// GetJSONWithBearerToken 便捷函数:使用Bearer Token发送GET请求
|
|
|
274
|
+func GetJSONWithBearerToken(token, url string, result interface{}) (*resty.Response, error) {
|
|
|
275
|
+ client, err := CreateClient()
|
|
|
276
|
+ if err != nil {
|
|
|
277
|
+ return nil, err
|
|
|
278
|
+ }
|
|
|
279
|
+
|
|
|
280
|
+ return client.GetWithAuth(url, token, result)
|
|
|
281
|
+}
|
|
|
282
|
+
|
|
|
283
|
+// GetJSONWithBasicAuth 便捷函数:使用Basic认证发送GET请求
|
|
|
284
|
+func GetJSONWithBasicAuth(username, password, url string, result interface{}) (*resty.Response, error) {
|
|
|
285
|
+ client, err := CreateClient()
|
|
|
286
|
+ if err != nil {
|
|
|
287
|
+ return nil, err
|
|
|
288
|
+ }
|
|
|
289
|
+
|
|
|
290
|
+ // 为这个特定请求设置Basic Auth
|
|
|
291
|
+ req := client.client.R()
|
|
|
292
|
+ if username != "" && password != "" {
|
|
|
293
|
+ auth := username + ":" + password
|
|
|
294
|
+ token := base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
295
|
+ req.SetHeader("Authorization", "Basic "+token)
|
|
|
296
|
+ }
|
|
|
297
|
+ if result != nil {
|
|
|
298
|
+ req.SetResult(result)
|
|
|
299
|
+ }
|
|
|
300
|
+ return req.Get(url)
|
|
48
|
301
|
}
|