-
Notifications
You must be signed in to change notification settings - Fork 15
/
options.go
148 lines (129 loc) · 3.8 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
package flagsmith
import (
"context"
"strings"
"time"
)
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(""),
WithRealtime(),
WithRealtimeBaseURL(""),
}
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) {
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) {
c.client.SetRetryCount(count)
c.client.SetRetryWaitTime(waitTime)
}
}
func WithCustomHeaders(headers map[string]string) Option {
return func(c *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 = 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) {
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
}
}