-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
147 lines (125 loc) · 4.63 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
package httpr
import (
"net/http"
"time"
)
func newDefaultSettings() clientSettings {
return clientSettings{
redirectCheckFn: func(_ *http.Request, _ []*http.Request) error { return nil },
preRequestHookFn: func(_ *http.Request) error { return nil },
postRequestHookFn: func(_ *http.Request, _ *Response) {},
retryConditionFn: func(_ *Response, err error) bool { return true },
}
}
// Option is a function type used for altering client-scoped or request-scoped settings like
// retry count, retry delay, timeout and others.
type Option func(settings *clientSettings)
// WithRetryCount sets number of retries used for request being carried. If requests number failed equals
// specified retry count, Client.Do and all shortcut methods return corresponding error.
func WithRetryCount(retries int) Option {
return func(settings *clientSettings) {
settings.retryCount = retries
}
}
// WithRetryDelay is used to specify delay being taken after unsuccessful request.
// This option is ignored if retry count is not set.
func WithRetryDelay(delay time.Duration) Option {
return func(settings *clientSettings) {
settings.retryDelay = delay
}
}
// WithRetryDelayDelta is used to specify delay delta being added to delay time after each unsuccessful request.
// This option is ignored if retry count is not set.
func WithRetryDelayDelta(delayDelta time.Duration) Option {
return func(settings *clientSettings) {
settings.retryDelayDelta = delayDelta
}
}
// WithTransport is used to change http.Transport used.
func WithTransport(transport http.RoundTripper) Option {
return func(settings *clientSettings) {
if transport != nil {
settings.transport = transport
}
}
}
// WithTimeout specified timeout for request being executed. If response wasn't received within specified timeout,
// Client.Do and all shortcut methods return context.DeadlineExceeded.
func WithTimeout(timeout time.Duration) Option {
return func(settings *clientSettings) {
settings.timeout = timeout
}
}
// WithCheckRedirect sets middleware function for specifying request redirect policy.
func WithCheckRedirect(checkFn func(*http.Request, []*http.Request) error) Option {
return func(settings *clientSettings) {
if checkFn != nil {
settings.redirectCheckFn = checkFn
}
}
}
// RetryConditionFunc is function, used for specifying whether request execution must be
// attempted again. Function must return true is retry is needed, false if not.
type RetryConditionFunc func(*Response, error) bool
// WithRetryCondition sets RetryConditionFunc middleware.
func WithRetryCondition(conditionFn RetryConditionFunc) Option {
return func(settings *clientSettings) {
settings.retryConditionFn = conditionFn
}
}
// WithCookieJar sets http.CookieJar used by underlying http.Client.
func WithCookieJar(cookieJar http.CookieJar) Option {
return func(settings *clientSettings) {
settings.cookieJar = cookieJar
}
}
// PreRequestHookFn is function, which is called before request execution. If request execution must not take place,
// PreRequestHookFn must return non-nil error.
type PreRequestHookFn func(req *http.Request) error
// WithPreRequestHook set PreRequestHookFn compliant function.
func WithPreRequestHook(hookFn PreRequestHookFn) Option {
return func(settings *clientSettings) {
if hookFn != nil {
settings.preRequestHookFn = hookFn
}
}
}
// PostRequestHookFn is function, which is called after request execution.
type PostRequestHookFn func(req *http.Request, resp *Response)
// WithPostRequestHook sets PostRequestHookFn compliant function.
func WithPostRequestHook(hookFn PostRequestHookFn) Option {
return func(settings *clientSettings) {
if hookFn != nil {
settings.postRequestHookFn = hookFn
}
}
}
// WithRateLimiter sets Limiter instance. Limiter is in charged for limiting rate of requests being executed.
func WithRateLimiter(limiter Limiter) Option {
return func(settings *clientSettings) {
if limiter != nil {
settings.rateLimiter = limiter
}
}
}
// WithAutoDecompression specifies whether response body should be unarchived automatically.
// Currently only GZIP is supported.
func WithAutoDecompression(enabled bool) Option {
return func(settings *clientSettings) {
settings.decompressionEnabled = enabled
}
}
// Limiter interface is used to abstract concrete types which purpose is to set and handle rate-limiting for
// request execution.
type Limiter interface {
Take() time.Time
}
// NewUnlimitedLimiter creates dummy struct which implements Limiter interface.
// Used for testing purposes.
func NewUnlimitedLimiter() Limiter {
return &unlimitedLimiter{}
}
type unlimitedLimiter struct{}
func (l *unlimitedLimiter) Take() time.Time {
return time.Now()
}