-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor custom code to get around import cycles testing add readme again update readme for new api changes fix typos in the readme i don't know convert SchematicClient to struct
- Loading branch information
1 parent
96b4abb
commit 488c473
Showing
22 changed files
with
1,445 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
# Specify files that shouldn't be modified by Fern | ||
|
||
buffer/* | ||
cache/* | ||
client/opts.go | ||
client/schematic_client.go | ||
client/* | ||
client/schematic.go | ||
flags/* | ||
http/* | ||
logger/* | ||
|
||
|
||
generate.go | ||
generate.sh | ||
README.md | ||
|
||
|
||
mocks/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
# schematic-go | ||
|
||
## Installation and Setup | ||
|
||
1. Install the Go library: | ||
|
||
```bash | ||
go get github.com/schematichq/schematic-go | ||
``` | ||
|
||
2. [Issue an API key](https://docs.schematichq.com/quickstart#create-an-api-key) for the appropriate environment using the [Schematic app](https://app.schematichq.com/settings/api-keys). Be sure to capture the secret key when you issue the API key; you'll only see this key once, and this is what you'll use with the Schematic Go library. | ||
|
||
3. Using this secret key, initialize a client in your Go application: | ||
|
||
```go | ||
import ( | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func main() { | ||
apiKey := os.Getenv("SCHEMATIC_API_KEY") | ||
client := schematicclient.SchematicClient(apiKey) | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
By default, the client will do some local caching for flag checks, if you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of records) and the max age of the cache (as a `time.Duration`): | ||
|
||
```go | ||
import ( | ||
"os" | ||
"time" | ||
|
||
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), | ||
) | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
You can also disable local caching entirely with an initialization option; bear in mind that, in this case, every flag check will result in a network request: | ||
|
||
```go | ||
import ( | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func main() { | ||
apiKey := os.Getenv("SCHEMATIC_API_KEY") | ||
client := schematicclient.SchematicClient(apiKey, schematicclient.WithDisabledFlagCheckCache()) | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below). You can do this using an initialization option: | ||
|
||
```go | ||
import ( | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func main() { | ||
apiKey := os.Getenv("SCHEMATIC_API_KEY") | ||
client := schematicclient.SchematicClient(apiKey, schematicclient.WithDefaultFlagValues(map[string]bool{ | ||
"some-flag-key": true, | ||
})) | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
## Usage examples | ||
|
||
### Sending identify events | ||
|
||
Create or update users and companies using identify events. | ||
|
||
```go | ||
import ( | ||
"context" | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
schematicgo "github.com/schematichq/schematic-go/" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY")) | ||
defer client.Close() | ||
|
||
client.Identify(context.Background(), &schematicgo.EventBodyIdentify{ | ||
Event: "some-action", | ||
Company: map[string]any{ | ||
"id": "your-company-id", | ||
}, | ||
User: map[string]any{ | ||
"email": "[email protected]", | ||
"user-id": "your-user-id", | ||
}, | ||
Name: "Wile E. Coyote", | ||
Traits: map[string]any{ | ||
"city": "Atlanta", | ||
"login_count": 24, | ||
"is_staff": false, | ||
}, | ||
}) | ||
} | ||
``` | ||
|
||
This call is non-blocking and there is no response to check. | ||
|
||
### Sending track events | ||
|
||
Track activity in your application using track events; these events can later be used to produce metrics for targeting. | ||
|
||
```go | ||
import ( | ||
"context" | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
schematicgo "github.com/schematichq/schematic-go/" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.SchematicClient(os.Getenv("SCHEMATIC_API_KEY")) | ||
defer client.Close() | ||
|
||
client.Track(context.Background(), &schematicgo.EventBodyTrack{ | ||
Event: "some-action", | ||
Company: map[string]any{ | ||
"id": "your-company-id", | ||
}, | ||
User: map[string]any{ | ||
"email": "[email protected]", | ||
"user-id": "your-user-id", | ||
}, | ||
}) | ||
} | ||
``` | ||
|
||
This call is non-blocking and there is no response to check. | ||
|
||
### Creating and updating companies | ||
|
||
Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation: | ||
|
||
```go | ||
import ( | ||
"context" | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
schematicgo "github.com/schematichq/schematic-go/" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.SchematicClient(os.Getenv("SCHEMATIC_API_KEY")) | ||
defer client.Close() | ||
|
||
body := &schematicgo.UpsertCompanyRequestBody{ | ||
Keys: map[string]any{ | ||
"id": "your-company-id", | ||
}, | ||
Name: "Acme Widgets, Inc.", | ||
Traits: map[string]any{ | ||
"city": "Atlanta", | ||
"high_score": 25, | ||
"is_active": true, | ||
}, | ||
}) | ||
resp, err := client.API().Companies.UpsertCompany(context.Background, body) | ||
} | ||
``` | ||
|
||
You can define any number of company keys; these are used to address the company in the future, for example by updating the company's traits or checking a flag for the company. | ||
You can also define any number of company traits; these can then be used as targeting parameters. | ||
|
||
### Creating and updating users | ||
|
||
Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation: | ||
|
||
```go | ||
import ( | ||
"context" | ||
"os" | ||
|
||
schematicclient "github.com/SchematicHQ/schematic-go/client" | ||
schematicgo "github.com/SchematicHQ/schematic-go/" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY")) | ||
defer client.Close() | ||
|
||
body := &schematicgo.UpsertUserRequestBody{ | ||
Keys: map[string]any{ | ||
"email": "[email protected]", | ||
"user-id": "your-user-id", | ||
}, | ||
Company: map[string]any{ | ||
"id": "your-company-id", | ||
}, | ||
Name: "Wile E. Coyote", | ||
Traits: map[string]any{ | ||
"city": "Atlanta", | ||
"login_count": 24, | ||
"is_staff": false, | ||
}, | ||
}) | ||
|
||
resp, err := client.API().Companies.UpsertUser(context.Background(), body) | ||
} | ||
``` | ||
|
||
You can define any number of user keys; these are used to address the user in the future, for example by updating the user's traits or checking a flag for the user. | ||
You can also define any number of user traits; these can then be used as targeting parameters. | ||
|
||
### Checking flags | ||
|
||
When checking a flag, you'll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you'll get the default value for the flag. | ||
|
||
```go | ||
import ( | ||
"context" | ||
"os" | ||
|
||
schematicclient "github.com/schematichq/schematic-go/client" | ||
schematicgo "github.com/schematichq/schematic-go/" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.NewSchematicClient(os.Getenv("SCHEMATIC_API_KEY")) | ||
defer client.Close() | ||
|
||
evaluationCtx := schematicgo.CheckFlagRequestBody{ | ||
Company: map[string]any{ | ||
"id": "your-company-id", | ||
}, | ||
User: map[string]any{ | ||
"email": "[email protected]", | ||
"user-id": "your-user-id", | ||
}, | ||
} | ||
|
||
if client.CheckFlag(context.Background(), "some-flag-key", evaluationCtx) { | ||
// Flag is on | ||
} else { | ||
// Flag is off | ||
} | ||
} | ||
``` | ||
|
||
### Other API operations | ||
|
||
The Schematic API supports many operations beyond these, accessible via `client.API()`. See the [API submodule readme](https://github.com/SchematicHQ/schematic-go/tree/main/api#readme) for a full list and documentation of supported operations. | ||
|
||
## Testing | ||
|
||
### 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: | ||
|
||
```go | ||
import ( | ||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.NewSchematicClient("") | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
Offline mode works well with flag defaults: | ||
|
||
```go | ||
import ( | ||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func main() { | ||
client := schematicclient.NewSchematicClient("", schematicclient.WithDefaultFlagValues(map[string]bool{ | ||
"some-flag-key": true, | ||
})) | ||
defer client.Close() | ||
} | ||
``` | ||
|
||
In an automated testing context, you may also want to use offline mode and specify single flag responses for test cases: | ||
|
||
```go | ||
import ( | ||
schematicclient "github.com/schematichq/schematic-go/client" | ||
) | ||
|
||
func TestSomeFunctionality(t *testing.T) { | ||
client := schematicclient.NewSchematicClient("") | ||
defer client.Close() | ||
|
||
client.SetFlagDefault("some-flag-key", true) | ||
|
||
// test code that expects the flag to be on | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.