|
| 1 | +from typing import Any |
| 2 | +from typing import Optional |
| 3 | +from typing import cast |
| 4 | + |
| 5 | +from mailtrap.http import HttpClient |
| 6 | +from mailtrap.models.messages import AnalysisReport |
| 7 | +from mailtrap.models.messages import AnalysisReportResponse |
| 8 | +from mailtrap.models.messages import EmailMessage |
| 9 | +from mailtrap.models.messages import ForwardedMessage |
| 10 | +from mailtrap.models.messages import SpamReport |
| 11 | +from mailtrap.models.messages import UpdateEmailMessageParams |
| 12 | + |
| 13 | + |
| 14 | +class MessagesApi: |
| 15 | + def __init__(self, client: HttpClient, account_id: str) -> None: |
| 16 | + self._account_id = account_id |
| 17 | + self._client = client |
| 18 | + |
| 19 | + def show_message(self, inbox_id: int, message_id: int) -> EmailMessage: |
| 20 | + """Get email message by ID.""" |
| 21 | + response = self._client.get(self._api_path(inbox_id, message_id)) |
| 22 | + return EmailMessage(**response) |
| 23 | + |
| 24 | + def update( |
| 25 | + self, inbox_id: int, message_id: int, message_params: UpdateEmailMessageParams |
| 26 | + ) -> EmailMessage: |
| 27 | + """ |
| 28 | + Update message attributes |
| 29 | + (right now only the **is_read** attribute is available for modification). |
| 30 | + """ |
| 31 | + response = self._client.patch( |
| 32 | + self._api_path(inbox_id, message_id), |
| 33 | + json={"message": message_params.api_data}, |
| 34 | + ) |
| 35 | + return EmailMessage(**response) |
| 36 | + |
| 37 | + def delete(self, inbox_id: int, message_id: int) -> EmailMessage: |
| 38 | + """Delete message from inbox.""" |
| 39 | + response = self._client.delete(self._api_path(inbox_id, message_id)) |
| 40 | + return EmailMessage(**response) |
| 41 | + |
| 42 | + def get_list( |
| 43 | + self, |
| 44 | + inbox_id: int, |
| 45 | + search: Optional[str] = None, |
| 46 | + last_id: Optional[int] = None, |
| 47 | + page: Optional[int] = None, |
| 48 | + ) -> list[EmailMessage]: |
| 49 | + """ |
| 50 | + Get messages from the inbox. |
| 51 | +
|
| 52 | + The response contains up to 30 messages per request. You can use pagination |
| 53 | + parameters (`last_id` or `page`) to retrieve additional results. |
| 54 | +
|
| 55 | + Args: |
| 56 | + inbox_id (int): ID of the inbox to retrieve messages from. |
| 57 | + search (Optional[str]): |
| 58 | + Search query string. Matches `subject`, `to_email`, and `to_name`. |
| 59 | + Example: `"welcome"` |
| 60 | + last_id (Optional[int]): |
| 61 | + If specified, returns a page of records before the given `last_id`. |
| 62 | + Overrides `page` if both are provided. |
| 63 | + Must be `>= 1`. |
| 64 | + Example: `123` |
| 65 | + page (Optional[int]): |
| 66 | + Page number for paginated results. |
| 67 | + Ignored if `last_id` is also provided. |
| 68 | + Must be `>= 1`. |
| 69 | + Example: `5` |
| 70 | +
|
| 71 | + Returns: |
| 72 | + list[EmailMessage]: A list of email messages. |
| 73 | +
|
| 74 | + Notes: |
| 75 | + - Only one of `last_id` or `page` should typically be used. |
| 76 | + - `last_id` has higher priority if both are provided. |
| 77 | + - Each response contains at most 30 messages. |
| 78 | + """ |
| 79 | + params: dict[str, Any] = {} |
| 80 | + if search: |
| 81 | + params["search"] = search |
| 82 | + if last_id: |
| 83 | + params["last_id"] = last_id |
| 84 | + if page: |
| 85 | + params["page"] = page |
| 86 | + |
| 87 | + response = self._client.get(self._api_path(inbox_id), params=params) |
| 88 | + return [EmailMessage(**message) for message in response] |
| 89 | + |
| 90 | + def forward(self, inbox_id: int, message_id: int, email: str) -> ForwardedMessage: |
| 91 | + """ |
| 92 | + Forward message to an email address. |
| 93 | + The email address must be confirmed by the recipient in advance. |
| 94 | + """ |
| 95 | + response = self._client.post( |
| 96 | + f"{self._api_path(inbox_id, message_id)}/forward", json={"email": email} |
| 97 | + ) |
| 98 | + return ForwardedMessage(**response) |
| 99 | + |
| 100 | + def get_spam_report(self, inbox_id: int, message_id: int) -> SpamReport: |
| 101 | + """Get a brief spam report by message ID.""" |
| 102 | + response = self._client.get(f"{self._api_path(inbox_id, message_id)}/spam_report") |
| 103 | + return SpamReport(**response["report"]) |
| 104 | + |
| 105 | + def get_html_analysis(self, inbox_id: int, message_id: int) -> AnalysisReport: |
| 106 | + """Get a brief HTML report by message ID.""" |
| 107 | + response = self._client.get(f"{self._api_path(inbox_id, message_id)}/analyze") |
| 108 | + return AnalysisReportResponse(**response).report |
| 109 | + |
| 110 | + def get_text_body(self, inbox_id: int, message_id: int) -> str: |
| 111 | + """Get text email body, if it exists.""" |
| 112 | + return cast( |
| 113 | + str, self._client.get(f"{self._api_path(inbox_id, message_id)}/body.txt") |
| 114 | + ) |
| 115 | + |
| 116 | + def get_raw_body(self, inbox_id: int, message_id: int) -> str: |
| 117 | + """Get raw email body.""" |
| 118 | + return cast( |
| 119 | + str, self._client.get(f"{self._api_path(inbox_id, message_id)}/body.raw") |
| 120 | + ) |
| 121 | + |
| 122 | + def get_html_source(self, inbox_id: int, message_id: int) -> str: |
| 123 | + """Get HTML source of email.""" |
| 124 | + return cast( |
| 125 | + str, |
| 126 | + self._client.get(f"{self._api_path(inbox_id, message_id)}/body.htmlsource"), |
| 127 | + ) |
| 128 | + |
| 129 | + def get_html_body(self, inbox_id: int, message_id: int) -> str: |
| 130 | + """Get formatted HTML email body. Not applicable for plain text emails.""" |
| 131 | + return cast( |
| 132 | + str, self._client.get(f"{self._api_path(inbox_id, message_id)}/body.html") |
| 133 | + ) |
| 134 | + |
| 135 | + def get_eml_body(self, inbox_id: int, message_id: int) -> str: |
| 136 | + """Get email message in .eml format.""" |
| 137 | + return cast( |
| 138 | + str, self._client.get(f"{self._api_path(inbox_id, message_id)}/body.eml") |
| 139 | + ) |
| 140 | + |
| 141 | + def get_mail_headers(self, inbox_id: int, message_id: int) -> dict[str, Any]: |
| 142 | + """Get mail headers of a message.""" |
| 143 | + response = self._client.get( |
| 144 | + f"{self._api_path(inbox_id, message_id)}/mail_headers" |
| 145 | + ) |
| 146 | + return cast(dict[str, Any], response["headers"]) |
| 147 | + |
| 148 | + def _api_path(self, inbox_id: int, message_id: Optional[int] = None) -> str: |
| 149 | + path = f"/api/accounts/{self._account_id}/inboxes/{inbox_id}/messages" |
| 150 | + if message_id: |
| 151 | + return f"{path}/{message_id}" |
| 152 | + return path |
0 commit comments