Skip to content

Commit 988e197

Browse files
author
wood
committed
feat: prevent-setting-logger-and-default-options-on-configured-custom-clients
1 parent 6820695 commit 988e197

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

client.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,49 +78,44 @@ func NewClient(apiKey string, options ...Option) *Client {
7878
}
7979
}
8080

81+
// If a resty custom client has been provided, client is already set - otherwise we use a custom http client or default to a resty
8182
if c.client == nil {
8283
if c.httpClient != nil {
8384
c.client = resty.NewWithClient(c.httpClient)
85+
c.config.userProvidedClient = true
8486
} else {
8587
c.client = resty.New()
8688
}
87-
}
88-
89-
for _, opt := range options {
90-
name := getOptionQualifiedName(opt)
91-
if isClientOption(name) {
92-
continue
93-
}
94-
opt(c)
89+
} else {
90+
c.config.userProvidedClient = true
9591
}
9692

9793
c.client.SetHeaders(map[string]string{
9894
"Accept": "application/json",
9995
EnvironmentKeyHeader: c.apiKey,
10096
})
101-
c.client.SetTimeout(c.config.timeout)
97+
98+
if c.client.GetClient().Timeout == 0 {
99+
c.client.SetTimeout(c.config.timeout)
100+
}
101+
102102
c.log = createLogger()
103103

104104
for _, opt := range options {
105-
if opt != nil {
106-
opt(c)
105+
name := getOptionQualifiedName(opt)
106+
if isClientOption(name) {
107+
continue
107108
}
109+
opt(c)
108110
}
109111

110-
// If a resty custom client has been provided, client is already set - otherwise we use a custom http client or default to a resty
111-
if c.client == nil {
112-
if c.httpClient != nil {
113-
c.client = resty.NewWithClient(c.httpClient)
114-
} else {
115-
c.client = resty.New()
116-
}
112+
if !c.config.userProvidedClient {
113+
c.client = c.client.
114+
SetLogger(newSlogToRestyAdapter(c.log)).
115+
OnBeforeRequest(newRestyLogRequestMiddleware(c.log)).
116+
OnAfterResponse(newRestyLogResponseMiddleware(c.log))
117117
}
118118

119-
c.client = c.client.
120-
SetLogger(newSlogToRestyAdapter(c.log)).
121-
OnBeforeRequest(newRestyLogRequestMiddleware(c.log)).
122-
OnAfterResponse(newRestyLogResponseMiddleware(c.log))
123-
124119
c.log.Info("initialising Flagsmith client",
125120
"base_url", c.config.baseURL,
126121
"local_evaluation", c.config.localEvaluation,

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type config struct {
2727
realtimeBaseUrl string
2828
useRealtime bool
2929
polling bool
30+
userProvidedClient bool
3031
}
3132

3233
// defaultConfig returns default configuration.
@@ -36,5 +37,6 @@ func defaultConfig() config {
3637
timeout: DefaultTimeout,
3738
envRefreshInterval: time.Second * 60,
3839
realtimeBaseUrl: DefaultRealtimeBaseUrl,
40+
userProvidedClient: false,
3941
}
4042
}

options.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ func WithAnalytics(ctx context.Context) Option {
8989

9090
func WithRetries(count int, waitTime time.Duration) Option {
9191
return func(c *Client) {
92-
c.client.SetRetryCount(count)
93-
c.client.SetRetryWaitTime(waitTime)
92+
if c.config.userProvidedClient && c.client.RetryCount == 0 {
93+
c.client.SetRetryCount(count)
94+
c.client.SetRetryWaitTime(waitTime)
95+
}
9496
}
9597
}
9698

0 commit comments

Comments
 (0)