Skip to content

Commit

Permalink
add new env vars
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Oct 25, 2024
1 parent 3fea866 commit 23f7fd8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.37
v0.2.38
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func run() error {

data := Data{
PackageName: "kittycad",
BaseURL: "https://api.kittycad.io",
EnvVariable: "KITTYCAD_API_TOKEN",
BaseURL: "https://api.zoo.dev",
EnvVariable: "ZOO_API_TOKEN",
Tags: []Tag{},
Examples: []string{},
Paths: []string{},
Expand Down
25 changes: 21 additions & 4 deletions cmd/tmpl/lib.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type service struct {
// You need to pass in your API token to create the client.
func NewClient(token, userAgent string) (*Client, error) {
if token == "" {
return nil, fmt.Errorf("you need to pass in an API token to create the client. Create a token at https://kittycad.io/account")
return nil, fmt.Errorf("you need to pass in an API token to create the client. Create a token at https://zoo.dev/account")
}

client := &Client{
Expand Down Expand Up @@ -59,14 +59,31 @@ func NewClient(token, userAgent string) (*Client, error) {
}

// NewClientFromEnv creates a new client for the KittyCad API, using the token
// stored in the environment variable `KITTYCAD_API_TOKEN`.
// stored in the environment variable `KITTYCAD_API_TOKEN` or `ZOO_API_TOKEN`.
// Optionally, you can pass in a different base url from the default with `ZOO_HOST`. But that
// is not recommended, unless you know what you are doing or you are hosting
// your own instance of the KittyCAD API.
func NewClientFromEnv(userAgent string) (*Client, error) {
token := os.Getenv(TokenEnvVar)
if token == "" {
return nil, fmt.Errorf("the environment variable %s must be set with your API token. Create a token at https://kittycad.io/account", TokenEnvVar)
// Try the old environment variable name.
token = os.Getenv("KITTYCAD_API_TOKEN")
if token == "" {
return nil, fmt.Errorf("the environment variable %s must be set with your API token. Create a token at https://zoo.dev/account", TokenEnvVar)
}
}

return NewClient(token, userAgent)
host := os.Getenv("ZOO_HOST")
if host == "" {
host = DefaultServerURL
}

c, err := NewClient(token, userAgent)
if err != nil {
return nil, err
}
c.WithBaseURL(host)
return c, nil
}

// WithBaseURL overrides the baseURL.
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kittycad.go.patch.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"value": {
"client": "// Create a client with your token.\nfunc ExampleNewClient() {\n\tclient, err := kittycad.NewClient(\"$TOKEN\", \"your apps user agent\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Call the client's methods.\n\tresult, err := client.Meta.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(result)\n}\n\n// - OR -\n\n// Create a new client with your token parsed from the environment\n// variable: `KITTYCAD_API_TOKEN`.\nfunc ExampleNewClientFromEnv() {\n\tclient, err := kittycad.NewClientFromEnv(\"your apps user agent\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Call the client's methods.\n\tresult, err := client.Meta.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"%#v\", result)\n}\n",
"client": "// Create a client with your token.\nfunc ExampleNewClient() {\n\tclient, err := kittycad.NewClient(\"$TOKEN\", \"your apps user agent\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Call the client's methods.\n\tresult, err := client.Meta.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(result)\n}\n\n// - OR -\n\n// Create a new client with your token parsed from the environment\n// variable: `ZOO_API_TOKEN`.\nfunc ExampleNewClientFromEnv() {\n\tclient, err := kittycad.NewClientFromEnv(\"your apps user agent\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Call the client's methods.\n\tresult, err := client.Meta.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"%#v\", result)\n}\n",
"install": "go get github.com/kittycad/kittycad.go"
},
"op": "add",
Expand Down
29 changes: 23 additions & 6 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

// DefaultServerURL is the default server URL for the KittyCad API.
const DefaultServerURL = "https://api.kittycad.io"
const DefaultServerURL = "https://api.zoo.dev"

// TokenEnvVar is the environment variable that contains the token.
const TokenEnvVar = "KITTYCAD_API_TOKEN"
const TokenEnvVar = "ZOO_API_TOKEN"

type service struct {
client *Client
Expand All @@ -26,7 +26,7 @@ type service struct {
// You need to pass in your API token to create the client.
func NewClient(token, userAgent string) (*Client, error) {
if token == "" {
return nil, fmt.Errorf("you need to pass in an API token to create the client. Create a token at https://kittycad.io/account")
return nil, fmt.Errorf("you need to pass in an API token to create the client. Create a token at https://zoo.dev/account")
}

client := &Client{
Expand Down Expand Up @@ -75,14 +75,31 @@ func NewClient(token, userAgent string) (*Client, error) {
}

// NewClientFromEnv creates a new client for the KittyCad API, using the token
// stored in the environment variable `KITTYCAD_API_TOKEN`.
// stored in the environment variable `KITTYCAD_API_TOKEN` or `ZOO_API_TOKEN`.
// Optionally, you can pass in a different base url from the default with `ZOO_HOST`. But that
// is not recommended, unless you know what you are doing or you are hosting
// your own instance of the KittyCAD API.
func NewClientFromEnv(userAgent string) (*Client, error) {
token := os.Getenv(TokenEnvVar)
if token == "" {
return nil, fmt.Errorf("the environment variable %s must be set with your API token. Create a token at https://kittycad.io/account", TokenEnvVar)
// Try the old environment variable name.
token = os.Getenv("KITTYCAD_API_TOKEN")
if token == "" {
return nil, fmt.Errorf("the environment variable %s must be set with your API token. Create a token at https://zoo.dev/account", TokenEnvVar)
}
}

host := os.Getenv("ZOO_HOST")
if host == "" {
host = DefaultServerURL
}

return NewClient(token, userAgent)
c, err := NewClient(token, userAgent)
if err != nil {
return nil, err
}
c.WithBaseURL(host)
return c, nil
}

// WithBaseURL overrides the baseURL.
Expand Down

0 comments on commit 23f7fd8

Please sign in to comment.