Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (66 loc) · 2.36 KB
/
Copy pathmain.go
File metadata and controls
79 lines (66 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/featurevisor/featurevisor-go/v4"
)
const datafileURL = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json"
type serviceEndpoints struct {
BaseURL string `json:"baseUrl"`
TimeoutMS int `json:"timeoutMs"`
Retries int `json:"retries"`
}
func main() {
client := &http.Client{Timeout: 10 * time.Second}
response, err := client.Get(datafileURL)
if err != nil {
panic(fmt.Errorf("fetch datafile: %w", err))
}
defer response.Body.Close()
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
panic(fmt.Errorf("fetch datafile: unexpected HTTP status %s", response.Status))
}
datafileJSON, err := io.ReadAll(response.Body)
if err != nil {
panic(fmt.Errorf("read datafile: %w", err))
}
var datafile featurevisor.DatafileContent
if err := datafile.FromJSON(string(datafileJSON)); err != nil {
panic(fmt.Errorf("parse datafile: %w", err))
}
logLevel := featurevisor.LogLevelError
f := featurevisor.CreateFeaturevisor(featurevisor.FeaturevisorOptions{
Datafile: datafile,
LogLevel: &logLevel,
Context: featurevisor.Context{
"userId": "customer-123",
"country": "nl",
"locale": "nl-NL",
"accountPlan": "pro",
},
})
defer f.Close()
commerceEnabled := f.IsEnabled("commerce_platform")
checkoutVariation := f.GetVariation("checkout_experience")
maxItems := f.GetVariableInteger("checkout_experience", "max_items")
paymentMethods := f.GetVariableArray("checkout_experience", "payment_methods")
supportContact := f.GetGlobalVariableString("supportContact")
var endpoints serviceEndpoints
if err := f.GetGlobalVariableObjectInto("serviceEndpoints", &endpoints); err != nil {
panic(fmt.Errorf("evaluate service endpoints: %w", err))
}
fmt.Println("Commerce platform enabled:", commerceEnabled)
fmt.Println("Checkout variation:", valueOrUnavailable(checkoutVariation))
fmt.Println("Maximum checkout items:", valueOrUnavailable(maxItems))
fmt.Println("Payment methods:", paymentMethods)
fmt.Printf("Service endpoint: %s (timeout: %d ms, retries: %d)\n", endpoints.BaseURL, endpoints.TimeoutMS, endpoints.Retries)
fmt.Println("Support contact:", valueOrUnavailable(supportContact))
}
func valueOrUnavailable[T any](value *T) any {
if value == nil {
return "unavailable"
}
return *value
}