@@ -32,32 +32,43 @@ type Attributes struct {
3232 VSCodeClientID string
3333}
3434
35+ type ClientOpt func (o * options )
36+
37+ func WithGitpodProxy (gitpodHost string ) ClientOpt {
38+ return func (o * options ) {
39+ o .sdkKey = "gitpod"
40+ o .baseURL = fmt .Sprintf ("https://%s/configcat" , gitpodHost )
41+ o .pollInterval = 1 * time .Minute
42+ }
43+ }
44+
45+ type options struct {
46+ pollInterval time.Duration
47+ baseURL string
48+ sdkKey string
49+ }
50+
3551// NewClient constructs a new experiments.Client. This is NOT A SINGLETON.
3652// You should normally only call this once in the lifecycle of an application, clients are independent of each other will refresh flags on their own.
37- // If the environment contains CONFIGCAT_SDK_KEY value, it vill be used to construct a ConfigCat client.
53+ // If the environment contains CONFIGCAT_SDK_KEY value, it will be used to construct a ConfigCat client.
3854// Otherwise, it returns a client which always returns the default value. This client is used for Self-Hosted installations.
39- func NewClient () Client {
40- // TODO: get rid fo GITPOD_HOST is implementaion detail of supervisor, it is a subject to change
41- // gitpodHost should be provided as an option instead
42- gitpodHost := os .Getenv ("GITPOD_HOST" )
43- if gitpodHost == "" {
44- gitpodHost = os .Getenv ("HOST_URL" )
55+ func NewClient (opts ... ClientOpt ) Client {
56+ opt := & options {
57+ sdkKey : os .Getenv ("CONFIGCAT_SDK_KEY" ),
58+ baseURL : os .Getenv ("CONFIGCAT_BASE_URL" ),
59+ pollInterval : 3 * time .Minute ,
4560 }
46- if gitpodHost != "" {
47- return newConfigCatClient (configcat.Config {
48- SDKKey : "gitpod" ,
49- BaseURL : fmt .Sprintf ("%s%s" , gitpodHost , "/configcat" ),
50- PollInterval : 1 * time .Minute ,
51- HTTPTimeout : 3 * time .Second ,
52- })
61+ for _ , o := range opts {
62+ o (opt )
5363 }
54- sdkKey := os . Getenv ( "CONFIGCAT_SDK_KEY" )
55- if sdkKey == "" {
64+
65+ if opt . sdkKey == "" {
5666 return NewAlwaysReturningDefaultValueClient ()
5767 }
5868 return newConfigCatClient (configcat.Config {
59- SDKKey : sdkKey ,
60- PollInterval : 3 * time .Minute ,
69+ SDKKey : opt .sdkKey ,
70+ BaseURL : opt .baseURL ,
71+ PollInterval : opt .pollInterval ,
6172 HTTPTimeout : 3 * time .Second ,
6273 Logger : & configCatLogger {log .Log },
6374 })
0 commit comments