-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
187 lines (161 loc) · 3.81 KB
/
option.go
File metadata and controls
187 lines (161 loc) · 3.81 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
package greq
import (
"context"
"net/http"
gourl "net/url"
"time"
"github.com/gookit/goutil/netutil/httpreq"
)
// Options for one request build
type Options struct {
// url or path for current request
pathURL string
// Method for request
Method string
// ContentType header
ContentType string
// Headers for request
Header http.Header
// HeaderM map string data.
HeaderM map[string]string
// Query params data.
Query gourl.Values
QueryM map[string]any
// Data for request. Will be encoded to query string or request body.
//
// type allow: string, []byte, io.Reader, io.ReadCloser, url.Values, map[string]string
Data any
// Body data for request, only for POST, PUT, PATCH
//
// type allow: string, []byte, io.Reader, io.ReadCloser, url.Values, map[string]string
Body any
// Provider body data provider, can with custom content-type
Provider BodyProvider
// EncodeJSON req body
EncodeJSON bool
// Timeout unit: ms
Timeout int
// TCancelFn will auto set it on Timeout > 0
TCancelFn context.CancelFunc
// Context for request
Context context.Context
// Logger for request
Logger httpreq.ReqLogger
// Retry configuration
MaxRetries int
RetryDelay int
RetryChecker RetryChecker
}
// OptionFn for config request options
type OptionFn func(opt *Options)
// NewOpt for request
func NewOpt(fns ...OptionFn) *Options {
return NewOpt2(fns, "")
}
// NewOpt2 for request
func NewOpt2(fns []OptionFn, method string) *Options {
opt := &Options{
Method: method,
Header: make(http.Header),
HeaderM: make(map[string]string),
Query: make(gourl.Values),
}
for _, fn := range fns {
fn(opt)
}
return opt
}
func orCreate(opt *Options) *Options {
if opt == nil {
opt = &Options{}
}
return opt
}
func ensureOpt(opt *Options) *Options {
if opt == nil {
opt = &Options{}
}
if opt.Context == nil {
opt.Context = context.Background()
}
if opt.Timeout > 0 && opt.TCancelFn == nil {
opt.Context, opt.TCancelFn = context.WithTimeout(
opt.Context,
time.Duration(opt.Timeout)*time.Millisecond,
)
}
return opt
}
//
// ----------- built in OptionFn ------------
//
// WithMethod set method
func WithMethod(method string) OptionFn {
return func(opt *Options) {
if method != "" {
opt.Method = method
}
}
}
// WithContentType set content-type
func WithContentType(contentType string) OptionFn {
return func(opt *Options) {
opt.ContentType = contentType
}
}
// WithUserAgent set user-agent header
func WithUserAgent(userAgent string) OptionFn {
return func(opt *Options) {
opt.Header.Set("User-Agent", userAgent)
}
}
// WithHeader set header
func WithHeader(key, value string) OptionFn {
return func(opt *Options) {
opt.Header.Set(key, value)
}
}
// WithBody set body data
func WithBody(body any) OptionFn {
return func(opt *Options) {
opt.Body = body
}
}
// WithData set data for request
func WithData(data any) OptionFn {
return func(opt *Options) {
opt.Data = data
}
}
// WithTimeout set timeout (ms)
func WithTimeout(timeoutMs int) OptionFn {
return func(opt *Options) {
opt.Timeout = timeoutMs
}
}
// WithRetry set retry configuration for the request
func WithRetry(maxRetries, retryDelay int, checker RetryChecker) OptionFn {
return func(opt *Options) {
opt.MaxRetries = maxRetries
opt.RetryDelay = retryDelay
opt.RetryChecker = checker
}
}
// WithMaxRetries set max retry times for the request
func WithMaxRetries(maxRetries int) OptionFn {
return func(opt *Options) {
opt.MaxRetries = maxRetries
}
}
// WithRetryDelay set retry delay time in milliseconds for the request
func WithRetryDelay(retryDelay int) OptionFn {
return func(opt *Options) {
opt.RetryDelay = retryDelay
}
}
// WithRetryChecker set custom retry checker function for the request
func WithRetryChecker(checker RetryChecker) OptionFn {
return func(opt *Options) {
opt.RetryChecker = checker
}
}