Skip to content

Commit c763ba3

Browse files
committed
Merge pull request #2 from optimizely/make_library_more_awesome
Consolidates two functions to get data from Optimizely, changes funct…
2 parents 696946a + b43ad25 commit c763ba3

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

examples/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/optimizely/go-sdk/optimizely"
56
)
67

78
func main() {
8-
OPTIMIZELY_ACCOUNT_ID := 12345
9-
buffer := optimizely.FetchProjectConfig(OPTIMIZELY_ACCOUNT_ID)
10-
optimizely.DeserializeConfigBuffer(buffer)
9+
OPTIMIZELY_ACCOUNT_ID := "12345"
10+
_, err := optimizely.GetOptimizelyClient(OPTIMIZELY_ACCOUNT_ID)
11+
if err != nil {
12+
fmt.Print(err)
13+
}
1114
}

optimizely/optimizely.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,34 @@ import (
55
"net/url"
66
)
77

8+
// OptimizelyClient is the client to interface with the Optimizely server
9+
// side APIs.
10+
type OptimizelyClient struct {
11+
account_id string
12+
project_config ProjectConfig
13+
}
14+
15+
// GetOptimizelyClient returns a client that can be used to interface
16+
// with Optimizely
17+
func GetOptimizelyClient(account_id string) (*OptimizelyClient, error) {
18+
client := OptimizelyClient{}
19+
project_config, err := FetchProjectConfig(account_id)
20+
if err != nil {
21+
return &client, err
22+
}
23+
client.account_id = account_id
24+
client.project_config = project_config
25+
return &client, nil
26+
27+
}
28+
829
// Track tracks a conversion event for a user_id
930
// Logs the conversion
1031
// event_key: goal key representing the event which needs to be recorded
1132
// user_id: ID for user.
1233
// attributes: Dict representing visitor attributes and values which need to be recorded.
1334
// event_value: Value associated with the event. Can be used to represent revenue in cents.
14-
func Track(
35+
func (client *OptimizelyClient) Track(
1536
event_key string,
1637
user_id string,
1738
attributes []AttributeEntity,
@@ -62,13 +83,13 @@ func Track(
6283
// experiment_key: experiment which needs to be activated
6384
// user_id: ID for user
6485
// attributes: optional list representing visitor attributes and values
65-
func Activate(experiment_key string, user_id string, attributes []AttributeEntity) {
86+
func (client *OptimizelyClient) Activate(experiment_key string, user_id string, attributes []AttributeEntity) {
6687

6788
}
6889

6990
// GetVariation gets the variation where the visitor will be bucketed
7091
// Experiment_key: experiment which needs to be activated
7192
// User_id: ID for user
72-
func GetVariation(experient_key string, user_id string) {
93+
func (client *OptimizelyClient) GetVariation(experient_key string, user_id string) {
7394

7495
}

optimizely/project_config.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package optimizely
22

33
import (
4-
"bytes"
54
"encoding/json"
5+
"errors"
66
"fmt"
77
"log"
88
"net/http"
@@ -12,38 +12,25 @@ var PROJECT_CONFIG_LINK_TEMPLATE = "https://cdn.optimizely.com/json/%v.json"
1212

1313
// FetchConfig retrieves a JSON file from the Optimizely CDN
1414
// and returns it accordingly
15-
func FetchProjectConfig(project_id int) bytes.Buffer {
16-
var b bytes.Buffer
17-
15+
func FetchProjectConfig(project_id string) (ProjectConfig, error) {
16+
var project_config = ProjectConfig{}
1817
var project_config_url = fmt.Sprintf(PROJECT_CONFIG_LINK_TEMPLATE, project_id)
1918
resp, err := http.Get(project_config_url)
2019
defer resp.Body.Close()
2120

2221
if resp.StatusCode != http.StatusOK {
2322
log.Printf("Status code of %v when fetching project config", resp.StatusCode)
23+
return project_config, errors.New(fmt.Sprintf("Error fetching config, Optimizely returned %v", resp.StatusCode))
2424
}
2525
if err != nil {
2626
log.Printf("Error fetching project config: %v", err)
27-
return b
27+
return project_config, errors.New("Error fetching config")
2828
}
2929

30-
_, err = b.ReadFrom(resp.Body)
30+
err = json.NewDecoder(resp.Body).Decode(&project_config)
3131
if err != nil {
3232
log.Printf("Error reading JSON response into a buffer")
33+
return project_config, errors.New("Cannot parse JSON file")
3334
}
34-
return b
35-
}
36-
37-
// DeserializeConfigBuffer takes a buffer containing a JSON response
38-
// from the Optimizely CDN and converts it to a ProjectConfig entity
39-
// which gets returned
40-
func DeserializeConfigBuffer(b bytes.Buffer) ProjectConfig {
41-
var project_config = ProjectConfig{}
42-
r := bytes.NewReader(b.Bytes())
43-
err := json.NewDecoder(r).Decode(&project_config)
44-
if err != nil {
45-
log.Print(err)
46-
return project_config
47-
}
48-
return project_config
35+
return project_config, nil
4936
}

0 commit comments

Comments
 (0)