MT-22692: API tokens & billing - #19
Conversation
📝 WalkthroughWalkthroughAdds Mailtrap client support for API token lifecycle operations and billing usage retrieval, including public models, client wiring, tests, runnable examples, and README links. ChangesManagement API services
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
bd9e144 to
ef71943
Compare
51957f4 to
d86d430
Compare
d86d430 to
60ad753
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@examples/billing/main.go`:
- Around line 23-30: Both reporting conditions can panic by dereferencing a nil
Usage field. In the Sending and Testing checks, add usage.Sending.Usage != nil
and usage.Testing.Usage != nil before accessing SentMessagesCount, while
preserving the existing output behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 20ff4f85-a598-4de2-985e-f4ed1dbb2984
📒 Files selected for processing (8)
README.mdapi_tokens.goapi_tokens_test.gobilling.gobilling_test.goclient.goexamples/api-tokens/main.goexamples/billing/main.go
| if usage.Sending != nil && usage.Sending.Usage.SentMessagesCount != nil { | ||
| sent := usage.Sending.Usage.SentMessagesCount | ||
| fmt.Printf("email sending: %d/%d messages this cycle\n", sent.Current, sent.Limit) | ||
| } | ||
| if usage.Testing != nil && usage.Testing.Usage.SentMessagesCount != nil { | ||
| sent := usage.Testing.Usage.SentMessagesCount | ||
| fmt.Printf("sandbox: %d/%d messages this cycle\n", sent.Current, sent.Limit) | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Missing nil check on Usage before accessing SentMessagesCount.
BillingProduct.Usage is a pointer (*BillingUsageMetrics) and can be nil if the API omits the usage key for a product. The condition checks usage.Sending != nil && usage.Sending.Usage.SentMessagesCount != nil but skips usage.Sending.Usage != nil — if Usage is nil, the access to SentMessagesCount panics. Same issue on lines 27-29 for Testing.
🛡️ Proposed fix: add intermediate nil checks
- if usage.Sending != nil && usage.Sending.Usage.SentMessagesCount != nil {
+ if usage.Sending != nil && usage.Sending.Usage != nil && usage.Sending.Usage.SentMessagesCount != nil {
sent := usage.Sending.Usage.SentMessagesCount
fmt.Printf("email sending: %d/%d messages this cycle\n", sent.Current, sent.Limit)
}
- if usage.Testing != nil && usage.Testing.Usage.SentMessagesCount != nil {
+ if usage.Testing != nil && usage.Testing.Usage != nil && usage.Testing.Usage.SentMessagesCount != nil {
sent := usage.Testing.Usage.SentMessagesCount
fmt.Printf("sandbox: %d/%d messages this cycle\n", sent.Current, sent.Limit)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if usage.Sending != nil && usage.Sending.Usage.SentMessagesCount != nil { | |
| sent := usage.Sending.Usage.SentMessagesCount | |
| fmt.Printf("email sending: %d/%d messages this cycle\n", sent.Current, sent.Limit) | |
| } | |
| if usage.Testing != nil && usage.Testing.Usage.SentMessagesCount != nil { | |
| sent := usage.Testing.Usage.SentMessagesCount | |
| fmt.Printf("sandbox: %d/%d messages this cycle\n", sent.Current, sent.Limit) | |
| } | |
| if usage.Sending != nil && usage.Sending.Usage != nil && usage.Sending.Usage.SentMessagesCount != nil { | |
| sent := usage.Sending.Usage.SentMessagesCount | |
| fmt.Printf("email sending: %d/%d messages this cycle\n", sent.Current, sent.Limit) | |
| } | |
| if usage.Testing != nil && usage.Testing.Usage != nil && usage.Testing.Usage.SentMessagesCount != nil { | |
| sent := usage.Testing.Usage.SentMessagesCount | |
| fmt.Printf("sandbox: %d/%d messages this cycle\n", sent.Current, sent.Limit) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@examples/billing/main.go` around lines 23 - 30, Both reporting conditions can
panic by dereferencing a nil Usage field. In the Sending and Testing checks, add
usage.Sending.Usage != nil and usage.Testing.Usage != nil before accessing
SentMessagesCount, while preserving the existing output behavior.
Motivation
Adds API token management and billing usage to the Go SDK's General surface.
Changes
client.APITokens):List/Get/Create/Reset/Delete; the full token value is returned only byCreateandReset.client.Billing):Usage— current billing-cycle usage across Sandbox, Sending & Marketing.examples/general+ README entries.Field shapes and paths verified against the OpenAPI spec (
account-management). Stacked on the accounts/access/permissions PR, whoseAccessLevel*/ResourceType*constants the API-token permissions reuse.How to test
go test -race ./...go vet ./...golangci-lint run ./...Summary by CodeRabbit