Skip to content

Commit

Permalink
Redo initialization options
Browse files Browse the repository at this point in the history
  • Loading branch information
bpapillon committed Oct 25, 2024
1 parent 1b1df12 commit a6054fa
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 226 deletions.
4 changes: 3 additions & 1 deletion .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
README.md
buffer/buffer.go
cache/cache.go
core/request_option.go
core/custom_options.go
custom_types.go
client/opts.go
client/schematic_client.go
client/schematic_client_test.go
flags/flags.go
Expand All @@ -14,3 +15,4 @@ generate.sh
http/offline.go
logger/logger.go
mocks/http_mock.go
option/custom_options.go
79 changes: 62 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ go get github.com/schematichq/schematic-go
import (
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.SchematicClient(apiKey)
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()
}
```
Expand All @@ -33,16 +34,17 @@ import (
"os"
"time"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
apiKey := os.Getenv("SCHEMATIC_API_KEY")
cacheSize := 100
cacheTTL := 1 * time.Millisecond
client := schematicclient.SchematicClient(
apiKey,
schematicclient.WithLocalFlagCheckCache(cacheSize, cacheTTL),
client := schematicclient.NewSchematicClient(
option.WithAPIKey(apiKey),
option.WithLocalFlagCheckCache(cacheSize, cacheTTL),
)
defer client.Close()
}
Expand All @@ -54,12 +56,13 @@ You can also disable local caching entirely with an initialization option; bear
import (
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.SchematicClient(apiKey, schematicclient.WithDisabledFlagCheckCache())
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey), option.WithDisabledFlagCheckCache())
defer client.Close()
}
```
Expand All @@ -70,12 +73,13 @@ You may want to specify default flag values for your application, which will be
import (
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.SchematicClient(apiKey, schematicclient.WithDefaultFlagValues(map[string]bool{
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey), option.WithDefaultFlagValues(map[string]bool{
"some-flag-key": true,
}))
defer client.Close()
Expand All @@ -93,12 +97,14 @@ import (
"context"
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
schematicgo "github.com/schematichq/schematic-go/"
)

func main() {
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY"))
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()

client.Identify(context.Background(), &schematicgo.EventBodyIdentify{
Expand Down Expand Up @@ -131,12 +137,14 @@ import (
"context"
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
schematicgo "github.com/schematichq/schematic-go/"
)

func main() {
client := schematicclient.SchematicClient(os.Getenv("SCHEMATIC_API_KEY"))
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()

client.Track(context.Background(), &schematicgo.EventBodyTrack{
Expand All @@ -163,12 +171,14 @@ import (
"context"
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
schematicgo "github.com/schematichq/schematic-go/"
)

func main() {
client := schematicclient.SchematicClient(os.Getenv("SCHEMATIC_API_KEY"))
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()

body := &schematicgo.UpsertCompanyRequestBody{
Expand Down Expand Up @@ -198,12 +208,14 @@ import (
"context"
"os"

option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/SchematicHQ/schematic-go/client"
schematicgo "github.com/SchematicHQ/schematic-go/"
)

func main() {
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY"))
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()

body := &schematicgo.UpsertUserRequestBody{
Expand Down Expand Up @@ -237,13 +249,15 @@ When checking a flag, you'll provide keys for a company and/or keys for a user.
import (
"context"
"os"


option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
schematicgo "github.com/schematichq/schematic-go/"
)

func main() {
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY"))
apiKey := os.Getenv("SCHEMATIC_API_KEY")
client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
defer client.Close()

evaluationCtx := schematicgo.CheckFlagRequestBody{
Expand Down Expand Up @@ -272,28 +286,58 @@ The Schematic API supports many operations beyond these, accessible via `client.

### Offline Mode

In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by providing an empty API key to the client:
In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by not providing an API key to the client:

```go
import (
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
client := schematicclient.NewSchematicClient()
defer client.Close()
}
```

You can also enable offline mode by providing an empty API key:

```go
import (
option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
client := schematicclient.NewSchematicClient("")
client := schematicclient.NewSchematicClient(option.WithAPIKey(""))
defer client.Close()
}
```

Or, by using the offline mode option:

```go
import (
option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
client := schematicclient.NewSchematicClient(option.WithOfflineMode())
defer client.Close()
}
```


Offline mode works well with flag defaults:

```go
import (
option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func main() {
client := schematicclient.NewSchematicClient("", schematicclient.WithDefaultFlagValues(map[string]bool{
client := schematicclient.NewSchematicClient(option.WithOfflineMode(), option.WithDefaultFlagValues(map[string]bool{
"some-flag-key": true,
}))
defer client.Close()
Expand All @@ -304,15 +348,16 @@ In an automated testing context, you may also want to use offline mode and speci

```go
import (
option "github.com/schematichq/schematic-go/option"
schematicclient "github.com/schematichq/schematic-go/client"
)

func TestSomeFunctionality(t *testing.T) {
client := schematicclient.NewSchematicClient("")
client := schematicclient.NewSchematicClient(option.WithOfflineMode())
defer client.Close()

client.SetFlagDefault("some-flag-key", true)

// test code that expects the flag to be on
}
```
```
5 changes: 5 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"time"
)

type CacheProvider[T any] interface {
Get(ctx context.Context, key string) (T, bool)
Set(ctx context.Context, key string, val T, ttlOverride *time.Duration) error
}

const defaultCacheSize = 1000 // 1000 records
const defaultCacheTTL = 5 * time.Second

Expand Down
127 changes: 0 additions & 127 deletions client/opts.go

This file was deleted.

Loading

0 comments on commit a6054fa

Please sign in to comment.