-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathoptions.go
228 lines (193 loc) · 4.98 KB
/
options.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
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
package httpreq
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/netutil/httpctype"
)
// Options alias of Option
type Options = Option
// Option struct
type Option struct {
cli *Client
sent bool
// Timeout for request. unit: ms
Timeout int
// Method for request
Method string
// HeaderMap data. eg: traceid
HeaderMap map[string]string
// ContentType header
ContentType string
// Logger for request
Logger ReqLogger
// Context for request
Context context.Context
// Data for request. can be used on any request method.
//
// type allow:
// string, []byte, io.Reader, map[string]string, ...
Data any
// Body data for request. used on POST, PUT, PATCH method.
//
// eg: strings.NewReader("name=inhere")
Body io.Reader
}
// OptionFn option func type
type OptionFn func(opt *Option)
// OptOrNew create a new Option if opt is nil
func OptOrNew(opt *Option) *Option {
if opt == nil {
opt = &Option{}
}
return opt
}
// NewOpt create a new Option and with option func
func NewOpt(fns ...OptionFn) *Option {
return NewOption(fns)
}
// NewOption create a new Option and set option func
func NewOption(fns []OptionFn) *Option {
opt := &Option{}
return opt.WithOptionFn(fns...)
}
// WithOptionFn set option func
func (o *Option) WithOptionFn(fns ...OptionFn) *Option {
return o.WithOptionFns(fns)
}
// WithOptionFns set option func
func (o *Option) WithOptionFns(fns []OptionFn) *Option {
for _, fn := range fns {
if fn != nil {
fn(o)
}
}
return o
}
// WithClient set client
func (o *Option) WithClient(cli *Client) *Option {
o.cli = cli
return o
}
// Copy option for new request, use for repeat send request
func (o *Option) Copy() *Option {
o.Body = nil
on := *o
on.sent = false
return &on
}
// WithMethod set method
func (o *Option) WithMethod(method string) *Option {
if method != "" {
o.Method = method
}
return o
}
// WithContentType set content type
func (o *Option) WithContentType(ct string) *Option {
o.ContentType = ct
return o
}
// WithHeaderMap set header map
func (o *Option) WithHeaderMap(m map[string]string) *Option {
if o.HeaderMap == nil {
o.HeaderMap = make(map[string]string)
}
for k, v := range m {
o.HeaderMap[k] = v
}
return o
}
// WithHeader set header
func (o *Option) WithHeader(key, val string) *Option {
if o.HeaderMap == nil {
o.HeaderMap = make(map[string]string)
}
o.HeaderMap[key] = val
return o
}
// WithData with custom data
func (o *Option) WithData(data any) *Option {
o.Data = data
return o
}
// AnyBody with custom body.
//
// Allow type:
// - string, []byte, map[string][]string/url.Values, io.Reader(eg: bytes.Buffer, strings.Reader)
func (o *Option) AnyBody(data any) *Option {
o.Body = ToRequestBody(data, o.ContentType)
return o
}
// WithBody with custom body
func (o *Option) WithBody(r io.Reader) *Option {
o.Body = r
return o
}
// BytesBody with custom bytes body
func (o *Option) BytesBody(bs []byte) *Option {
o.Body = bytes.NewReader(bs)
return o
}
// FormBody with custom form body data
func (o *Option) FormBody(data any) *Option {
o.ContentType = httpctype.Form
o.Body = ToRequestBody(data, o.ContentType)
return o
}
// WithJSON with custom JSON body
func (o *Option) WithJSON(data any) *Option {
o.ContentType = httpctype.JSON
o.Body = ToRequestBody(data, o.ContentType)
return o
}
// JSONBytesBody with custom bytes body, and set JSON content type
func (o *Option) JSONBytesBody(bs []byte) *Option {
o.ContentType = httpctype.JSON
return o.WithBody(bytes.NewReader(bs))
}
// StringBody with custom string body
func (o *Option) StringBody(s string) *Option {
o.Body = strings.NewReader(s)
return o
}
//
// send request with options
//
// Get send GET request and return http response
func (o *Option) Get(url string, fns ...OptionFn) (*http.Response, error) {
return o.Send(http.MethodGet, url, fns...)
}
// Post send POST request and return http response
func (o *Option) Post(url string, data any, fns ...OptionFn) (*http.Response, error) {
return o.AnyBody(data).Send(http.MethodPost, url, fns...)
}
// Put send PUT request and return http response
func (o *Option) Put(url string, data any, fns ...OptionFn) (*http.Response, error) {
return o.AnyBody(data).Send(http.MethodPut, url, fns...)
}
// Delete send DELETE request and return http response
func (o *Option) Delete(url string, fns ...OptionFn) (*http.Response, error) {
return o.Send(http.MethodDelete, url, fns...)
}
// Send request and return http response
func (o *Option) Send(method, url string, fns ...OptionFn) (*http.Response, error) {
cli := basefn.OrValue(o.cli != nil, o.cli, std)
o.sent = true
o.WithOptionFns(fns).WithMethod(method)
return cli.SendWithOpt(url, o)
}
// MustSend request, will panic on error
func (o *Option) MustSend(method, url string, fns ...OptionFn) *http.Response {
cli := basefn.OrValue(o.cli != nil, o.cli, std)
o.sent = true
o.WithOptionFns(fns).WithMethod(method)
resp, err := cli.SendWithOpt(url, o)
if err != nil {
panic(err)
}
return resp
}