-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #27: Add InboxesApi, related models, tests, examples. #40
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
Open
Ihor-Bilous
wants to merge
2
commits into
main
Choose a base branch
from
ISSUE-27
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.
Open
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
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,63 @@ | ||
from typing import Optional | ||
|
||
import mailtrap as mt | ||
from mailtrap.models.inboxes import Inbox | ||
|
||
API_TOKEN = "YOU_API_TOKEN" | ||
ACCOUNT_ID = "YOU_ACCOUNT_ID" | ||
|
||
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
inboxes_api = client.testing_api.inboxes | ||
|
||
|
||
def list_inboxes() -> list[Inbox]: | ||
return inboxes_api.get_list() | ||
|
||
|
||
def create_inbox(project_id: int, inbox_name: str) -> Inbox: | ||
return inboxes_api.create( | ||
project_id=project_id, inbox_params=mt.CreateInboxParams(name=inbox_name) | ||
) | ||
|
||
|
||
def get_inbox_by_id(inbox_id: int) -> Inbox: | ||
return inboxes_api.get_by_id(inbox_id) | ||
|
||
|
||
def update_inbox( | ||
inbox_id: int, | ||
new_name: Optional[str] = None, | ||
new_email_username: Optional[str] = None, | ||
) -> Inbox: | ||
return inboxes_api.update( | ||
inbox_id, mt.UpdateInboxParams(name=new_name, email_username=new_email_username) | ||
) | ||
|
||
|
||
def clean_inbox(inbox_id: int) -> Inbox: | ||
return inboxes_api.clean(inbox_id) | ||
|
||
|
||
def mark_inbox_as_read(inbox_id: int) -> Inbox: | ||
return inboxes_api.mark_as_read(inbox_id) | ||
|
||
|
||
def reset_inbox_credentials(inbox_id: int) -> Inbox: | ||
return inboxes_api.reset_credentials(inbox_id) | ||
|
||
|
||
def enable_inbox_email_address(inbox_id: int) -> Inbox: | ||
return inboxes_api.enable_email_address(inbox_id) | ||
|
||
|
||
def reset_inbox_email_username(inbox_id: int) -> Inbox: | ||
return inboxes_api.reset_email_username(inbox_id) | ||
|
||
|
||
def delete_inbox(inbox_id: int): | ||
return inboxes_api.delete(inbox_id) | ||
|
||
|
||
if __name__ == "__main__": | ||
inboxes = list_inboxes() | ||
print(inboxes) |
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
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
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,74 @@ | ||
from typing import Optional | ||
|
||
from mailtrap.http import HttpClient | ||
from mailtrap.models.inboxes import CreateInboxParams | ||
from mailtrap.models.inboxes import Inbox | ||
from mailtrap.models.inboxes import UpdateInboxParams | ||
|
||
|
||
class InboxesApi: | ||
def __init__(self, client: HttpClient, account_id: str) -> None: | ||
self._account_id = account_id | ||
self._client = client | ||
|
||
def get_list(self) -> list[Inbox]: | ||
"""Get a list of inboxes.""" | ||
response = self._client.get(self._api_path()) | ||
return [Inbox(**inbox) for inbox in response] | ||
|
||
def get_by_id(self, inbox_id: int) -> Inbox: | ||
"""Get inbox attributes by inbox id.""" | ||
response = self._client.get(self._api_path(inbox_id)) | ||
return Inbox(**response) | ||
|
||
def create(self, project_id: int, inbox_params: CreateInboxParams) -> Inbox: | ||
"""Create an inbox in a project.""" | ||
response = self._client.post( | ||
f"/api/accounts/{self._account_id}/projects/{project_id}/inboxes", | ||
json={"inbox": inbox_params.api_data}, | ||
) | ||
return Inbox(**response) | ||
|
||
def update(self, inbox_id: int, inbox_params: UpdateInboxParams) -> Inbox: | ||
"""Update inbox name, inbox email username.""" | ||
response = self._client.patch( | ||
self._api_path(inbox_id), | ||
json={"inbox": inbox_params.api_data}, | ||
) | ||
return Inbox(**response) | ||
|
||
def delete(self, inbox_id: int) -> Inbox: | ||
"""Delete an inbox with all its emails.""" | ||
response = self._client.delete(self._api_path(inbox_id)) | ||
return Inbox(**response) | ||
|
||
def clean(self, inbox_id: int) -> Inbox: | ||
"""Delete all messages (emails) from inbox.""" | ||
response = self._client.patch(f"{self._api_path(inbox_id)}/clean") | ||
return Inbox(**response) | ||
|
||
def mark_as_read(self, inbox_id: int) -> Inbox: | ||
"""Mark all messages in the inbox as read.""" | ||
response = self._client.patch(f"{self._api_path(inbox_id)}/all_read") | ||
return Inbox(**response) | ||
|
||
def reset_credentials(self, inbox_id: int) -> Inbox: | ||
"""Reset SMTP credentials of the inbox.""" | ||
response = self._client.patch(f"{self._api_path(inbox_id)}/reset_credentials") | ||
return Inbox(**response) | ||
|
||
def enable_email_address(self, inbox_id: int) -> Inbox: | ||
"""Turn the email address of the inbox on/off.""" | ||
response = self._client.patch(f"{self._api_path(inbox_id)}/toggle_email_username") | ||
return Inbox(**response) | ||
|
||
def reset_email_username(self, inbox_id: int) -> Inbox: | ||
"""Reset username of email address per inbox.""" | ||
response = self._client.patch(f"{self._api_path(inbox_id)}/reset_email_username") | ||
return Inbox(**response) | ||
|
||
def _api_path(self, inbox_id: Optional[int] = None) -> str: | ||
path = f"/api/accounts/{self._account_id}/inboxes" | ||
if inbox_id: | ||
return f"{path}/{inbox_id}" | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.