-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #24: Add batch_send method to SendingApi, add models #47
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
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
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 |
|---|---|---|
| @@ -1,23 +1,36 @@ | ||
| from typing import Optional | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.mail.base import BaseMail | ||
| from mailtrap.models.mail.base import SendingMailResponse | ||
| from mailtrap.models.mail import BaseMail | ||
| from mailtrap.models.mail import SendingMailResponse | ||
| from mailtrap.models.mail.batch_mail import BatchSendEmailParams | ||
| from mailtrap.models.mail.batch_mail import BatchSendResponse | ||
|
|
||
|
|
||
| class SendingApi: | ||
| def __init__(self, client: HttpClient, inbox_id: Optional[str] = None) -> None: | ||
| self._inbox_id = inbox_id | ||
| self._client = client | ||
|
|
||
| @property | ||
| def _api_url(self) -> str: | ||
| url = "/api/send" | ||
| def _get_api_url(self, base_url: str) -> str: | ||
| if self._inbox_id: | ||
| return f"{url}/{self._inbox_id}" | ||
| return url | ||
| return f"{base_url}/{self._inbox_id}" | ||
| return base_url | ||
|
|
||
| def send(self, mail: BaseMail) -> SendingMailResponse: | ||
| """Send email (text, html, text&html, templates).""" | ||
| response = self._client.post(self._api_url, json=mail.api_data) | ||
| response = self._client.post(self._get_api_url("/api/send"), json=mail.api_data) | ||
| return SendingMailResponse(**response) | ||
|
|
||
| def batch_send(self, mail: BatchSendEmailParams) -> BatchSendResponse: | ||
| """ | ||
| Batch send email (text, html, text&html, templates). Please note that | ||
| the endpoint will return a 200-level http status, even when sending | ||
| for individual messages may fail. Users of this endpoint should check | ||
| the success and errors for each message in the response (the results | ||
| are ordered the same as the original messages - requests). Please note | ||
| that the endpoint accepts up to 500 messages per API call, and up to 50 MB | ||
| payload size, including attachments. | ||
| """ | ||
| response = self._client.post(self._get_api_url("/api/batch"), json=mail.api_data) | ||
| return BatchSendResponse(**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 |
|---|---|---|
| @@ -1,15 +1,29 @@ | ||
| from mailtrap.models.mail.address import Address | ||
| from mailtrap.models.mail.attachment import Attachment | ||
| from mailtrap.models.mail.attachment import Disposition | ||
| from mailtrap.models.mail.base import BaseMail | ||
| from mailtrap.models.mail.from_template import MailFromTemplate | ||
| from mailtrap.models.mail.batch_mail import BaseBatchMail | ||
| from mailtrap.models.mail.batch_mail import BatchEmailRequest | ||
| from mailtrap.models.mail.batch_mail import BatchMail | ||
| from mailtrap.models.mail.batch_mail import BatchMailFromTemplate | ||
| from mailtrap.models.mail.batch_mail import BatchSendEmailParams | ||
| from mailtrap.models.mail.batch_mail import BatchSendResponse | ||
| from mailtrap.models.mail.mail import BaseMail | ||
| from mailtrap.models.mail.mail import Mail | ||
| from mailtrap.models.mail.mail import MailFromTemplate | ||
| from mailtrap.models.mail.mail import SendingMailResponse | ||
|
|
||
| __all__ = [ | ||
| "Address", | ||
| "Attachment", | ||
| "Disposition", | ||
| "BaseBatchMail", | ||
| "BaseMail", | ||
| "BatchEmailRequest", | ||
| "BatchMail", | ||
| "BatchMailFromTemplate", | ||
| "BatchSendEmailParams", | ||
| "BatchSendResponse", | ||
| "Disposition", | ||
| "Mail", | ||
| "MailFromTemplate", | ||
| "SendingMailResponse", | ||
| ] |
This file was deleted.
Oops, something went wrong.
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,69 @@ | ||
| from typing import Any | ||
| from typing import Optional | ||
| from typing import Union | ||
|
|
||
| from pydantic import Field | ||
| from pydantic.dataclasses import dataclass | ||
|
|
||
| from mailtrap.models.common import RequestParams | ||
| from mailtrap.models.mail.address import Address | ||
| from mailtrap.models.mail.attachment import Attachment | ||
|
|
||
|
|
||
| @dataclass | ||
| class BaseBatchMail(RequestParams): | ||
| sender: Address = Field(..., serialization_alias="from") | ||
| attachments: Optional[list[Attachment]] = None | ||
| headers: Optional[dict[str, str]] = None | ||
| custom_variables: Optional[dict[str, Any]] = None | ||
| reply_to: Optional[Address] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchMail(BaseBatchMail): | ||
| subject: str = Field(...) # type:ignore | ||
| text: Optional[str] = None | ||
| html: Optional[str] = None | ||
| category: Optional[str] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchMailFromTemplate(BaseBatchMail): | ||
| template_uuid: str = Field(...) # type:ignore | ||
| 6D3F template_variables: Optional[dict[str, Any]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchEmailRequest(BaseBatchMail): | ||
| to: list[Address] = Field(...) # type:ignore | ||
| sender: Optional[Address] = Field( | ||
| None, serialization_alias="from" | ||
| ) # type: ignore[assignment] | ||
| cc: Optional[list[Address]] = None | ||
| bcc: Optional[list[Address]] = None | ||
| subject: Optional[str] = None | ||
| text: Optional[str] = None | ||
| html: Optional[str] = None | ||
| category: Optional[str] = None | ||
| template_uuid: Optional[str] = None | ||
| template_variables: Optional[dict[str, Any]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchSendEmailParams(RequestParams): | ||
| base: Union[BatchMail, BatchMailFromTemplate] | ||
| requests: list[BatchEmailRequest] | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchSendResponseItem: | ||
| success: bool | ||
| message_ids: Optional[list[str]] = None | ||
| errors: Optional[list[str]] = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchSendResponse: | ||
| success: bool | ||
| responses: list[BatchSendResponseItem] | ||
| errors: Optional[list[str]] = None | ||
Ihor-Bilous marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file was deleted.
Oops, something went wrong.
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.