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 ReportUsage required params to positional argumen…
Browse files Browse the repository at this point in the history
…ts (#217)
  • Loading branch information
bmizerany committed Jan 13, 2023
1 parent 0896352 commit 737be1d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
12 changes: 3 additions & 9 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ func TestAPISubscribe(t *testing.T) {
t.Helper()
defer maybeFailNow(t)
fn := mpn(feature)
err := tc.ReportUsage(ctx, apitypes.ReportRequest{
Org: org,
Feature: fn,
err := tc.ReportUsage(ctx, org, fn.String(), n, &tier.ReportParams{
At: time.Now().Add(1 * time.Minute),
N: n,
Clobber: false,
})
diff.Test(t, t.Errorf, err, wantErr)
}
Expand Down Expand Up @@ -403,11 +401,7 @@ func TestTierReport(t *testing.T) {
t.Fatal(err)
}

if err := tc.ReportUsage(ctx, apitypes.ReportRequest{
Org: "org:test",
Feature: mpn("feature:t"),
N: 10,

if err := tc.ReportUsage(ctx, "org:test", "feature:t", 10, &tier.ReportParams{
// Report the usage at a time in the near future to avoid
// complaints from Stripe about being too early (e.g. the same
// start time as the current phase) or too late (e.g. > 5mins
Expand Down
2 changes: 1 addition & 1 deletion api/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ReportRequest struct {
Org string
Feature refs.Name
N int
At time.Time
At time.Time // default is time.Now()
Clobber bool
}

Expand Down
24 changes: 22 additions & 2 deletions client/tier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,29 @@ func (c *Client) Report(ctx context.Context, org, feature string, n int) error {
return err
}

type ReportParams struct {
At time.Time // default is time.Now()
Clobber bool // default is false
}

// ReportUsage reports usage based on the provided ReportRequest fields.
func (c *Client) ReportUsage(ctx context.Context, r apitypes.ReportRequest) error {
_, err := fetch.OK[struct{}, *apitypes.Error](ctx, c.client(), "POST", c.baseURL("/v1/report"), r)
func (c *Client) ReportUsage(ctx context.Context, org, feature string, n int, rp *ReportParams) error {
var p ReportParams
if rp != nil {
p = *rp
}
fn, err := refs.ParseName(feature)
if err != nil {
return err
}
r := apitypes.ReportRequest{
Org: org,
Feature: fn,
N: n,
At: p.At,
Clobber: p.Clobber,
}
_, err = fetch.OK[struct{}, *apitypes.Error](ctx, c.client(), "POST", c.baseURL("/v1/report"), r)
return err
}

Expand Down

0 comments on commit 737be1d

Please sign in to comment.