-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxhttp.go
More file actions
144 lines (125 loc) · 3.75 KB
/
xhttp.go
File metadata and controls
144 lines (125 loc) · 3.75 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
package xhttp
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type API interface {
Get(url string) *Response
Post(url string, body any) *Response
Patch(url string, body any) *Response
Put(url string, body any) *Response
Delete(url string, body any) *Response
GetCtx(ctx context.Context, url string) *Response
PostCtx(ctx context.Context, url string, body any) *Response
PatchCtx(ctx context.Context, url string, body any) *Response
PutCtx(ctx context.Context, url string, body any) *Response
DeleteCtx(ctx context.Context, url string, body any) *Response
}
func NewAPI(headerSetter HeaderSetter) API {
return &apiImpl{setter: headerSetter}
}
type apiImpl struct {
setter HeaderSetter
}
func (a *apiImpl) Get(url string) *Response {
return a.GetCtx(context.Background(), url)
}
func (a *apiImpl) Post(url string, body any) *Response {
return a.PostCtx(context.Background(), url, body)
}
func (a *apiImpl) Patch(url string, body any) *Response {
return a.PatchCtx(context.Background(), url, body)
}
func (a *apiImpl) Put(url string, body any) *Response {
return a.PutCtx(context.Background(), url, body)
}
func (a *apiImpl) Delete(url string, body any) *Response {
return a.DeleteCtx(context.Background(), url, body)
}
func (a *apiImpl) GetCtx(ctx context.Context, url string) *Response {
return sendRequest(ctx, http.MethodGet, url, nil, a.setter)
}
func (a *apiImpl) PostCtx(ctx context.Context, url string, body any) *Response {
return sendRequest(ctx, http.MethodPost, url, body, a.setter)
}
func (a *apiImpl) PatchCtx(ctx context.Context, url string, body any) *Response {
return sendRequest(ctx, http.MethodPatch, url, body, a.setter)
}
func (a *apiImpl) PutCtx(ctx context.Context, url string, body any) *Response {
return sendRequest(ctx, http.MethodPut, url, body, a.setter)
}
func (a *apiImpl) DeleteCtx(ctx context.Context, url string, body any) *Response {
return sendRequest(ctx, http.MethodDelete, url, body, a.setter)
}
// 发送HTTP请求的公共函数(每次创建新client)
func sendRequest(ctx context.Context, method, url string, body any, headerSetter HeaderSetter) *Response {
// 构建请求体
var reqBody io.Reader
switch v := body.(type) {
case io.Reader:
// 直接使用io.Reader(如multipart.Reader)文件上传body不是json
reqBody = v
default:
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return &Response{
body: nil,
err: fmt.Errorf("请求体JSON序列化失败: %v", err),
}
}
reqBody = bytes.NewBuffer(jsonData)
}
}
// 创建请求
req, err := http.NewRequestWithContext(ctx, method, url, reqBody)
if err != nil {
return &Response{
body: nil,
err: fmt.Errorf("创建请求失败: %v", err),
}
}
// 设置默认Content-Type(可被headerSetter覆盖)
if reqBody != nil && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
// 设置请求头
if headerSetter != nil {
headerSetter.SetHeaders(req)
}
client := &http.Client{}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return &Response{
body: nil,
err: fmt.Errorf("发送请求失败: %v", err),
}
}
defer resp.Body.Close()
// 读取响应体
bytesBody, err := io.ReadAll(resp.Body)
if err != nil {
return &Response{
body: nil,
err: fmt.Errorf("读取响应体失败: %v", err),
statusCode: resp.StatusCode,
}
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return &Response{
body: nil,
err: fmt.Errorf("请求失败,状态码: %d,响应体: %s", resp.StatusCode, string(bytesBody)),
statusCode: resp.StatusCode,
}
}
return &Response{
body: bytesBody,
err: err,
statusCode: resp.StatusCode,
}
}