-
Notifications
You must be signed in to change notification settings - Fork 7
Add billing API #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DagonWat
wants to merge
1
commit into
main
Choose a base branch
from
MT-19862-billing-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add billing API #93
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| require 'mailtrap' | ||
|
|
||
| account_id = 3229 | ||
| client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
| billing_api = Mailtrap::BillingAPI.new(account_id, client) | ||
|
|
||
| # Get billing information for the account | ||
| billing_api.get | ||
| # => #<Mailtrap::Billing billing: { cycle_start: '2026-02-06T12:59:54.000Z', ... }, ...> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mailtrap | ||
| # Data Transfer Object for Billing data | ||
| # @see https://docs.mailtrap.io/developers/account-management/billing | ||
| # @attr_reader billing [Hash] The billing cycles | ||
| # @attr_reader testing [Hash] Testing subscription details | ||
| # @attr_reader sending [Hash] Sending subscription details | ||
| # | ||
| Billing = Struct.new( | ||
| :billing, | ||
| :testing, | ||
| :sending, | ||
| keyword_init: true | ||
| ) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'base_api' | ||
| require_relative 'billing' | ||
|
|
||
| module Mailtrap | ||
| class BillingAPI | ||
| include BaseAPI | ||
|
|
||
| self.response_class = Billing | ||
|
|
||
| # Show billing details for the account | ||
| # @return <Billing> Billing data for account | ||
| # @!macro api_errors | ||
| def get | ||
| response = client.get(base_path) | ||
| handle_response(response) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def base_path | ||
| "/api/accounts/#{account_id}/billing/usage" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/maps_response_data_to_Billing_object.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
...ssettes/Mailtrap_BillingAPI/_get/when_api_key_is_incorrect/raises_authorization_error.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Mailtrap::BillingAPI, :vcr do | ||
| subject(:billing_api) { described_class.new(account_id, client) } | ||
|
|
||
| let(:account_id) { ENV.fetch('MAILTRAP_ACCOUNT_ID', 1_111_111) } | ||
| let(:client) { Mailtrap::Client.new(api_key: ENV.fetch('MAILTRAP_API_KEY', 'local-api-key')) } | ||
|
|
||
| describe '#get' do | ||
| subject(:get) { billing_api.get } | ||
|
|
||
| it 'maps response data to Billing object' do | ||
| expect(get).to be_a(Mailtrap::Billing) | ||
|
|
||
| expect(get).to match_struct( | ||
| billing: { cycle_start: '2026-02-06T12:59:54.000Z', cycle_end: '2026-03-06T12:59:54.000Z' }, | ||
| testing: { plan: { name: 'Team' }, | ||
| usage: { sent_messages_count: { current: 0, limit: 5000 }, | ||
| forwarded_messages_count: { current: 0, limit: 500 } } }, | ||
| sending: { plan: { name: 'Basic 10K' }, usage: { sent_messages_count: { current: 0, limit: 10_000 } } } | ||
| ) | ||
| end | ||
|
|
||
| context 'when api key is incorrect' do | ||
| let(:client) { Mailtrap::Client.new(api_key: 'incorrect-api-key') } | ||
|
|
||
| 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 | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Mailtrap::Billing do | ||
| describe '#initialize' do | ||
| subject(:billing) { described_class.new(attributes) } | ||
|
|
||
| let(:attributes) do | ||
| { | ||
| billing: { | ||
| cycle_start: '2024-02-15T21:11:59.624Z', | ||
| cycle_end: '2024-02-15T21:11:59.624Z' | ||
| }, | ||
| testing: { | ||
| plan: { | ||
| name: 'Individual' | ||
| }, | ||
| usage: { | ||
| sent_messages_count: { | ||
| current: 1234, | ||
| limit: 5000 | ||
| }, | ||
| forwarded_messages_count: { | ||
| current: 0, | ||
| limit: 100 | ||
| } | ||
| } | ||
| }, | ||
| sending: { | ||
| plan: { | ||
| name: 'Basic 10K' | ||
| }, | ||
| usage: { | ||
| sent_messages_count: { | ||
| current: 6789, | ||
| limit: 10_000 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| end | ||
|
|
||
| it 'creates a billing data with all attributes' do | ||
| expect(billing).to match_struct( | ||
| billing: { | ||
| cycle_start: '2024-02-15T21:11:59.624Z', | ||
| cycle_end: '2024-02-15T21:11:59.624Z' | ||
| }, | ||
| testing: { | ||
| plan: { | ||
| name: 'Individual' | ||
| }, | ||
| usage: { | ||
| sent_messages_count: { | ||
| current: 1234, | ||
| limit: 5000 | ||
| }, | ||
| forwarded_messages_count: { | ||
| current: 0, | ||
| limit: 100 | ||
| } | ||
| } | ||
| }, | ||
| sending: { | ||
| plan: { | ||
| name: 'Basic 10K' | ||
| }, | ||
| usage: { | ||
| sent_messages_count: { | ||
| current: 6789, | ||
| limit: 10_000 | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YARD
@returntag uses incorrect syntax.Should use square brackets per YARD convention:
@return [Billing].📝 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents