Skip to content

Add billing API#93

Draft
DagonWat wants to merge 1 commit intomainfrom
MT-19862-billing-resources
Draft

Add billing API#93
DagonWat wants to merge 1 commit intomainfrom
MT-19862-billing-resources

Conversation

@DagonWat
Copy link
Contributor

@DagonWat DagonWat commented Feb 17, 2026

Motivation

  • Add Billing API

Changes

  • Add new model and new API handler

Summary by CodeRabbit

New Features

  • Introduced a new Billing API that enables retrieval of comprehensive account billing information, including billing cycle dates, active plan details, and usage metrics across testing and sending services.

Documentation

  • Added detailed documentation and practical code examples to help users quickly implement and utilize the Billing API in their applications.

@coderabbitai
Copy link

coderabbitai bot commented Feb 17, 2026

📝 Walkthrough

Walkthrough

The pull request introduces a new Billing API integration to the Mailtrap Ruby SDK. It includes a data model for billing information, an API wrapper class, usage examples, comprehensive test coverage with VCR fixtures, and documentation updates to the changelog and README.

Changes

Cohort / File(s) Summary
Documentation & Configuration
CHANGELOG.md, README.md, lib/mailtrap/project.rb
Added Billing API entry to changelog and README general API listing. Updated documentation URL reference in Project DTO.
API Implementation
lib/mailtrap/billing.rb, lib/mailtrap/billing_api.rb, lib/mailtrap.rb
Introduced Billing Struct data model with keyword initialization. Implemented BillingAPI class with get method and base_path helper. Added require statement to main module loader.
Usage Example
examples/billing_api.rb
Added Ruby example demonstrating BillingAPI client instantiation, billing data retrieval, and expected output structure.
Test Suite
spec/mailtrap/billing_api_spec.rb, spec/mailtrap/billing_spec.rb
Created RSpec tests verifying BillingAPI#get response mapping, Billing object initialization with nested attributes, and authorization error handling.
Test Fixtures
spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/*
Added VCR cassettes recording successful billing usage API response and unauthorized (401) error scenario.

Sequence Diagram

sequenceDiagram
    actor Client
    participant Mailtrap
    participant BillingAPI
    participant HTTP as HTTP Client
    participant Server as Mailtrap Server
    participant Billing as Billing Object

    Client->>Mailtrap: Initialize with API key
    Client->>BillingAPI: Create BillingAPI wrapper
    Client->>BillingAPI: Call `#get`()
    activate BillingAPI
    BillingAPI->>BillingAPI: Construct base_path with account_id
    BillingAPI->>HTTP: GET /api/accounts/{id}/billing/usage
    deactivate BillingAPI
    activate HTTP
    HTTP->>Server: Send request with Authorization header
    deactivate HTTP
    activate Server
    Server-->>HTTP: 200 OK + JSON billing data
    deactivate Server
    activate HTTP
    HTTP-->>BillingAPI: Response received
    deactivate HTTP
    activate BillingAPI
    BillingAPI->>Billing: Map response to Billing (Struct)
    deactivate BillingAPI
    Billing-->>Client: Return Billing object with usage data
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • IgorDobryn
  • mklocek
  • i7an

Poem

🐰 Hops of joy, a billing tale,
New API shines without fail!
Structs and fixtures, tests so grand,
Usage data, now at hand! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The pull request description lacks specific details about the changes and omits required sections from the template, such as 'How to test' and 'Images and GIFs'. Expand the description with detailed bullet points under Changes, include specific testing instructions with checkboxes, and add relevant images/GIFs if applicable to demonstrate the new functionality.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add billing API' clearly and concisely summarizes the main change in the pull request, which introduces a new Billing API module, data transfer object, and associated tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch MT-19862-billing-resources

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@DagonWat DagonWat marked this pull request as draft February 17, 2026 13:19
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
spec/mailtrap/billing_api_spec.rb (1)

27-33: Consider simplifying the error expectation block.

The block form of raise_error works but could be made more concise. That said, since it intentionally tests both error.message (string) and error.messages (array), this is reasonable if the two accessors have distinct behavior.

♻️ Optional: Simplified error assertion (if `messages` check isn't needed separately)
-      it 'raises authorization error' do
-        expect { get }.to raise_error do |error|
-          expect(error).to be_a(Mailtrap::AuthorizationError)
-          expect(error.message).to include('Incorrect API token')
-          expect(error.messages.any? { |msg| msg.include?('Incorrect API token') }).to be true
-        end
-      end
+      it 'raises authorization error' do
+        expect { get }.to raise_error(Mailtrap::AuthorizationError, /Incorrect API token/)
+      end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@spec/mailtrap/billing_api_spec.rb` around lines 27 - 33, The current block
form of expect { get }.to raise_error can be simplified: replace the multiline
block in the spec with a single matcher call like expect { get }.to
raise_error(Mailtrap::AuthorizationError, /Incorrect API token/) to assert both
the class and the message in one line; if you still need to assert the
error.messages array, keep a short two-step assertion instead—first use expect {
get }.to raise_error(Mailtrap::AuthorizationError) to capture the error, then
assert on error.messages to check the array contents (refer to the raise_error
matcher and the error.messages accessor used in the existing spec).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/mailtrap/billing_api.rb`:
- Line 13: The YARD `@return` tag uses angle brackets instead of YARD's required
square-bracket syntax; update the doc comment that currently reads "@return
<Billing>" to "@return [Billing]" so the return type is correctly recognized by
YARD (i.e., replace the angle brackets with square brackets in the `@return` tag).

---

Nitpick comments:
In `@spec/mailtrap/billing_api_spec.rb`:
- Around line 27-33: The current block form of expect { get }.to raise_error can
be simplified: replace the multiline block in the spec with a single matcher
call like expect { get }.to raise_error(Mailtrap::AuthorizationError, /Incorrect
API token/) to assert both the class and the message in one line; if you still
need to assert the error.messages array, keep a short two-step assertion
instead—first use expect { get }.to raise_error(Mailtrap::AuthorizationError) to
capture the error, then assert on error.messages to check the array contents
(refer to the raise_error matcher and the error.messages accessor used in the
existing spec).

self.response_class = Billing

# Show billing details for the account
# @return <Billing> Billing data for account
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

YARD @return tag uses incorrect syntax.

Should use square brackets per YARD convention: @return [Billing].

📝 Proposed fix
-    # `@return` <Billing> Billing data for account
+    # `@return` [Billing] Billing data for account
📝 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.

Suggested change
# @return <Billing> Billing data for account
# `@return` [Billing] Billing data for account
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/mailtrap/billing_api.rb` at line 13, The YARD `@return` tag uses angle
brackets instead of YARD's required square-bracket syntax; update the doc
comment that currently reads "@return <Billing>" to "@return [Billing]" so the
return type is correctly recognized by YARD (i.e., replace the angle brackets
with square brackets in the `@return` tag).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments