-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
302 lines (258 loc) · 8.48 KB
/
option.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package klient
import (
"context"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/worldline-go/logz"
)
type RoundTripperFunc = func(context.Context, http.RoundTripper) (http.RoundTripper, error)
type optionClientValue struct {
// HTTPClient is the http client.
HTTPClient *http.Client
// PooledClient is generate pooled client if no http client provided.
// Default is true.
PooledClient bool
// RoundTripperList is functions that wraps the default transport.
//
// Last function will be called first.
RoundTripperList []RoundTripperFunc
// BaseTransport for change base transport of http client.
BaseTransport http.RoundTripper
// Ctx for RoundTripper.
Ctx context.Context
// MaxConnections is the maximum number of idle (keep-alive) connections.
MaxConnections int
// Logger is the customer logger instance of retryablehttp. Can be either Logger or LeveledLogger
Logger interface{}
// InsecureSkipVerify is the flag to skip TLS verification.
InsecureSkipVerify bool
// Timeout is the timeout for the request.
Timeout time.Duration
// BaseURL is the base URL of the service.
BaseURL string
// DisableBaseURLCheck is the flag to disable base URL check.
DisableBaseURLCheck bool
// Header for default header to set if not exist.
Header http.Header
// DisableRetry is the flag to disable retry.
DisableRetry bool
// RetryWaitMin is the minimum wait time.
// Default is 1 * time.Second.
RetryWaitMin time.Duration
// RetryWaitMax is the maximum wait time.
// Default is 30 * time.Second.
RetryWaitMax time.Duration
// RetryMax is the maximum number of retry.
// Default is 4.
RetryMax int
// RetryPolicy is the retry policy.
RetryPolicy retryablehttp.CheckRetry
// Backoff is the backoff policy.
Backoff retryablehttp.Backoff
// RetryLog is the flag to enable retry log of the http body. Default is true.
RetryLog bool
// OptionRetryFns is the retry options for default retry policy.
OptionRetryFns []OptionRetryFn
// DisableEnvValues is the flag to disable all env values check.
DisableEnvValues bool
// Proxy for http(s) requests. Not used for http2.
Proxy string
// Inject extra content to request (e.g. tracing propagation).
Inject func(ctx context.Context, req *http.Request)
// HTTP2 is the flag to enable http2 transport.
HTTP2 bool
// TLSConfig is the TLS configuration.
TLSConfig *TLSConfig
}
// OptionClientFn is a function that configures the client.
type OptionClientFn func(*optionClientValue)
// WithHTTPClient configures the client to use the provided http client.
func WithHTTPClient(httpClient *http.Client) OptionClientFn {
return func(o *optionClientValue) {
o.HTTPClient = httpClient
}
}
func WithPooledClient(pooledClient bool) OptionClientFn {
return func(o *optionClientValue) {
o.PooledClient = pooledClient
}
}
// WithRoundTripper configures the client to wrap the default transport.
func WithRoundTripper(f ...func(context.Context, http.RoundTripper) (http.RoundTripper, error)) OptionClientFn {
return func(o *optionClientValue) {
o.RoundTripperList = append(o.RoundTripperList, f...)
}
}
// WithCtx for TransportWrapper call.
func WithCtx(ctx context.Context) OptionClientFn {
return func(o *optionClientValue) {
o.Ctx = ctx
}
}
// WithHeader configures the client to use this default header if not exist.
func WithHeader(header http.Header) OptionClientFn {
return func(o *optionClientValue) {
o.Header = header
}
}
func WithHeaderAdd(header http.Header) OptionClientFn {
return func(o *optionClientValue) {
if o.Header == nil {
o.Header = make(http.Header)
}
for key, values := range header {
for _, value := range values {
o.Header.Add(key, value)
}
}
}
}
func WithHeaderSet(header http.Header) OptionClientFn {
return func(o *optionClientValue) {
if o.Header == nil {
o.Header = make(http.Header)
}
for key, values := range header {
o.Header.Set(key, values[0])
}
}
}
func WithHeaderDel(keys ...string) OptionClientFn {
return func(o *optionClientValue) {
if o.Header == nil {
o.Header = make(http.Header)
}
for _, key := range keys {
o.Header.Del(key)
}
}
}
// WithMaxConnections configures the client to use the provided maximum number of idle connections.
func WithMaxConnections(maxConnections int) OptionClientFn {
return func(o *optionClientValue) {
o.MaxConnections = maxConnections
}
}
// WithLogger configures the client to use the provided logger.
//
// For zerolog logz.AdapterKV{Log: logger} can usable.
func WithLogger(logger logz.Adapter) OptionClientFn {
return func(o *optionClientValue) {
o.Logger = logger
}
}
// WithInsecureSkipVerify configures the client to skip TLS verification.
func WithInsecureSkipVerify(insecureSkipVerify bool) OptionClientFn {
return func(o *optionClientValue) {
o.InsecureSkipVerify = insecureSkipVerify
}
}
// WithBaseURL configures the client to use the provided base URL.
func WithBaseURL(baseURL string) OptionClientFn {
return func(o *optionClientValue) {
o.BaseURL = baseURL
}
}
// WithDisableBaseURLCheck configures the client to disable base URL check.
func WithDisableBaseURLCheck(baseURLCheck bool) OptionClientFn {
return func(o *optionClientValue) {
o.DisableBaseURLCheck = baseURLCheck
}
}
// WithTimeout configures the client to use the provided timeout. Default is no timeout.
//
// Warning: This timeout is for the whole request, including retries.
func WithTimeout(timeout time.Duration) OptionClientFn {
return func(o *optionClientValue) {
o.Timeout = timeout
}
}
// WithDisableRetry configures the client to disable retry.
func WithDisableRetry(disableRetry bool) OptionClientFn {
return func(options *optionClientValue) {
options.DisableRetry = disableRetry
}
}
// WithRetryWaitMin configures the client to use the provided minimum wait time.
func WithRetryWaitMin(retryWaitMin time.Duration) OptionClientFn {
return func(options *optionClientValue) {
options.RetryWaitMin = retryWaitMin
}
}
// WithRetryWaitMax configures the client to use the provided maximum wait time.
func WithRetryWaitMax(retryWaitMax time.Duration) OptionClientFn {
return func(options *optionClientValue) {
options.RetryWaitMax = retryWaitMax
}
}
// WithRetryMax configures the client to use the provided maximum number of retry.
func WithRetryMax(retryMax int) OptionClientFn {
return func(options *optionClientValue) {
options.RetryMax = retryMax
}
}
// WithBackoff configures the client to use the provided backoff.
func WithBackoff(backoff retryablehttp.Backoff) OptionClientFn {
return func(options *optionClientValue) {
options.Backoff = backoff
}
}
// WithRetryPolicy configures the client to use the provided retry policy.
func WithRetryPolicy(retryPolicy retryablehttp.CheckRetry) OptionClientFn {
return func(options *optionClientValue) {
options.RetryPolicy = retryPolicy
}
}
// WithRetryLog configures the client to use the provided retry log flag, default is true.
//
// This option is only used with default retry policy.
func WithRetryLog(retryLog bool) OptionClientFn {
return func(options *optionClientValue) {
options.RetryLog = retryLog
}
}
func WithRetryOptions(opts ...OptionRetryFn) OptionClientFn {
return func(options *optionClientValue) {
options.OptionRetryFns = append(options.OptionRetryFns, opts...)
}
}
// WithDisableEnvValues configures the client to disable all env values check.
// - API_GATEWAY_ADDRESS and KLIENT_* env values will be disabled.
func WithDisableEnvValues(disableEnvValues bool) OptionClientFn {
return func(options *optionClientValue) {
options.DisableEnvValues = disableEnvValues
}
}
func WithProxy(proxy string) OptionClientFn {
return func(options *optionClientValue) {
options.Proxy = proxy
}
}
// WithInject configures the client to use the provided inject function.
//
// func(ctx context.Context, req *http.Request) {
// otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
// }
func WithInject(inject func(ctx context.Context, req *http.Request)) OptionClientFn {
return func(options *optionClientValue) {
options.Inject = inject
}
}
// WithHTTP2 configures the client to use the provided http2 transport.
// - WithProxy option will be ignored.
func WithHTTP2(v bool) OptionClientFn {
return func(options *optionClientValue) {
options.HTTP2 = v
}
}
func WithTLSConfig(tlsConfig *TLSConfig) OptionClientFn {
return func(options *optionClientValue) {
options.TLSConfig = tlsConfig
}
}
func WithBaseTransport(baseTransport http.RoundTripper) OptionClientFn {
return func(options *optionClientValue) {
options.BaseTransport = baseTransport
}
}