-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #42: Add GeneralApi, related models, examples, tests. #48
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
2 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,23 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.accounts import AccountAccess | ||
| from mailtrap.models.common import DeletedObject | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| account_accesses_api = client.general_api.account_accesses | ||
|
|
||
|
|
||
| def get_account_accesses(account_id: int) -> list[AccountAccess]: | ||
| return account_accesses_api.get_list(account_id=account_id) | ||
|
|
||
|
|
||
| def delete_account_access(account_id: int, account_access_id: int) -> DeletedObject: | ||
| return account_accesses_api.delete( | ||
| account_id=account_id, account_access_id=account_access_id | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_account_accesses(ACCOUNT_ID)) | ||
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 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.accounts import Account | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| accounts_api = client.general_api.accounts | ||
|
|
||
|
|
||
| def get_accounts() -> list[Account]: | ||
| return accounts_api.get_list() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_accounts()) |
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 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.billing import BillingCycleUsage | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
Ihor-Bilous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| billing_api = client.general_api.billing | ||
|
|
||
|
|
||
| def get_current_billing_usage(account_id: int) -> BillingCycleUsage: | ||
| return billing_api.get_current_billing_usage(account_id=account_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_current_billing_usage(ACCOUNT_ID)) | ||
Ihor-Bilous marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
|
||
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 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.permissions import PermissionResource | ||
| from mailtrap.models.permissions import UpdatePermissionsResponse | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
Ihor-Bilous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| permissions_api = client.general_api.permissions | ||
|
|
||
|
|
||
| def get_permission_resources(account_id: int) -> list[PermissionResource]: | ||
| return permissions_api.get_resources(account_id=account_id) | ||
|
|
||
|
|
||
| def bulk_permissions_update( | ||
| account_id: int, | ||
| account_access_id: int, | ||
| permissions: list[mt.PermissionResourceParams], | ||
| ) -> UpdatePermissionsResponse: | ||
| return permissions_api.bulk_permissions_update( | ||
| account_id=account_id, | ||
| account_access_id=account_access_id, | ||
| permissions=permissions, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(get_permission_resources(ACCOUNT_ID)) | ||
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,26 @@ | ||
| from mailtrap.api.resources.account_accesses import AccountAccessesApi | ||
| from mailtrap.api.resources.accounts import AccountsApi | ||
| from mailtrap.api.resources.billing import BillingApi | ||
| from mailtrap.api.resources.permissions import PermissionsApi | ||
| from mailtrap.http import HttpClient | ||
|
|
||
|
|
||
| class GeneralApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| @property | ||
| def accounts(self) -> AccountsApi: | ||
| return AccountsApi(client=self._client) | ||
|
|
||
| @property | ||
| def account_accesses(self) -> AccountAccessesApi: | ||
| return AccountAccessesApi(client=self._client) | ||
|
|
||
| @property | ||
| def billing(self) -> BillingApi: | ||
| return BillingApi(client=self._client) | ||
|
|
||
| @property | ||
| def permissions(self) -> PermissionsApi: | ||
| return PermissionsApi(client=self._client) |
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,43 @@ | ||
| from typing import Optional | ||
| from urllib.parse import quote | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.accounts import AccountAccess | ||
| from mailtrap.models.accounts import AccountAccessFilterParams | ||
| from mailtrap.models.common import DeletedObject | ||
|
|
||
|
|
||
| class AccountAccessesApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get_list( | ||
| self, account_id: int, filter_params: Optional[AccountAccessFilterParams] = None | ||
| ) -> list[AccountAccess]: | ||
| """ | ||
| Get list of account accesses for which specifier_type is User or Invite. | ||
| You have to have account admin/owner permissions for this endpoint to work. | ||
| If you specify project_ids, inbox_ids or domain_ids, the endpoint will return | ||
| account accesses for these resources. | ||
| """ | ||
| response = self._client.get( | ||
| self._api_path(account_id), | ||
| params=filter_params.api_data if filter_params else None, | ||
| ) | ||
| return [AccountAccess(**account_access) for account_access in response] | ||
|
|
||
| def delete(self, account_id: int, account_access_id: int) -> DeletedObject: | ||
| """ | ||
| If specifier type is User, it removes user permissions. | ||
| If specifier type is Invite or ApiToken, it removes specifier | ||
| along with permissions. You have to be an account admin/owner | ||
| for this method to work. | ||
| """ | ||
| self._client.delete(self._api_path(account_id, account_access_id)) | ||
| return DeletedObject(account_access_id) | ||
|
|
||
| def _api_path(self, account_id: int, account_access_id: Optional[int] = None) -> str: | ||
| path = f"/api/accounts/{account_id}/account_accesses" | ||
| if account_access_id is not None: | ||
| return f"{path}/{quote(str(account_access_id), safe='')}" | ||
| return path |
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 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.accounts import Account | ||
|
|
||
|
|
||
| class AccountsApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get_list(self) -> list[Account]: | ||
| """Get a list of your Mailtrap accounts.""" | ||
| response = self._client.get("/api/accounts") | ||
| return [Account(**account) for account in response] |
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 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.billing import BillingCycleUsage | ||
|
|
||
|
|
||
| class BillingApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get_current_billing_usage(self, account_id: int) -> BillingCycleUsage: | ||
| """Get current billing cycle usage for Email Testing and Email Sending.""" | ||
| response = self._client.get(f"/api/accounts/{account_id}/billing/usage") | ||
| return BillingCycleUsage(**response) |
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,39 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.permissions import PermissionResource | ||
| from mailtrap.models.permissions import PermissionResourceParams | ||
| from mailtrap.models.permissions import UpdatePermissionsResponse | ||
|
|
||
|
|
||
| class PermissionsApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get_resources(self, account_id: int) -> list[PermissionResource]: | ||
| """ | ||
| Get all resources in your account (Inboxes, Projects, Domains, | ||
| Email Campaigns, Billing and Account itself) to which the token | ||
| has admin access. | ||
| """ | ||
| response = self._client.get(f"/api/accounts/{account_id}/permissions/resources") | ||
| return [PermissionResource(**resource) for resource in response] | ||
|
|
||
| def bulk_permissions_update( | ||
| self, | ||
| account_id: int, | ||
| account_access_id: int, | ||
| permissions: list[PermissionResourceParams], | ||
| ) -> UpdatePermissionsResponse: | ||
| """ | ||
| Manage user or token permissions. For this endpoint, you should send | ||
| an array of objects (in JSON format) as the body of the request. | ||
| If you send a combination of resource_type and resource_id that already exists, | ||
| the permission is updated. If the combination doesn't exist, | ||
| the permission is created. | ||
| """ | ||
| response = self._client.put( | ||
| f"/api/accounts/{account_id}" | ||
| f"/account_accesses/{account_access_id}" | ||
| "/permissions/bulk", | ||
| json={"permissions": [resource.api_data for resource in permissions]}, | ||
| ) | ||
| return UpdatePermissionsResponse(**response) |
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,44 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
| from mailtrap.models.permissions import Permissions | ||
|
|
||
|
|
||
| @dataclass | ||
| class AccountAccessFilterParams(RequestParams): | ||
| domain_ids: Optional[list[str]] = None | ||
| inbox_ids: Optional[list[str]] = None | ||
| project_ids: Optional[list[str]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class Account: | ||
| id: int | ||
| name: str | ||
| access_levels: list[int] | ||
|
|
||
|
|
||
| @dataclass | ||
| class Specifier: | ||
| id: int | ||
| email: str | ||
| name: str | ||
| two_factor_authentication_enabled: bool | ||
|
|
||
|
|
||
| @dataclass | ||
| class AccountAccessResource: | ||
| resource_id: int | ||
| resource_type: str | ||
| access_level: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class AccountAccess: | ||
| id: int | ||
| specifier_type: str | ||
| specifier: Specifier | ||
| resources: list[AccountAccessResource] | ||
| permissions: Permissions |
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,50 @@ | ||
| from datetime import datetime | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass | ||
| class MessagesCount: | ||
| current: int | ||
| limit: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class UsageTesting: | ||
| sent_messages_count: MessagesCount | ||
| forwarded_messages_count: MessagesCount | ||
|
|
||
|
|
||
| @dataclass | ||
| class UsageSending: | ||
| sent_messages_count: MessagesCount | ||
|
|
||
|
|
||
| @dataclass | ||
| class Plan: | ||
| name: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class Testing: | ||
| plan: Plan | ||
| usage: UsageTesting | ||
|
|
||
|
|
||
| @dataclass | ||
| class Sending: | ||
| plan: Plan | ||
| usage: UsageSending | ||
|
|
||
|
|
||
| @dataclass | ||
| class Billing: | ||
| cycle_start: datetime | ||
| cycle_end: datetime | ||
|
|
||
|
|
||
| @dataclass | ||
| class BillingCycleUsage: | ||
| billing: Billing | ||
| testing: Testing | ||
| sending: Sending |
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 |
|---|---|---|
| @@ -1,9 +1,35 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
|
|
||
|
|
||
| @dataclass | ||
| class Permissions: | ||
| can_read: bool | ||
| can_update: bool | ||
| can_destroy: bool | ||
| can_leave: bool | ||
|
|
||
|
|
||
| @dataclass | ||
| class PermissionResource: | ||
| id: int | ||
| name: str | ||
| type: str | ||
| access_level: int | ||
| resources: list["PermissionResource"] | ||
|
|
||
|
|
||
| @dataclass | ||
| class PermissionResourceParams(RequestParams): | ||
| resource_id: str | ||
| resource_type: str | ||
| access_level: Optional[str] = None | ||
| _destroy: Optional[bool] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class UpdatePermissionsResponse: | ||
| message: str |
Empty file.
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.
Uh oh!
There was an error while loading. Please reload this page.