-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathoptions.go
More file actions
205 lines (179 loc) · 5.16 KB
/
Copy pathoptions.go
File metadata and controls
205 lines (179 loc) · 5.16 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package flagsmith
import (
"context"
"net/http"
"strings"
"time"
"log/slog"
"github.com/go-resty/resty/v2"
)
const (
OptionWithHTTPClient = "WithHTTPClient"
OptionWithRestyClient = "WithRestyClient"
)
type Option func(c *Client)
// Make sure With* functions have correct type.
var _ = []Option{
WithBaseURL(""),
WithLocalEvaluation(context.TODO()),
WithRemoteEvaluation(),
WithRequestTimeout(0),
WithEnvironmentRefreshInterval(0),
WithAnalytics(context.TODO()),
WithRetries(3, 1*time.Second),
WithCustomHeaders(nil),
WithDefaultHandler(nil),
WithProxy(""),
WithPolling(),
WithRealtime(),
WithRealtimeBaseURL(""),
WithLogger(nil),
WithSlogLogger(nil),
WithRestyClient(nil),
WithHTTPClient(nil),
}
func WithBaseURL(url string) Option {
return func(c *Client) {
c.config.baseURL = url
}
}
// WithLocalEvaluation enables local evaluation of the Feature flags.
//
// The goroutine responsible for asynchronously updating the environment makes
// use of the context provided here, which means that if it expires the
// background process will exit.
func WithLocalEvaluation(ctx context.Context) Option {
return func(c *Client) {
c.config.localEvaluation = true
c.ctxLocalEval = ctx
}
}
func WithRemoteEvaluation() Option {
return func(c *Client) {
c.config.localEvaluation = false
}
}
func WithRequestTimeout(timeout time.Duration) Option {
return func(c *Client) {
if c.config.userProvidedClient {
panic("options modifying the client can not be used with a custom client")
}
c.client.SetTimeout(timeout)
}
}
func WithEnvironmentRefreshInterval(interval time.Duration) Option {
return func(c *Client) {
c.config.envRefreshInterval = interval
}
}
// WithAnalytics enables tracking of the usage of the Feature flags.
//
// The goroutine responsible for asynchronously uploading the locally stored
// cache uses the context provided here, which means that if it expires the
// background process will exit.
func WithAnalytics(ctx context.Context) Option {
return func(c *Client) {
c.config.enableAnalytics = true
c.ctxAnalytics = ctx
}
}
func WithRetries(count int, waitTime time.Duration) Option {
return func(c *Client) {
if c.config.userProvidedClient {
panic("options modifying the client can not be used with a custom client")
}
c.client.SetRetryCount(count)
c.client.SetRetryWaitTime(waitTime)
}
}
func WithCustomHeaders(headers map[string]string) Option {
return func(c *Client) {
if c.config.userProvidedClient {
panic("options modifying the client can not be used with a custom client")
}
c.client.SetHeaders(headers)
}
}
func WithDefaultHandler(handler func(string) (Flag, error)) Option {
return func(c *Client) {
c.defaultFlagHandler = handler
}
}
// Allows the client to use any logger that implements the `Logger` interface.
func WithLogger(logger Logger) Option {
return func(c *Client) {
c.log = newLoggerToSlogAdapter(logger)
}
}
// WithSlogLogger allows the client to use a slog.Logger for logging.
func WithSlogLogger(logger *slog.Logger) Option {
return func(c *Client) {
c.log = logger
}
}
// WithProxy returns an Option function that sets the proxy(to be used by internal resty client).
// The proxyURL argument is a string representing the URL of the proxy server to use, e.g. "http://proxy.example.com:8080".
func WithProxy(proxyURL string) Option {
return func(c *Client) {
if c.config.userProvidedClient {
panic("options modifying the client can not be used with a custom client")
}
c.client.SetProxy(proxyURL)
}
}
// WithOfflineHandler returns an Option function that sets the offline handler.
func WithOfflineHandler(handler OfflineHandler) Option {
return func(c *Client) {
c.offlineHandler = handler
}
}
// WithOfflineMode returns an Option function that enables the offline mode.
// NOTE: before using this option, you should set the offline handler.
func WithOfflineMode() Option {
return func(c *Client) {
c.config.offlineMode = true
}
}
// WithErrorHandler provides a way to handle errors that occur during update of an environment.
func WithErrorHandler(handler func(handler *FlagsmithAPIError)) Option {
return func(c *Client) {
c.errorHandler = handler
}
}
// WithRealtime returns an Option function that enables real-time updates for the Client.
// NOTE: Before enabling real-time updates, ensure that local evaluation is enabled.
func WithRealtime() Option {
return func(c *Client) {
c.config.useRealtime = true
}
}
// WithRealtimeBaseURL returns an Option function for configuring the real-time base URL of the Client.
func WithRealtimeBaseURL(url string) Option {
return func(c *Client) {
// Ensure the URL ends with a trailing slash
if !strings.HasSuffix(url, "/") {
url += "/"
}
c.config.realtimeBaseUrl = url
}
}
// WithPolling makes it so that the client will poll for updates even when WithRealtime is used.
func WithPolling() Option {
return func(c *Client) {
c.config.polling = true
}
}
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) {
if httpClient != nil {
c.httpClient = httpClient
}
}
}
func WithRestyClient(restyClient *resty.Client) Option {
return func(c *Client) {
if restyClient != nil {
c.client = restyClient
}
}
}