-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathoption.go
57 lines (48 loc) · 1.54 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
package httpclient
import (
"net/http"
"time"
"github.com/ankorstore/yokai/httpclient/transport"
)
// Options are options for the [HttpClientFactory] implementations.
type Options struct {
Transport http.RoundTripper
CheckRedirect func(req *http.Request, via []*http.Request) error
Jar http.CookieJar
Timeout time.Duration
}
// DefaultHttpClientOptions are the default options used in the [DefaultHttpClientFactory].
func DefaultHttpClientOptions() Options {
return Options{
Transport: transport.NewBaseTransport(),
CheckRedirect: nil,
Jar: nil,
Timeout: time.Second * 30,
}
}
// HttpClientOption are functional options for the [HttpClientFactory] implementations.
type HttpClientOption func(o *Options)
// WithTransport is used to specify the [http.RoundTripper] to use by the [http.Client].
func WithTransport(t http.RoundTripper) HttpClientOption {
return func(o *Options) {
o.Transport = t
}
}
// WithCheckRedirect is used to specify the check redirect func to use by the [http.Client].
func WithCheckRedirect(f func(req *http.Request, via []*http.Request) error) HttpClientOption {
return func(o *Options) {
o.CheckRedirect = f
}
}
// WithCookieJar is used to specify the [http.CookieJar] to use by the [http.Client].
func WithCookieJar(j http.CookieJar) HttpClientOption {
return func(o *Options) {
o.Jar = j
}
}
// WithTimeout is used to specify the timeout to use by the [http.Client].
func WithTimeout(t time.Duration) HttpClientOption {
return func(o *Options) {
o.Timeout = t
}
}