Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
client/tier: change default BaseURL if TIER_API_KEY set (#281)
Browse files Browse the repository at this point in the history
This commit changes FromEnv to set BaseURL to https://api.tier.run if a
TIER_API_KEY is present in the environment and TIER_BASE_URL is unset.
  • Loading branch information
bmizerany committed Mar 9, 2023
1 parent 108add4 commit d87ce90
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion client/tier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ func clockFromContext(ctx context.Context) string {
// invalid URL.
func FromEnv() (*Client, error) {
key := os.Getenv("TIER_API_KEY")

baseURL := os.Getenv("TIER_BASE_URL")
if key != "" {
if baseURL == "" {
baseURL = "https://api.tier.run"
}
}
if baseURL == "" {
baseURL = defaultBaseURL
}
Expand Down
51 changes: 51 additions & 0 deletions client/tier/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,57 @@ func TestUserPassword(t *testing.T) {
diff.Test(t, t.Errorf, got, want)
}

func TestFromEnv(t *testing.T) {
cases := []struct {
envBaseURL string
envAPIKey string
wantBaseURL string
wantAPIKey string
}{
{
envBaseURL: "https://x.com",
envAPIKey: "testKey",
wantBaseURL: "https://x.com",
wantAPIKey: "testKey",
},
{
envBaseURL: "",
envAPIKey: "testKey",
wantBaseURL: "https://api.tier.run",
wantAPIKey: "testKey",
},
{
envBaseURL: "",
envAPIKey: "",
wantBaseURL: defaultBaseURL,
wantAPIKey: "",
},
{
envBaseURL: "https://y.com",
envAPIKey: "",
wantBaseURL: "https://y.com",
wantAPIKey: "",
},
}

for _, tt := range cases {
t.Run("", func(t *testing.T) {
t.Setenv("TIER_BASE_URL", tt.envBaseURL)
t.Setenv("TIER_API_KEY", tt.envAPIKey)
c, err := FromEnv()
if err != nil {
t.Fatal(err)
}
if c.BaseURL != tt.wantBaseURL {
t.Errorf("BaseURL = %q; want %q", c.BaseURL, tt.wantBaseURL)
}
if c.APIKey != tt.wantAPIKey {
t.Errorf("APIKey = %q; want %q", c.APIKey, tt.wantAPIKey)
}
})
}
}

func TestReportNow(t *testing.T) {
var mu sync.Mutex
var got []apitypes.ReportRequest
Expand Down

0 comments on commit d87ce90

Please sign in to comment.