-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
315 lines (271 loc) · 8.12 KB
/
client.go
File metadata and controls
315 lines (271 loc) · 8.12 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
package ginx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"reflect"
"sort"
"strings"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"github.com/go-courier/reflectx"
"github.com/shrewx/ginx/pkg/statuserror"
"github.com/spf13/cast"
)
const (
Query = "query"
Path = "path"
Form = "form"
UrlEncode = "urlencoded"
Multipart = "multipart"
Body = "body"
Head = "header"
Cookies = "cookies"
)
const DefaultTimeout = 60 * time.Second
// Client 底层 HTTP 客户端
type Client struct{}
type MultipartFile struct {
Filename string
Header textproto.MIMEHeader
Data io.Reader
}
// Invoke 执行请求(核心方法)
func (c *Client) Invoke(ctx context.Context, req interface{}, config RequestConfig) (ResponseBind, error) {
// 2. 构建 HTTP 请求
// 如果 req 已经是 *http.Request,直接使用;否则通过 NewRequest 构建
var httpReq *http.Request
var err error
if httpRequest, ok := req.(*http.Request); ok {
httpReq = httpRequest
} else {
httpReq, err = NewRequest(ctx, req, config)
if err != nil {
return nil, err
}
}
return InvokeRequest(ctx, httpReq, config.Timeout, config.Transport)
}
func InvokeRequest(ctx context.Context, httpReq *http.Request, timeout *time.Duration, transport *http.Transport) (ResponseBind, error) {
// 1. 获取或创建 HTTP Client
httpClient := getHTTPClient(timeout, transport)
// 2. 注入 OpenTelemetry 追踪信息
if ctxReq, ok := ctx.Value(RequestContextKey).(*http.Request); ok {
otel.GetTextMapPropagator().Inject(ctxReq.Context(), propagation.HeaderCarrier(httpReq.Header))
}
// 3. 执行请求
resp, err := httpClient.Do(httpReq)
if err != nil {
logrus.Errorf("http client error: %v", err)
return nil, err
}
return &Result{Response: resp}, nil
}
func NewRequest(ctx context.Context, req interface{}, config RequestConfig) (*http.Request, error) {
if ctx == nil {
ctx = context.Background()
}
method := ""
if methodDescriber, ok := req.(MethodDescriber); ok {
method = methodDescriber.Method()
}
path := ""
if pathDescriber, ok := req.(PathDescriber); ok {
path = pathDescriber.Path()
}
host := config.Host
if config.Port != 0 {
host = fmt.Sprintf("%s:%d", config.Host, config.Port)
}
u := url.URL{
Scheme: config.Schema,
Host: host,
Path: path,
}
request, err := newRequestWithContext(ctx, method, u.String(), req)
if err != nil {
return nil, err
}
applyRequestConfig(request, config)
return request, nil
}
// newRequestWithContext 根据结构体字段标签构建HTTP请求
// 这是客户端的核心函数,负责解析结构体字段的in标签,
// 并将字段值绑定到HTTP请求的不同部分(header、query、body等)
// 支持多种数据格式:JSON、表单、multipart、URL编码等
func newRequestWithContext(ctx context.Context, method string, rawUrl string, v interface{}) (*http.Request, error) {
header := http.Header{}
// 从上下文获取语言设置,支持国际化
lang, ok := ctx.Value(CurrentLangHeader()).(string)
if ok {
header.Add(CurrentLangHeader(), lang)
} else {
header.Add(CurrentLangHeader(), I18nZH)
}
// 处理空请求体的情况
if v == nil {
req, err := http.NewRequestWithContext(ctx, method, rawUrl, nil)
if err != nil {
return nil, err
}
req.Header = header
return req, nil
}
// 初始化各种参数容器
query := url.Values{} // 查询参数
cookies := url.Values{} // Cookie参数
body := new(bytes.Buffer) // 请求体
writer := multipart.NewWriter(body) // multipart表单写入器
// 获取反射值和类型信息
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
rv = reflectx.Indirect(rv)
rt := reflectx.Deref(reflect.TypeOf(v))
var closeWriter bool // 标记是否需要关闭multipart写入器
// 遍历结构体字段,根据in标签进行参数绑定
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
if in, ok := field.Tag.Lookup("in"); ok {
// 确定参数名称:优先使用name标签,其次json标签,最后使用小写字段名
name := field.Tag.Get("name")
if name == "" {
name = field.Tag.Get("json")
}
if name == "" {
name = strings.ToLower(field.Name[:1]) + field.Name[1:]
}
// 根据in标签值进行不同的参数绑定
switch in {
case Head: // HTTP头部
header[textproto.CanonicalMIMEHeaderKey(name)] = append(header[textproto.CanonicalMIMEHeaderKey(name)], cast.ToString(rv.Field(i).Interface()))
case Cookies: // Cookie
cookies[name] = append(cookies[name], cast.ToString(rv.Field(i).Interface()))
case Query: // URL查询参数
query.Add(name, cast.ToString(rv.Field(i).Interface()))
case Path: // 路径参数(替换URL中的占位符)
rawUrl = strings.Replace(rawUrl, fmt.Sprintf(":%s", name), cast.ToString(rv.Field(i).Interface()), -1)
case Body: // JSON请求体
data, _ := json.Marshal(rv.Field(i).Interface())
body.Write(data)
header.Set("Content-Type", MineApplicationJson)
case UrlEncode: // URL编码表单数据
query.Add(name, rv.Field(i).String())
header.Set("Content-Type", MineApplicationUrlencoded)
case Form, Multipart: // 表单或multipart数据
switch typ := rv.Field(i).Interface().(type) {
case MultipartFile: // 单个文件上传
part, err := writer.CreateFormFile(name, typ.Filename)
if err != nil {
return nil, err
}
if _, err := io.Copy(part, typ.Data); err != nil {
return nil, err
}
case []MultipartFile: // 多文件上传
for _, f := range typ {
part, err := writer.CreateFormFile(name, f.Filename)
if err != nil {
return nil, err
}
if _, err := io.Copy(part, f.Data); err != nil {
return nil, err
}
}
default: // 普通表单字段
writer.WriteField(name, cast.ToString(rv.Field(i).Interface()))
}
header.Set("Content-Type", writer.FormDataContentType())
closeWriter = true // 标记需要关闭写入器
}
}
}
// 关闭multipart写入器(关键!否则内容长度会不匹配导致panic)
if closeWriter {
// necessary !!!! otherwise the length of the content is shorter than the length of the body !!!! panic
if writer != nil {
err := writer.Close()
if err != nil {
return nil, err
}
}
}
// 处理查询参数:如果是URL编码格式,放入body;否则放入URL
var rawQuery string
if len(query) > 0 {
if header.Get("Content-Type") == MineApplicationUrlencoded {
body = bytes.NewBufferString(query.Encode())
} else {
rawQuery = query.Encode()
}
}
// 构建最终的HTTP请求
req, err := http.NewRequestWithContext(ctx, method, rawUrl, body)
if err != nil {
return nil, err
}
req.URL.RawQuery = rawQuery
req.Header = header
// 添加Cookie(按名称排序以确保一致性)
if n := len(cookies); n > 0 {
names := make([]string, n)
i := 0
for name := range cookies {
names[i] = name
i++
}
sort.Strings(names)
for _, name := range names {
values := cookies[name]
for i := range values {
req.AddCookie(&http.Cookie{
Name: name,
Value: values[i],
})
}
}
}
return req, nil
}
type Result struct {
Response *http.Response
}
func (r *Result) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}
func (r *Result) Bind(body interface{}) error {
defer func() {
if r.Response != nil && r.Response.Body != nil {
r.Response.Body.Close()
}
}()
data, err := io.ReadAll(r.Response.Body)
if err != nil {
return err
}
if isOk(r.Response.StatusCode) {
return json.Unmarshal(data, body)
}
statusErr := &statuserror.StatusErr{}
err = json.Unmarshal(data, statusErr)
// 如果解析失败或返回空结构体,返回 RemoteHTTPError
if err != nil || statusErr.K == "" && statusErr.ErrorCode == 0 && statusErr.Message == "" {
return NewRemoteHTTPError(r.Response.StatusCode, r.Response.Header, data, r.Response.Header.Get("Content-Type"))
}
return statusErr
}
func isOk(code int) bool {
return code >= http.StatusOK && code < http.StatusMultipleChoices
}