-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
options.go
166 lines (147 loc) · 4.36 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package httpc
import (
"compress/gzip"
"crypto/tls"
"io"
"net"
"net/http"
"time"
)
// ClientOptFn are options to set different parameters on the Client.
type ClientOptFn func(*clientOpt) error
type clientOpt struct {
addr string
insecureSkipVerify bool
doer doer
headers http.Header
authFn func(*http.Request)
respFn func(*http.Response) error
statusFn func(*http.Response) error
writerFns []WriteCloserFn
}
// WithAddr sets the host address on the client.
func WithAddr(addr string) ClientOptFn {
return func(opt *clientOpt) error {
opt.addr = addr
return nil
}
}
// WithAuth provides a means to set a custom auth that doesn't match
// the provided auth types here.
func WithAuth(fn func(r *http.Request)) ClientOptFn {
return func(opt *clientOpt) error {
opt.authFn = fn
return nil
}
}
// WithAuthToken provides token auth for requests.
func WithAuthToken(token string) ClientOptFn {
return WithAuth(func(r *http.Request) {
r.Header.Set("Authorization", "Token "+token)
})
}
// WithSessionCookie provides cookie auth for requests to mimic the browser.
// Typically, session is influxdb.Session.Key.
func WithSessionCookie(session string) ClientOptFn {
return WithAuth(func(r *http.Request) {
r.AddCookie(&http.Cookie{
Name: "session",
Value: session,
})
})
}
// WithContentType sets the content type that will be applied to the requests created
// by the Client.
func WithContentType(ct string) ClientOptFn {
return WithHeader(headerContentType, ct)
}
func withDoer(d doer) ClientOptFn {
return func(opt *clientOpt) error {
opt.doer = d
return nil
}
}
// WithHeader sets a default header that will be applied to all requests created
// by the client.
func WithHeader(header, val string) ClientOptFn {
return func(opt *clientOpt) error {
if opt.headers == nil {
opt.headers = make(http.Header)
}
opt.headers.Add(header, val)
return nil
}
}
// WithUserAgentHeader sets the user agent for the http client requests.
func WithUserAgentHeader(userAgent string) ClientOptFn {
return WithHeader("User-Agent", userAgent)
}
// WithHTTPClient sets the raw http client on the httpc Client.
func WithHTTPClient(c *http.Client) ClientOptFn {
return func(opt *clientOpt) error {
opt.doer = c
return nil
}
}
// WithInsecureSkipVerify sets the insecure skip verify on the http client's htp transport.
func WithInsecureSkipVerify(b bool) ClientOptFn {
return func(opts *clientOpt) error {
opts.insecureSkipVerify = b
return nil
}
}
// WithRespFn sets the default resp fn for the client that will be applied to all requests
// generated from it.
func WithRespFn(fn func(*http.Response) error) ClientOptFn {
return func(opt *clientOpt) error {
opt.respFn = fn
return nil
}
}
// WithStatusFn sets the default status fn for the client that will be applied to all requests
// generated from it.
func WithStatusFn(fn func(*http.Response) error) ClientOptFn {
return func(opt *clientOpt) error {
opt.statusFn = fn
return nil
}
}
// WithWriterFn applies the provided writer behavior to all the request bodies'
// generated from the client.
func WithWriterFn(fn WriteCloserFn) ClientOptFn {
return func(opt *clientOpt) error {
opt.writerFns = append(opt.writerFns, fn)
return nil
}
}
// WithWriterGZIP gzips the request body generated from this client.
func WithWriterGZIP() ClientOptFn {
return WithWriterFn(func(w io.WriteCloser) (string, string, io.WriteCloser) {
return headerContentEncoding, "gzip", gzip.NewWriter(w)
})
}
// DefaultTransportInsecure is identical to http.DefaultTransport, with
// the exception that tls.Config is configured with InsecureSkipVerify
// set to true.
var DefaultTransportInsecure http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
func defaultHTTPClient(scheme string, insecure bool) *http.Client {
if scheme == "https" && insecure {
return &http.Client{Transport: DefaultTransportInsecure}
}
return &http.Client{Transport: http.DefaultTransport}
}