-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
94 lines (79 loc) · 1.82 KB
/
config.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
package requests
import (
"net"
"net/http"
"net/url"
"time"
)
var (
defaultRequestHeaders = http.Header{
"Content-Type": []string{"application/json"},
}
defaultRequestTimeout = 30 * time.Second
defaultTransport = &http.Transport{
MaxIdleConns: 300,
MaxIdleConnsPerHost: 30,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
DualStack: true,
}).DialContext,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
)
// Options passed into hooks
type Options struct {
DefaultHeaders http.Header
BaseURL *url.URL
Name string
}
type config struct {
Options
timeout *time.Duration
transport http.RoundTripper
}
// Option defines the function signature to set the optional configuration properties
type Option func(c *config)
func WithTimeout(t time.Duration) Option {
return func(c *config) {
c.timeout = &t
}
}
func WithTransport(t http.RoundTripper) Option {
return func(c *config) {
c.transport = t
}
}
func WithDefaultRequestHeaders(headers http.Header) Option {
return func(c *config) {
c.DefaultHeaders = headers
}
}
func WithBaseURL(base *url.URL) Option {
return func(c *config) {
c.BaseURL = base
}
}
func WithName(name string) Option {
return func(c *config) {
c.Name = name
}
}
func (cli *Client) WithName(name string) {
cli.Options.Name = name
}
func (cli *Client) WithBaseURL(base *url.URL) {
cli.Options.BaseURL = base
}
func (cli *Client) WithTimeout(t time.Duration) {
cli.RawClient.Timeout = t
}
func (cli *Client) WithDefaultRequestHeaders(headers http.Header) {
cli.Options.DefaultHeaders = headers
}
func (cli *Client) WithTransport(t http.RoundTripper) {
cli.RawClient.Transport = t
}