Skip to content

Commit 0a6c698

Browse files
committed
MustNewClient
1 parent 7de1240 commit 0a6c698

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

client.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flagsmith
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log/slog"
78
"strings"
@@ -54,7 +55,7 @@ type Client struct {
5455
// flagsmith.WithLocalEvaluation(context.Background()),
5556
// flagsmith.WithDefaultHandler(GetDefaultFlag),
5657
// )
57-
func NewClient(apiKey string, options ...Option) *Client {
58+
func NewClient(apiKey string, options ...Option) (*Client, error) {
5859
c := &Client{
5960
config: defaultConfig(),
6061
client: resty.New(),
@@ -83,13 +84,13 @@ func NewClient(apiKey string, options ...Option) *Client {
8384
}
8485

8586
if c.config.offlineMode && c.offlineHandler == nil {
86-
panic("offline handler must be provided to use offline mode.")
87+
return nil, errors.New("offline handler must be provided to use offline mode")
8788
}
8889
if c.defaultFlagHandler != nil && c.offlineHandler != nil {
89-
panic("default flag handler and offline handler cannot be used together.")
90+
return nil, errors.New("default flag handler and offline handler cannot be used together")
9091
}
9192
if c.config.localEvaluation && c.offlineHandler != nil {
92-
panic("local evaluation and offline handler cannot be used together.")
93+
return nil, errors.New("local evaluation and offline handler cannot be used together")
9394
}
9495
if c.offlineHandler != nil {
9596
c.environment.SetEnvironment(c.offlineHandler.GetEnvironment())
@@ -109,7 +110,16 @@ func NewClient(apiKey string, options ...Option) *Client {
109110
if c.config.enableAnalytics {
110111
c.analyticsProcessor = NewAnalyticsProcessor(c.ctxAnalytics, c.client, c.config.baseURL, nil, c.log)
111112
}
112-
return c
113+
return c, nil
114+
}
115+
116+
// MustNewClient panics if NewClient returns an error.
117+
func MustNewClient(apiKey string, options ...Option) *Client {
118+
client, err := NewClient(apiKey, options...)
119+
if err != nil {
120+
panic(err)
121+
}
122+
return client
113123
}
114124

115125
// GetFlags evaluates the feature flags within an [EvaluationContext].

client_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func getTestHttpServer(t *testing.T, expectedPath string, expectedEnvKey string,
5959
func TestClientErrorsIfLocalEvaluationWithNonServerSideKey(t *testing.T) {
6060
// When, Then
6161
assert.Panics(t, func() {
62-
_ = flagsmith.NewClient("key", flagsmith.WithLocalEvaluation(context.Background()))
62+
_ = flagsmith.MustNewClient("key", flagsmith.WithLocalEvaluation(context.Background()))
6363
})
6464
}
6565

@@ -69,13 +69,13 @@ func TestClientErrorsIfOfflineModeWithoutOfflineHandler(t *testing.T) {
6969
if r := recover(); r != nil {
7070
// Then
7171
errMsg := fmt.Sprintf("%v", r)
72-
expectedErrMsg := "offline handler must be provided to use offline mode."
72+
expectedErrMsg := "offline handler must be provided to use offline mode"
7373
assert.Equal(t, expectedErrMsg, errMsg, "Unexpected error message")
7474
}
7575
}()
7676

7777
// Trigger panic
78-
_ = flagsmith.NewClient("key", flagsmith.WithOfflineMode())
78+
_ = flagsmith.MustNewClient("key", flagsmith.WithOfflineMode())
7979
}
8080

8181
func TestClientErrorsIfDefaultHandlerAndOfflineHandlerAreBothSet(t *testing.T) {
@@ -89,13 +89,13 @@ func TestClientErrorsIfDefaultHandlerAndOfflineHandlerAreBothSet(t *testing.T) {
8989
if r := recover(); r != nil {
9090
// Then
9191
errMsg := fmt.Sprintf("%v", r)
92-
expectedErrMsg := "default flag handler and offline handler cannot be used together."
92+
expectedErrMsg := "default flag handler and offline handler cannot be used together"
9393
assert.Equal(t, expectedErrMsg, errMsg, "Unexpected error message")
9494
}
9595
}()
9696

9797
// Trigger panic
98-
_ = flagsmith.NewClient("key",
98+
_ = flagsmith.MustNewClient("key",
9999
flagsmith.WithOfflineHandler(offlineHandler),
100100
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
101101
return flagsmith.Flag{}, nil
@@ -112,13 +112,13 @@ func TestClientErrorsIfLocalEvaluationModeAndOfflineHandlerAreBothSet(t *testing
112112
if r := recover(); r != nil {
113113
// Then
114114
errMsg := fmt.Sprintf("%v", r)
115-
expectedErrMsg := "local evaluation and offline handler cannot be used together."
115+
expectedErrMsg := "local evaluation and offline handler cannot be used together"
116116
assert.Equal(t, expectedErrMsg, errMsg, "Unexpected error message")
117117
}
118118
}()
119119

120120
// Trigger panic
121-
_ = flagsmith.NewClient("key",
121+
_ = flagsmith.MustNewClient("key",
122122
flagsmith.WithOfflineHandler(offlineHandler),
123123
flagsmith.WithLocalEvaluation(context.Background()))
124124
}
@@ -147,7 +147,7 @@ func TestClientUpdatesEnvironmentOnStartForLocalEvaluation(t *testing.T) {
147147
defer server.Close()
148148

149149
// When
150-
_ = flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
150+
_ = flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
151151
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
152152

153153
// Sleep to ensure that the server has time to update the environment
@@ -183,7 +183,7 @@ func TestClientUpdatesEnvironmentOnEachRefresh(t *testing.T) {
183183
defer server.Close()
184184

185185
// When
186-
_ = flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
186+
_ = flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
187187
flagsmith.WithEnvironmentRefreshInterval(100*time.Millisecond),
188188
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
189189

@@ -204,7 +204,7 @@ func TestGetFlags(t *testing.T) {
204204
defer server.Close()
205205

206206
// When
207-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
207+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
208208

209209
flags, err := client.GetEnvironmentFlags(context.Background())
210210

@@ -228,7 +228,7 @@ func TestGetEnvironmentFlags(t *testing.T) {
228228
defer server.Close()
229229

230230
// When
231-
client := flagsmith.NewClient(expectedEnvKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
231+
client := flagsmith.MustNewClient(expectedEnvKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
232232

233233
_, err := client.GetEnvironmentFlags(ctx)
234234
assert.NoError(t, err)
@@ -241,7 +241,7 @@ func TestGetFlagsEnvironmentEvaluationContextIdentity(t *testing.T) {
241241
defer server.Close()
242242

243243
// When
244-
client := flagsmith.NewClient(expectedEnvKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
244+
client := flagsmith.MustNewClient(expectedEnvKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
245245

246246
_, err := client.GetFlags(
247247
context.Background(),
@@ -259,7 +259,7 @@ func TestGetEnvironmentFlagsUseslocalEnvironmentWhenAvailable(t *testing.T) {
259259
defer server.Close()
260260

261261
// When
262-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
262+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
263263
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
264264
err := client.UpdateEnvironment(ctx)
265265

@@ -295,7 +295,7 @@ func TestGetEnvironmentFlagsCallsAPIWhenLocalEnvironmentNotAvailable(t *testing.
295295
defer server.Close()
296296

297297
// When
298-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
298+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
299299
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
300300
return flagsmith.Flag{}, nil
301301
}))
@@ -332,7 +332,7 @@ func TestGetIdentityFlagsUseslocalEnvironmentWhenAvailable(t *testing.T) {
332332
server := httptest.NewServer(http.HandlerFunc(fixtures.EnvironmentDocumentHandler))
333333
defer server.Close()
334334
// When
335-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
335+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
336336
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
337337
err := client.UpdateEnvironment(ctx)
338338

@@ -358,7 +358,7 @@ func TestGetIdentityFlagsUseslocalOverridesWhenAvailable(t *testing.T) {
358358
server := httptest.NewServer(http.HandlerFunc(fixtures.EnvironmentDocumentHandler))
359359
defer server.Close()
360360
// When
361-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
361+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
362362
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
363363
err := client.UpdateEnvironment(ctx)
364364

@@ -423,7 +423,7 @@ func TestGetIdentityFlagsCallsAPIWhenLocalEnvironmentNotAvailableWithTraits(t *t
423423
}))
424424
defer server.Close()
425425

426-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey,
426+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey,
427427
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
428428

429429
flags, err := client.GetFlags(ctx, flagsmith.NewEvaluationContext("test_identity", traits))
@@ -453,7 +453,7 @@ func TestDefaultHandlerIsUsedWhenNoMatchingEnvironmentFlagReturned(t *testing.T)
453453
defer server.Close()
454454

455455
// When
456-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
456+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
457457
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
458458
return flagsmith.Flag{}, nil
459459
}))
@@ -485,7 +485,7 @@ func TestDefaultHandlerIsUsedWhenTimeout(t *testing.T) {
485485
defer server.Close()
486486

487487
// When
488-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
488+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
489489
flagsmith.WithRequestTimeout(10*time.Millisecond),
490490
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
491491
return flagsmith.Flag{}, nil
@@ -508,7 +508,7 @@ func TestDefaultHandlerIsUsedWhenRequestFails(t *testing.T) {
508508
defer server.Close()
509509

510510
// When
511-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
511+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"),
512512
flagsmith.WithDefaultHandler(func(featureName string) (flagsmith.Flag, error) {
513513
return flagsmith.Flag{}, nil
514514
}))
@@ -530,7 +530,7 @@ func TestFlagsmithAPIErrorIsReturnedIfRequestFailsWithoutDefaultHandler(t *testi
530530
defer server.Close()
531531

532532
// When
533-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
533+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
534534

535535
_, err := client.GetEnvironmentFlags(ctx)
536536
assert.Error(t, err)
@@ -544,7 +544,7 @@ func TestGetIdentitySegmentsNoTraits(t *testing.T) {
544544
server := httptest.NewServer(http.HandlerFunc(fixtures.EnvironmentDocumentHandler))
545545
defer server.Close()
546546

547-
client := flagsmith.NewClient(
547+
client := flagsmith.MustNewClient(
548548
fixtures.EnvironmentAPIKey,
549549
flagsmith.WithLocalEvaluation(ctx),
550550
flagsmith.WithBaseURL(server.URL+"/api/v1/"),
@@ -566,7 +566,7 @@ func TestGetIdentitySegmentsWithTraits(t *testing.T) {
566566
server := httptest.NewServer(http.HandlerFunc(fixtures.EnvironmentDocumentHandler))
567567
defer server.Close()
568568

569-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
569+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx),
570570
flagsmith.WithBaseURL(server.URL+"/api/v1/"))
571571

572572
err := client.UpdateEnvironment(ctx)
@@ -604,7 +604,7 @@ func TestBulkIdentifyReturnsErrorIfBatchSizeIsTooLargeToProcess(t *testing.T) {
604604

605605
}))
606606
defer server.Close()
607-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
607+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
608608

609609
// When
610610
err := client.BulkIdentify(ctx, data)
@@ -623,7 +623,7 @@ func TestBulkIdentifyReturnsErrorIfServerReturns404(t *testing.T) {
623623
rw.WriteHeader(http.StatusNotFound)
624624
}))
625625
defer server.Close()
626-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
626+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
627627

628628
// When
629629
err := client.BulkIdentify(ctx, data)
@@ -663,7 +663,7 @@ func TestBulkIdentify(t *testing.T) {
663663
}))
664664
defer server.Close()
665665

666-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
666+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithBaseURL(server.URL+"/api/v1/"))
667667

668668
// When
669669
err := client.BulkIdentify(ctx, data)
@@ -678,7 +678,7 @@ func TestWithProxyClientOption(t *testing.T) {
678678
server := httptest.NewServer(http.HandlerFunc(fixtures.EnvironmentDocumentHandler))
679679
defer server.Close()
680680

681-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx), flagsmith.WithProxy(server.URL),
681+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithLocalEvaluation(ctx), flagsmith.WithProxy(server.URL),
682682
flagsmith.WithBaseURL("http://some-other-url-that-should-not-be-used/api/v1/"))
683683

684684
err := client.UpdateEnvironment(ctx)
@@ -706,7 +706,7 @@ func TestOfflineMode(t *testing.T) {
706706
offlineHandler, err := flagsmith.NewLocalFileHandler(envJsonPath)
707707
assert.NoError(t, err)
708708

709-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey, flagsmith.WithOfflineMode(), flagsmith.WithOfflineHandler(offlineHandler))
709+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey, flagsmith.WithOfflineMode(), flagsmith.WithOfflineHandler(offlineHandler))
710710

711711
// Then
712712
flags, err := client.GetEnvironmentFlags(ctx)
@@ -746,7 +746,7 @@ func TestPollErrorHandlerIsUsedWhenPollFails(t *testing.T) {
746746
defer server.Close()
747747

748748
// When
749-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey,
749+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey,
750750
flagsmith.WithBaseURL(server.URL+"/api/v1/"),
751751
flagsmith.WithErrorHandler(func(handler *flagsmith.FlagsmithAPIError) {
752752
capturedError = handler.Err
@@ -815,7 +815,7 @@ func TestRealtime(t *testing.T) {
815815
defer server.Close()
816816

817817
// When
818-
client := flagsmith.NewClient(fixtures.EnvironmentAPIKey,
818+
client := flagsmith.MustNewClient(fixtures.EnvironmentAPIKey,
819819
flagsmith.WithBaseURL(server.URL+"/api/v1/"),
820820
flagsmith.WithLocalEvaluation(ctx),
821821
flagsmith.WithRealtime(),

0 commit comments

Comments
 (0)