-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
151 lines (121 loc) · 3.75 KB
/
request.go
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
package client
import (
"context"
"github.com/hopeio/cherry/utils/net/http/client"
"net/http"
"time"
)
// Request ...
type Request[RES any] client.Request
func New[RES any]() *Request[RES] {
return (*Request[RES])(client.New())
}
func NewRequest[RES any](url string, method string) *Request[RES] {
return (*Request[RES])(client.NewRequest(url, method))
}
func NewByRequest[RES any](req *client.Request) *Request[RES] {
return (*Request[RES])(req)
}
func NewGetRequest[RES any](url string) *Request[RES] {
return (*Request[RES])(client.NewGetRequest(url))
}
func (req *Request[RES]) Origin() *client.Request {
return (*client.Request)(req)
}
// Do create a HTTP request
func (r *Request[RES]) Do(req any) (*RES, error) {
response := new(RES)
err := (*client.Request)(r).Do(req, response)
return response, err
}
func (r *Request[RES]) Get(url string) (*RES, error) {
response := new(RES)
err := (*client.Request)(r).Get(url, response)
return response, err
}
func (r *Request[RES]) Post(url string, param any) (*RES, error) {
response := new(RES)
err := (*client.Request)(r).Post(url, param, response)
return response, err
}
func (req *Request[RES]) Context(ctx context.Context) *Request[RES] {
(*client.Request)(req).Context(ctx)
return req
}
func (req *Request[RES]) Url(url string) *Request[RES] {
(*client.Request)(req).Url(url)
return req
}
func (req *Request[RES]) Method(method string) *Request[RES] {
(*client.Request)(req).Method(method)
return req
}
func (req *Request[RES]) ContentType(contentType client.ContentType) *Request[RES] {
(*client.Request)(req).ContentType(contentType)
return req
}
func (req *Request[RES]) Header(header client.Header) *Request[RES] {
(*client.Request)(req).Header(header)
return req
}
func (req *Request[RES]) AddHeader(k, v string) *Request[RES] {
(*client.Request)(req).AddHeader(k, v)
return req
}
func (req *Request[RES]) CachedHeader(key string) *Request[RES] {
(*client.Request)(req).CachedHeader(key)
return req
}
func (req *Request[RES]) Logger(logger client.LogCallback) *Request[RES] {
(*client.Request)(req).Logger(logger)
return req
}
func (req *Request[RES]) DisableLog() *Request[RES] {
(*client.Request)(req).DisableLog()
return req
}
func (req *Request[RES]) LogLevel(lvl client.LogLevel) *Request[RES] {
(*client.Request)(req).LogLevel(lvl)
return req
}
func (req *Request[RES]) Tag(tag string) *Request[RES] {
(*client.Request)(req).Tag(tag)
return req
}
// handler 返回值:是否重试,返回数据,错误
func (req *Request[RES]) ResponseHandler(handler func(response *http.Response) (retry bool, data []byte, err error)) *Request[RES] {
(*client.Request)(req).ResponseHandler(handler)
return req
}
func (req *Request[RES]) Timeout(timeout time.Duration) *Request[RES] {
(*client.Request)(req).Timeout(timeout)
return req
}
func (req *Request[RES]) Client(c *http.Client) *Request[RES] {
(*client.Request)(req).Client(c)
return req
}
func (req *Request[RES]) RetryTimes(retryTimes int) *Request[RES] {
(*client.Request)(req).RetryTimes(retryTimes)
return req
}
func (req *Request[RES]) RetryTimesWithInterval(retryTimes int, retryInterval time.Duration) *Request[RES] {
(*client.Request)(req).RetryTimesWithInterval(retryTimes, retryInterval)
return req
}
func (req *Request[RES]) RetryHandler(handle func(*client.Request)) *Request[RES] {
(*client.Request)(req).RetryHandler(handle)
return req
}
func (req *Request[RES]) BasicAuth(authUser, authPass string) {
(*client.Request)(req).BasicAuth(authUser, authPass)
}
func (req *Request[RES]) SetRequest(handle func(*client.Request)) *Request[RES] {
handle((*client.Request)(req))
return req
}
func (req *Request[RES]) Proxy(url string) *Request[RES] {
(*client.Request)(req).Proxy(url)
return req
}
type Option[RES any] func(req *Request[RES])