Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/api_tokens_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,24 @@
# List API tokens
api_tokens.list
# => [#<struct Mailtrap::ApiToken id=12345, name="My API Token", last_4_digits="x7k9", ..., token=nil>, ...]

# Get a single API token (the `token` field is nil — the full value is only returned by create/reset)
api_tokens.get(12_345)
# => #<struct Mailtrap::ApiToken id=12345, name="My API Token", ..., token=nil>

# Create a new API token. The full `token` value is returned ONLY once — store it securely.
api_tokens.create(
name: 'My API Token',
resources: [
{ resource_type: 'account', resource_id: account_id, access_level: 100 }
]
)
# => #<struct Mailtrap::ApiToken id=12345, name="My API Token", ..., token="a1b2c3d4e5f6g7h8">

# Reset a token — expires the old value (short grace period) and returns a new value once.
api_tokens.reset(12_345)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that it would be nice quality of life improvement to have api_tokens.reset(id: 12_345) so to explicitly define what is the route parameter.

Very optional

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that would not be aligned with the rest of the endpoints e.g. get, delete, ...

# => #<struct Mailtrap::ApiToken id=12345, ..., token="new-secret-value">

# Permanently delete a token
api_tokens.delete(12_345)
# => nil
43 changes: 43 additions & 0 deletions lib/mailtrap/api_tokens_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Mailtrap
class ApiTokensAPI
include BaseAPI

self.supported_options = %i[name resources].freeze

self.response_class = ApiToken

# Lists API tokens visible to the current API token
Expand All @@ -16,6 +18,47 @@ def list
base_list
end

# Retrieves a single API token by ID
# @param token_id [Integer] The API token ID
# @return [ApiToken] API token object (the `token` field is nil — full value is only
# returned by #create and #reset)
# @!macro api_errors
def get(token_id)
base_get(token_id)
end

# Creates a new API token. The full `token` value is returned ONLY ONCE — store it securely.
# @param [Hash] options The parameters to create
# @option options [String] :name Display name for the token
# @option options [Array<Hash>] :resources Permissions to assign
# - `{ resource_type:, resource_id:, access_level: }`
# @return [ApiToken] Created token (full `token` value populated)
# @!macro api_errors
# @raise [ArgumentError] If invalid options are provided
def create(options)
base_create(options)
end

# Expires the requested token and returns a new one with the same permissions.
# The old token stops working after a short grace period. The new `token` value is
# returned ONLY ONCE — store it securely. Only tokens that have not already been reset
# can be reset.
Comment thread
IgorDobryn marked this conversation as resolved.
Outdated
# @param token_id [Integer] The API token ID
# @return [ApiToken] New token (full `token` value populated)
# @!macro api_errors
def reset(token_id)
response = client.post("#{base_path}/#{token_id}/reset")
handle_response(response)
end

# Permanently deletes an API token
# @param token_id [Integer] The API token ID
# @return nil
# @!macro api_errors
def delete(token_id)
base_delete(token_id)
end

private

def base_path
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading