Skip to content

Commit

Permalink
Merge pull request #33 from statsig-io/wrapper
Browse files Browse the repository at this point in the history
updates to make the go-sdk more wrappable in other languages
  • Loading branch information
tore-statsig authored Aug 19, 2021
2 parents c05b846 + a4fcb10 commit bddacea
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ func New(sdkKey string) *Client {

// Initializes a Statsig Client with the given sdkKey and options
func NewWithOptions(sdkKey string, options *types.StatsigOptions) *Client {
return WrapperSDKInstance(sdkKey, options, "", "")
}

func WrapperSDKInstance(sdkKey string, options *types.StatsigOptions, sdkName string, sdkVersion string) *Client {
if len(options.API) == 0 {
options.API = "https://api.statsig.com/v1"
}
net := net.New(sdkKey, options.API)
net := net.New(sdkKey, options.API, sdkName, sdkVersion)
logger := logging.New(net)
evaluator := evaluation.New(net)
if !strings.HasPrefix(sdkKey, "secret") {
Expand Down
12 changes: 10 additions & 2 deletions internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ type Net struct {
client *http.Client
}

func New(secret string, api string) *Net {
func New(secret string, api string, sdkType string, sdkVersion string) *Net {
if api == "" {
api = "https://api.statsig.com/v1"
}
if strings.HasSuffix(api, "/") {
api = api[:len(api)-1]
}

if sdkType == "" {
sdkType = "go-sdk"
}
if sdkVersion == "" {
sdkVersion = "0.2.1"
}

return &Net{
api: api,
metadata: StatsigMetadata{SDKType: "go-sdk", SDKVersion: "0.2.0"},
metadata: StatsigMetadata{SDKType: sdkType, SDKVersion: sdkVersion},
sdkKey: secret,
client: &http.Client{},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNonRetryable(t *testing.T) {
defer testServer.Close()
in := &Empty{}
var out ServerResponse
n := New("secret-123", testServer.URL)
n := New("secret-123", testServer.URL, "", "")
err := n.RetryablePostRequest("/123", in, &out, 2)
if err == nil {
t.Errorf("Expected error for network request but got nil")
Expand All @@ -54,7 +54,7 @@ func TestRetries(t *testing.T) {
defer func() { testServer.Close() }()
in := Empty{}
var out ServerResponse
n := New("secret-123", testServer.URL)
n := New("secret-123", testServer.URL, "", "")
err := n.RetryablePostRequest("/123", in, out, 2)
if err != nil {
t.Errorf("Expected successful request but got error")
Expand Down
6 changes: 5 additions & 1 deletion statsig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ func Initialize(sdkKey string) {

// Initializes the global Statsig instance with the given sdkKey and options
func InitializeWithOptions(sdkKey string, options *types.StatsigOptions) {
WrapperSDK(sdkKey, options, "", "")
}

func WrapperSDK(sdkKey string, options *types.StatsigOptions, sdkName string, sdkVersion string) {
once.Do(func() {
instance = NewWithOptions(sdkKey, options)
instance = WrapperSDKInstance(sdkKey, options, sdkName, sdkVersion)
})
}

Expand Down
8 changes: 4 additions & 4 deletions types/statsig_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package types

// Advanced options for configuring the Statsig SDK
type StatsigOptions struct {
API string
Environment StatsigEnvironment
API string `json:"api"`
Environment StatsigEnvironment `json:"environment"`
}

// See https://docs.statsig.com/guides/usingEnvironments
type StatsigEnvironment struct {
Tier string
Params map[string]string
Tier string `json:"tier"`
Params map[string]string `json:"params"`
}

0 comments on commit bddacea

Please sign in to comment.