@@ -29,13 +29,6 @@ import (
2929 "github.com/optimizely/go-sdk/optimizely/utils"
3030)
3131
32- // Options are used to create an instance of the OptimizelyClient with custom configuration
33- type Options struct {
34- ProjectConfigManager optimizely.ProjectConfigManager
35- DecisionService decision.Service
36- EventProcessor event.Processor
37- }
38-
3932// OptimizelyFactory is used to construct an instance of the OptimizelyClient
4033type OptimizelyFactory struct {
4134 SDKKey string
@@ -45,26 +38,40 @@ type OptimizelyFactory struct {
4538// OptionFunc is a type to a proper func
4639type OptionFunc func (* OptimizelyClient , utils.ExecutionCtx )
4740
48- // GetClient gets client and sets some parameters
49- func (f OptimizelyFactory ) GetClient (clientOptions ... OptionFunc ) * OptimizelyClient {
41+ // Client gets client and sets some parameters
42+ func (f OptimizelyFactory ) Client (clientOptions ... OptionFunc ) (* OptimizelyClient , error ) {
43+
5044 executionCtx := utils .NewCancelableExecutionCtx ()
45+ notificationCenter := notification .NewNotificationCenter ()
46+
5147 appClient := & OptimizelyClient {
52- executionCtx : executionCtx ,
48+ executionCtx : executionCtx ,
49+ decisionService : decision .NewCompositeService (notificationCenter ),
50+ eventProcessor : event .NewEventProcessor (executionCtx , event .DefaultBatchSize , event .DefaultEventQueueSize , event .DefaultEventFlushInterval ),
5351 }
52+
5453 for _ , opt := range clientOptions {
5554 opt (appClient , executionCtx )
5655 }
57- return appClient
56+
57+ if f .SDKKey == "" && f .Datafile == nil && appClient .configManager == nil {
58+ return nil , errors .New ("unable to instantiate client: no project config manager, SDK key, or a Datafile provided" )
59+ }
60+
61+ if appClient .configManager == nil { // if it was not passed then assign here
62+
63+ appClient .configManager = config .NewPollingProjectConfigManager (executionCtx , f .SDKKey ,
64+ config .InitialDatafile (f .Datafile ), config .PollingInterval (config .DefaultPollingInterval ), config .NotificationCenter (notificationCenter ))
65+ }
66+
67+ return appClient , nil
5868}
5969
6070// PollingConfigManager sets polling config manager on a client
61- func PollingConfigManager (sdkKey string , pollingInterval time.Duration , dataFile []byte ) OptionFunc {
71+ func PollingConfigManager (sdkKey string , pollingInterval time.Duration , initDataFile []byte , notificationCenter notification. Center ) OptionFunc {
6272 return func (f * OptimizelyClient , executionCtx utils.ExecutionCtx ) {
63- options := config.PollingProjectConfigManagerOptions {
64- Datafile : dataFile ,
65- PollingInterval : pollingInterval ,
66- }
67- f .configManager = config .NewPollingProjectConfigManagerWithOptions (f .executionCtx , sdkKey , options )
73+ f .configManager = config .NewPollingProjectConfigManager (f .executionCtx , sdkKey , config .InitialDatafile (initDataFile ),
74+ config .PollingInterval (pollingInterval ), config .NotificationCenter (notificationCenter ))
6875 }
6976}
7077
@@ -126,57 +133,12 @@ func (f OptimizelyFactory) StaticClient() (*OptimizelyClient, error) {
126133 configManager = staticConfigManager
127134 }
128135
129- clientOptions := Options {
130- ProjectConfigManager : configManager ,
131- }
132- client , err := f .ClientWithOptions (clientOptions )
133- return client , err
134- }
135-
136- // ClientWithOptions returns a client initialized with the given configuration options
137- func (f OptimizelyFactory ) ClientWithOptions (clientOptions Options ) (* OptimizelyClient , error ) {
138-
139- executionCtx := utils .NewCancelableExecutionCtx ()
140- client := & OptimizelyClient {
141- executionCtx : executionCtx ,
142- }
143-
144136 notificationCenter := notification .NewNotificationCenter ()
145137
146- switch {
147- case clientOptions .ProjectConfigManager != nil :
148- client .configManager = clientOptions .ProjectConfigManager
149- case f .SDKKey != "" :
150- options := config.PollingProjectConfigManagerOptions {
151- Datafile : f .Datafile ,
152- }
153- client .configManager = config .NewPollingProjectConfigManagerWithOptions (executionCtx , f .SDKKey , options )
154- case f .Datafile != nil :
155- staticConfigManager , _ := config .NewStaticProjectConfigManagerFromPayload (f .Datafile )
156- client .configManager = staticConfigManager
157- default :
158- return client , errors .New ("unable to instantiate client: no project config manager, SDK key, or a Datafile provided" )
159- }
160-
161- if clientOptions .DecisionService != nil {
162- client .decisionService = clientOptions .DecisionService
163- } else {
164- client .decisionService = decision .NewCompositeService (notificationCenter )
165- }
166-
167- if clientOptions .EventProcessor != nil {
168- client .eventProcessor = clientOptions .EventProcessor
169- } else {
170- client .eventProcessor = event .NewEventProcessor (executionCtx , event .DefaultBatchSize , event .DefaultEventQueueSize , event .DefaultEventFlushInterval )
171- }
172-
173- return client , nil
174- }
175-
176- // Client returns a client initialized with the defaults
177- func (f OptimizelyFactory ) Client () (* OptimizelyClient , error ) {
178- // Creates a default, canceleable context
179- clientOptions := Options {}
180- client , err := f .ClientWithOptions (clientOptions )
181- return client , err
138+ optlyClient , e := f .Client (
139+ ConfigManager (configManager ),
140+ CompositeDecisionService (notificationCenter ),
141+ BatchEventProcessor (event .DefaultBatchSize , event .DefaultEventQueueSize , event .DefaultEventFlushInterval ),
142+ )
143+ return optlyClient , e
182144}
0 commit comments