-
Notifications
You must be signed in to change notification settings - Fork 8
Account management API #106
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
382fdde
Add ApiTokensAPI#list
IgorDobryn 596b994
Add PermissionsAPI#bulk_update
IgorDobryn 361304e
Add SubAccountsAPI#create
IgorDobryn 7dbd6ff
Add SubAccountsAPI#list
IgorDobryn 4a4fdd4
Add bin/record-vcr pointing to shared credentials set, so it's eay to…
IgorDobryn 84922ed
Use shared vault to record vcr
IgorDobryn c52c422
Fix linter
IgorDobryn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #!/usr/bin/env bash | ||
| # Run RSpec against the live Mailtrap API to record (or re-record) VCR cassettes. | ||
| # Real HTTP calls are made — only run this when you intend to update the | ||
| # cassettes under spec/fixtures/vcr_cassettes/. Credentials are injected from | ||
| # 1Password at runtime. | ||
| # | ||
| # Usage: | ||
| # bin/record-vcr # all specs | ||
| # bin/record-vcr spec/mailtrap/sending_domains_api_spec.rb # single file | ||
| # bin/record-vcr spec/mailtrap/sending_domains_api_spec.rb:42 # single example | ||
| # bin/record-vcr -e "#update" # filter by name | ||
| # bin/record-vcr --org spec/mailtrap/sub_accounts_api_spec.rb # use organization API token | ||
| # | ||
| # Pass --org as the first argument to authenticate with the organization-level token | ||
| # (needed for organization-scoped endpoints like SubAccountsAPI). Default is the | ||
| # account-level token. | ||
| # | ||
| # Requires the 1Password CLI (`op`) to be installed and signed in. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| export MAILTRAP_ACCOUNT_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_id" | ||
| export MAILTRAP_ORGANIZATION_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_id" | ||
| export MAILTRAP_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_api_token" | ||
| export MAILTRAP_ORGANIZATION_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_api_token" | ||
|
|
||
| exec op run -- bundle exec rspec "$@" |
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') | ||
| api_tokens = Mailtrap::ApiTokensAPI.new(account_id, client) | ||
|
|
||
| # List API tokens | ||
| api_tokens.list | ||
| # => [#<struct Mailtrap::ApiToken id=12345, name="My API Token", last_4_digits="x7k9", ..., token=nil>, ...] |
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,15 @@ | ||
| require 'mailtrap' | ||
|
|
||
| account_id = 3229 | ||
| client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
| permissions = Mailtrap::PermissionsAPI.new(account_id, client) | ||
|
|
||
| # Bulk-update user/token permissions on an account access. Combine create/update with destroy: | ||
| permissions.bulk_update( | ||
| 5142, # account_access_id | ||
| [ | ||
| { resource_id: '3281', resource_type: 'account', access_level: 'viewer' }, | ||
| { resource_id: '3809', resource_type: 'inbox', _destroy: true } | ||
| ] | ||
| ) | ||
| # => { message: "Permissions have been updated!" } |
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,13 @@ | ||
| require 'mailtrap' | ||
|
|
||
| organization_id = 4567 | ||
| client = Mailtrap::Client.new(api_key: 'your-api-key') | ||
| sub_accounts = Mailtrap::SubAccountsAPI.new(organization_id, client) | ||
|
|
||
| # List sub accounts under the organization | ||
| sub_accounts.list | ||
| # => [#<struct Mailtrap::SubAccount id=12345, name="Development Team Account">, ...] | ||
|
|
||
| # Create a new sub account | ||
| sub_accounts.create(name: 'New Team Account') | ||
| # => #<struct Mailtrap::SubAccount id=12347, name="New Team Account"> |
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,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mailtrap | ||
| # Data Transfer Object for API Token | ||
| # @attr_reader id [Integer] The API token ID | ||
| # @attr_reader name [String] Token display name | ||
| # @attr_reader last_4_digits [String] Last 4 characters of the token | ||
| # @attr_reader created_by [String] Name of the user or token that created this token | ||
| # @attr_reader expires_at [String, nil] When the token expires (ISO 8601); nil if it does not expire | ||
| # @attr_reader resources [Array<Hash>] Permissions granted to this token | ||
| # @attr_reader token [String, nil] Full token value — only populated by #create and #reset | ||
| ApiToken = Struct.new( | ||
| :id, | ||
| :name, | ||
| :last_4_digits, | ||
| :created_by, | ||
| :expires_at, | ||
| :resources, | ||
| :token, | ||
| 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,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'base_api' | ||
| require_relative 'api_token' | ||
|
|
||
| module Mailtrap | ||
| class ApiTokensAPI | ||
| include BaseAPI | ||
|
|
||
| self.response_class = ApiToken | ||
|
|
||
| # Lists API tokens visible to the current API token | ||
| # @return [Array<ApiToken>] Array of API tokens | ||
| # @!macro api_errors | ||
| def list | ||
| base_list | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def base_path | ||
| "/api/accounts/#{account_id}/api_tokens" | ||
| 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
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,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'base_api' | ||
|
|
||
| module Mailtrap | ||
| class PermissionsAPI | ||
| include BaseAPI | ||
|
|
||
| # Bulk-updates user or token permissions on an account access | ||
| # @param account_access_id [Integer] The account access ID | ||
| # @param permissions [Array<Hash>] Array of permission entries | ||
| # - `{ resource_id:, resource_type:, access_level: }` to create or update | ||
| # - `{ resource_id:, resource_type:, _destroy: true }` to remove | ||
| # @return [Hash] API response (e.g. `{ message: 'Permissions have been updated!' }`) | ||
| # @!macro api_errors | ||
| def bulk_update(account_access_id, permissions) | ||
| client.put( | ||
| "#{base_path}/account_accesses/#{account_access_id}/permissions/bulk", | ||
| { permissions: permissions } | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def base_path | ||
| "/api/accounts/#{account_id}" | ||
| 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,12 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mailtrap | ||
| # Data Transfer Object for an organization sub account | ||
| # @attr_reader id [Integer] The sub account ID | ||
| # @attr_reader name [String] The sub account name | ||
| SubAccount = Struct.new( | ||
| :id, | ||
| :name, | ||
| 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,53 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'base_api' | ||
| require_relative 'sub_account' | ||
|
|
||
| module Mailtrap | ||
| class SubAccountsAPI | ||
| include BaseAPI | ||
|
|
||
| attr_reader :organization_id | ||
|
|
||
| self.supported_options = %i[name].freeze | ||
|
|
||
| self.response_class = SubAccount | ||
|
|
||
| # @param organization_id [Integer] The organization ID | ||
| # @param client [Mailtrap::Client] The client instance | ||
| # @raise [ArgumentError] If organization_id is nil | ||
| def initialize(organization_id, client = Mailtrap::Client.new) | ||
| raise ArgumentError, 'organization_id is required' if organization_id.nil? | ||
|
|
||
| @organization_id = organization_id | ||
| @client = client | ||
| end | ||
|
|
||
| # Lists all sub accounts under the organization | ||
| # @return [Array<SubAccount>] Array of sub accounts | ||
| # @!macro api_errors | ||
| def list | ||
| base_list | ||
| end | ||
|
|
||
| # Creates a new sub account under the organization | ||
| # @param [Hash] options The parameters to create | ||
| # @option options [String] :name Name of the sub account | ||
| # @return [SubAccount] Created sub account | ||
| # @!macro api_errors | ||
| # @raise [ArgumentError] If invalid options are provided | ||
| def create(options) | ||
| base_create(options) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def base_path | ||
| "/api/organizations/#{organization_id}/sub_accounts" | ||
| end | ||
|
|
||
| def wrap_request(options) | ||
| { account: options } | ||
| end | ||
| end | ||
| end |
74 changes: 74 additions & 0 deletions
74
...ures/vcr_cassettes/Mailtrap_ApiTokensAPI/_list/maps_response_data_to_ApiToken_objects.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
why is it called digits, while its chars? Sounds not so good!
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.
🤷♂️