-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.go
More file actions
100 lines (86 loc) · 2.76 KB
/
stdlib.go
File metadata and controls
100 lines (86 loc) · 2.76 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
package httpc
import (
"context"
"io"
"net/http"
)
// --- 标准库兼容方法 (使用 RequestBuilder 重构) ---
// NewRequest 创建请求,支持与 http.NewRequest 兼容
func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
builder := c.NewRequestBuilder(method, urlStr).SetBody(body)
return builder.Build()
}
// Get 发送 GET 请求
func (c *Client) Get(url string) (*http.Response, error) {
return c.GET(url).Execute()
}
// GetContext 发送带 Context 的 GET 请求
func (c *Client) GetContext(ctx context.Context, url string) (*http.Response, error) {
return c.GET(url).WithContext(ctx).Execute()
}
// PostJSON 发送 JSON POST 请求
func (c *Client) PostJSON(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.POST(url)
_, err := builder.SetJSONBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// PostXML 发送 XML POST 请求
func (c *Client) PostXML(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.POST(url)
_, err := builder.SetXMLBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// PostGOB 发送 GOB POST 请求
func (c *Client) PostGOB(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.POST(url)
_, err := builder.SetGOBBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// PutJSON 发送 JSON PUT 请求
func (c *Client) PutJSON(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.PUT(url)
_, err := builder.SetJSONBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// PutXML 发送 XML PUT 请求
func (c *Client) PutXML(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.PUT(url)
_, err := builder.SetXMLBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// PutGOB 发送 GOB PUT 请求
func (c *Client) PutGOB(ctx context.Context, url string, body any) (*http.Response, error) {
builder := c.PUT(url)
_, err := builder.SetGOBBody(body)
if err != nil {
return nil, err
}
return builder.WithContext(ctx).Execute()
}
// Post 发送 POST 请求
func (c *Client) Post(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.POST(url).SetBody(body).WithContext(ctx).Execute()
}
// Put 发送 PUT 请求
func (c *Client) Put(ctx context.Context, url string, body io.Reader) (*http.Response, error) {
return c.PUT(url).SetBody(body).WithContext(ctx).Execute()
}
// Delete 发送 DELETE 请求
func (c *Client) Delete(ctx context.Context, url string) (*http.Response, error) {
return c.DELETE(url).WithContext(ctx).Execute()
}