-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_options.go
100 lines (88 loc) · 2.62 KB
/
client_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
package tonicpow
import (
"strings"
"time"
)
// ClientOps allow functional options to be supplied
// that overwrite default client options.
type ClientOps func(c *ClientOptions)
// defaultClientOptions will return a ClientOptions struct with the default settings
//
// Useful for starting with the default and then modifying as needed
func defaultClientOptions() (opts *ClientOptions) {
// Set the default options
opts = &ClientOptions{
env: EnvironmentLive,
httpTimeout: defaultHTTPTimeout,
requestTracing: false,
retryCount: defaultRetryCount,
userAgent: defaultUserAgent,
}
return
}
// WithEnvironment will change the Environment
func WithEnvironment(e Environment) ClientOps {
return func(c *ClientOptions) {
c.env = e
}
}
// WithCustomEnvironment will set a custom Environment
func WithCustomEnvironment(name, alias, apiURL string) ClientOps {
return WithEnvironment(Environment{
alias: alias,
apiURL: apiURL,
name: name,
})
}
// WithEnvironmentString will change the Environment
func WithEnvironmentString(e string) ClientOps {
e = strings.ToLower(strings.TrimSpace(e))
if e == environmentStagingName || e == environmentStagingAlias {
return WithEnvironment(EnvironmentStaging)
} else if e == environmentDevelopmentName || e == environmentDevelopmentAlias {
return WithEnvironment(EnvironmentDevelopment)
}
return WithEnvironment(EnvironmentLive)
}
// WithHTTPTimeout can be supplied to adjust the default http client timeouts.
// The http client is used when creating requests
// Default timeout is 10 seconds.
func WithHTTPTimeout(timeout time.Duration) ClientOps {
return func(c *ClientOptions) {
c.httpTimeout = timeout
}
}
// WithRequestTracing will enable tracing.
// Tracing is disabled by default.
func WithRequestTracing() ClientOps {
return func(c *ClientOptions) {
c.requestTracing = true
}
}
// WithRetryCount will overwrite the default retry count for http requests.
// Default retries is 2.
func WithRetryCount(retries int) ClientOps {
return func(c *ClientOptions) {
c.retryCount = retries
}
}
// WithUserAgent will overwrite the default useragent.
// Default is package name + version.
func WithUserAgent(userAgent string) ClientOps {
return func(c *ClientOptions) {
c.userAgent = userAgent
}
}
// WithAPIKey provides the API key
func WithAPIKey(appAPIKey string) ClientOps {
return func(c *ClientOptions) {
c.apiKey = appAPIKey
}
}
// WithCustomHeaders will add custom headers to outgoing requests
// Custom headers is empty by default
func WithCustomHeaders(headers map[string][]string) ClientOps {
return func(c *ClientOptions) {
c.customHeaders = headers
}
}