Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redo initialization options
Browse files Browse the repository at this point in the history
bpapillon committed Oct 25, 2024
1 parent 1b1df12 commit 2489380
Showing 13 changed files with 257 additions and 231 deletions.
4 changes: 3 additions & 1 deletion .fernignore
Original file line number Diff line number Diff line change
@@ -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
@@ -14,3 +15,4 @@ generate.sh
http/offline.go
logger/logger.go
mocks/http_mock.go
option/custom_options.go
89 changes: 67 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -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()
}
```
@@ -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()
}
@@ -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()
}
```
@@ -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()
@@ -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/"
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{
@@ -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/"
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{
@@ -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/"
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{
@@ -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/"
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{
@@ -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/"
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{
@@ -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()
@@ -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
@@ -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

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

This file was deleted.

Loading

0 comments on commit 2489380

Please sign in to comment.