-
Notifications
You must be signed in to change notification settings - Fork 19
feat(api): Add top-level wrapper for more convenient usage. #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
017eec6
a021652
49dfacd
1419921
bd5a74e
413773b
09afdd7
0fa7418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,29 +7,37 @@ import ( | |
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/optimizely/go-sdk" | ||
| "github.com/optimizely/go-sdk/pkg/client" | ||
| "github.com/optimizely/go-sdk/pkg/entities" | ||
| "github.com/optimizely/go-sdk/pkg/event" | ||
| "github.com/optimizely/go-sdk/pkg/events" | ||
| "github.com/optimizely/go-sdk/pkg/logging" | ||
| ) | ||
|
|
||
| func main() { | ||
| sdkKey := "4SLpaJA1r1pgE6T2CoMs9q" | ||
| logging.SetLogLevel(logging.LogLevelDebug) | ||
| user := entities.UserContext{ | ||
| ID: "mike ng", | ||
| Attributes: map[string]interface{}{ | ||
|
|
||
| /************* Simple usage ********************/ | ||
|
|
||
| user := optimizely.UserContext( | ||
| "mike ng", | ||
| map[string]interface{}{ | ||
| "country": "Unknown", | ||
| "likes_donuts": true, | ||
| }, | ||
| } | ||
| ) | ||
| optimizelyClient, _ := optimizely.Client(sdkKey) | ||
| enabled, _ := optimizelyClient.IsFeatureEnabled("mutext_feat", user) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting an error below because we define |
||
| fmt.Printf("Is feature enabled? %v\n", enabled) | ||
|
|
||
| /************* StaticClient ********************/ | ||
|
|
||
| optimizelyFactory := &client.OptimizelyFactory{ | ||
| SDKKey: sdkKey, | ||
| } | ||
|
|
||
| /************* StaticClient ********************/ | ||
|
|
||
| optimizelyClient, err := optimizelyFactory.StaticClient() | ||
| optimizelyClient, err = optimizelyFactory.StaticClient() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if err != nil { | ||
| fmt.Printf("Error instantiating client: %s", err) | ||
|
|
@@ -42,6 +50,7 @@ func main() { | |
| fmt.Println() | ||
| optimizelyClient.Close() // user can close dispatcher | ||
| fmt.Println() | ||
|
|
||
| /************* Client ********************/ | ||
|
|
||
| optimizelyFactory = &client.OptimizelyFactory{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,11 @@ | |
| * limitations under the License. * | ||
| ***************************************************************************/ | ||
|
|
||
| package main | ||
| package optimizely | ||
|
||
|
|
||
| import "github.com/optimizely/go-sdk/cmd" | ||
|
|
||
| // This is used to execute the command-line tool when running the `go-sdk` command from the command-line. | ||
| func main() { | ||
| cmd.Execute() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /**************************************************************************** | ||
| * Copyright 2019, Optimizely, Inc. and contributors * | ||
| * * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| * you may not use this file except in compliance with the License. * | ||
| * You may obtain a copy of the License at * | ||
| * * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| * * | ||
| * Unless required by applicable law or agreed to in writing, software * | ||
| * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| * See the License for the specific language governing permissions and * | ||
| * limitations under the License. * | ||
| ***************************************************************************/ | ||
|
|
||
| package optimizely | ||
|
|
||
| import ( | ||
| "github.com/optimizely/go-sdk/pkg/client" | ||
| "github.com/optimizely/go-sdk/pkg/entities" | ||
| ) | ||
|
|
||
| // Client returns an OptimizelyClient instantitated with the given key and options | ||
| func Client(sdkKey string, options ...client.OptionFunc) (*client.OptimizelyClient, error) { | ||
| factory := &client.OptimizelyFactory{ | ||
| SDKKey: sdkKey, | ||
| } | ||
| return factory.Client(options...) | ||
| } | ||
|
|
||
| // UserContext is a helper method for creating a user context | ||
| func UserContext(userID string, attributes map[string]interface{}) entities.UserContext { | ||
| return entities.UserContext{ | ||
| ID: userID, | ||
| Attributes: attributes, | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import is no longer used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky because when imported, we import the namespace "optimizely", which is the package name and we would be referring to this import via
optimizely.xxxThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @mikecdavis is talking about
"github.com/optimizely/go-sdk/pkg/entities", this is unused.