/
options.go
103 lines (92 loc) · 2.56 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
package meroxa
import (
"io"
"net/http"
"net/url"
"time"
"golang.org/x/oauth2"
)
type Option func(*Requester) error
// WithBaseURL sets the base url in the client.
// The default is "https://api.meroxa.io".
func WithBaseURL(rawurl string) Option {
return func(r *Requester) error {
u, err := url.Parse(rawurl)
if err != nil {
return err
}
r.baseURL = u
return nil
}
}
// WithClientTimeout sets the http client timeout.
// The default is 5 seconds.
func WithClientTimeout(timeout time.Duration) Option {
return func(r *Requester) error {
r.httpClient.Timeout = timeout
return nil
}
}
// WithUserAgent sets the User-Agent header.
// The default is "meroxa-go".
func WithUserAgent(ua string) Option {
return func(r *Requester) error {
r.userAgent = ua
return nil
}
}
// WithDumpTransport will dump the outgoing requests and incoming responses and
// write them to writer.
func WithDumpTransport(writer io.Writer) Option {
return func(r *Requester) error {
r.httpClient.Transport = &dumpTransport{
out: writer,
transport: r.httpClient.Transport,
obfuscateAuthorization: true,
}
return nil
}
}
// WithClient sets the http client to use for requests.
func WithClient(httpClient *http.Client) Option {
return func(r *Requester) error {
r.httpClient = httpClient
return nil
}
}
// WithAuthentication sets an authenticated http client that takes care of
// adding the access token to requests as well as refreshing it with the
// refresh token when it expires. Observers will be called each time the token
// is refreshed.
// Note: provide WithClientTimeout option before WithAuthentication to set the
// timeout of the client used for fetching access tokens.
func WithAuthentication(conf *oauth2.Config, accessToken, refreshToken string, observers ...TokenObserver) Option {
return func(r *Requester) error {
httpClient, err := newAuthClient(r.httpClient, conf, accessToken, refreshToken, observers...)
if err != nil {
return err
}
r.httpClient = httpClient
return nil
}
}
// WithAccountUUID sets the http client to use for requests.
func WithAccountUUID(accountUUID string) Option {
return func(r *Requester) error {
if len(r.headers) == 0 {
r.headers = make(http.Header)
}
r.headers.Add(meroxaAccountUUIDHeader, accountUUID)
return nil
}
}
// WithHeader allows for setting a generic header to use for requests.
func WithHeader(key, value string) Option {
return func(r *Requester) error {
if len(r.headers) == 0 {
r.headers = make(http.Header)
}
r.headers.Add(key, value)
return nil
}
}