diff --git a/README.md b/README.md index b884138..a982e23 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![test](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml/badge.svg)](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml) +[![test](https://github.com/mailtrap/mailtrap-python/actions/workflows/main.yml/badge.svg)](https://github.com/mailtrap/mailtrap-python/actions/workflows/main.yml) [![PyPI](https://shields.io/pypi/v/mailtrap)](https://pypi.org/project/mailtrap/) [![downloads](https://shields.io/pypi/dm/mailtrap)](https://pypi.org/project/mailtrap/) @@ -27,34 +27,86 @@ pip install mailtrap ## Usage -### Minimal +### Minimal usage (Transactional sending) ```python import mailtrap as mt -# create mail object +API_TOKEN = "" # your API key here https://mailtrap.io/api-tokens + +client = mt.MailtrapClient(token=API_TOKEN) + +# Create mail object mail = mt.Mail( - sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"), - to=[mt.Address(email="your@email.com")], + sender=mt.Address(email="sender@example.com", name="John Smith"), + to=[mt.Address(email="recipient@example.com")], + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", +) + +client.send(mail) +``` + +### Sandbox vs Production (easy switching) + +Mailtrap lets you test safely in the Email Sandbox and then switch to Production (Sending). +Remove the inbox_id field or set it to None. Then, remove the sandbox field or set it to False. +You can change the arguments in the code or via another way. Here is an example using environment variables. + +Set next environment variables: +```bash +MAILTRAP_API_KEY=your_api_token # https://mailtrap.io/api-tokens +MAILTRAP_USE_SANDBOX=true # true/false toggle +MAILTRAP_INBOX_ID=123456 # Only needed for sandbox +``` + +Bootstrap logic: +```python +import os +import mailtrap as mt + +API_KEY = os.environ["MAILTRAP_API_KEY"] +IS_SANDBOX = os.environ.get("MAILTRAP_USE_SANDBOX", "true").lower() == "true" +INBOX_ID = os.environ.get("MAILTRAP_INBOX_ID") + +client = mt.MailtrapClient( + token=API_KEY, + sandbox=IS_SANDBOX, + inbox_id=INBOX_ID, # None is ignored for production +) + +# Create mail object +mail = mt.Mail( + sender=mt.Address(email="sender@example.com", name="John Smith"), + to=[mt.Address(email="recipient@example.com")], subject="You are awesome!", text="Congrats for sending test email with Mailtrap!", ) -# create client and send -client = mt.MailtrapClient(token="your-api-key") client.send(mail) ``` -### Full +Bulk stream example (optional) differs only by setting `bulk=True`: +`bulk_client = mt.MailtrapClient(token=API_KEY, bulk=True)` + +Recommendations: +- Use separate API tokens for Production and Sandbox. +- Keep initialisation in a single factory object/service so that switching is centralised. + +### Full-featured usage example ```python import base64 +import os from pathlib import Path import mailtrap as mt +client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) + welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes() + mail = mt.Mail( sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"), to=[mt.Address(email="your@email.com", name="Your name")], @@ -100,15 +152,17 @@ mail = mt.Mail( custom_variables={"year": 2023}, ) -client = mt.MailtrapClient(token="your-api-key") client.send(mail) ``` -### Using email template +### Minimal usage of email template ```python +import os import mailtrap as mt +client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) + # create mail object mail = mt.MailFromTemplate( sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"), @@ -117,21 +171,137 @@ mail = mt.MailFromTemplate( template_variables={"user_name": "John Doe"}, ) -# create client and send -client = mt.MailtrapClient(token="your-api-key") client.send(mail) ``` +### Sending email directly via SendingApi + +This approach is newer. It can be useful when you expect the response to be model-based rather than dictionary-based, as in MailtrapClient.send(). + +```python +import os +import mailtrap as mt + +client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) +sending_api = client.sending_api + +# create mail object +mail = mt.Mail( + sender=mt.Address(email="sender@example.com", name="John Smith"), + to=[mt.Address(email="recipient@example.com")], + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", +) + +sending_api.send(mail) +``` +#### Mailtrap sending responses difference + +#### 1. `client.send()` +**Response:** +```python +{ + "success": True, + "message_ids": ["5162954175"] +} +``` + +#### 2. `client.sending_api.send()` +**Response:** +```python +SendingMailResponse(success=True, message_ids=["5162955057"]) +``` + +The same situation applies to both `client.batch_send()` and `client.sending_api.batch_send()`. + +### All usage examples + +Refer to the [examples](examples) folder for the source code of this and other advanced examples. + +### Sending API +- [Sending minimal](examples/sending/minimal_sending.py) +- [Sending advanced](examples/sending/advanced_sending.py) +- [Sending using template](examples/sending/sending_with_template.py) + +### Batch Sending API +- [Batch sending minimal](examples/sending/batch_minimal_sending.py) +- [Batch sending advanced](examples/sending/batch_advanced_sending.py) +- [Batch sending using template](examples/sending/batch_sending_with_template.py) + +### Sandbox (Email Testing) API +- [Attachments](examples/testing/attachments.py) +- [Inboxes](examples/testing/inboxes.py) +- [Messages](examples/testing/messages.py) +- [Projects](examples/testing/projects.py) + +### Contacts API +- [Contacts](examples/contacts/contacts.py) +- [Contact Events](examples/contacts/contact_events.py) +- [Contact Exports](examples/contacts/contact_exports.py) +- [Contact Fields](examples/contacts/contact_fields.py) +- [Contact Imports](examples/contacts/contact_imports.py) +- [Contact Lists](examples/contacts/contact_lists.py) + +### Email Templates API +- [Email Templates](examples/email_templates/templates.py) + +### Suppressions API +- [Suppressions](examples/suppressions/suppressions.py) + +### General API +- [Account Accesses](examples/general/account_accesses.py) +- [Accounts](examples/general/accounts.py) +- [Billing](examples/general/billing.py) +- [Permissions](examples/general/permissions.py) + +## Supported functionality + +This Python package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io). + +Quickly integrate Mailtrap with your Python app. + +Currently, with this SDK you can: +- Email API/SMTP + - Send an email (Transactional and Bulk streams) + - Send an email with a template (Transactional and Bulk streams) + - Send a batch of emails (Transactional and Bulk streams) + - Send a batch of emails with a template (Transactional and Bulk streams) +- Email Sandbox (Testing) + - Send an email + - Send an email with a template + - Send a batch of emails + - Send a batch of emails with a template + - Messages management + - Inboxes management + - Projects management + - Attachments management +- Contacts + - Contacts management + - Contact Lists management + - Contact Fields management + - Contact Events management + - Contact Exports management + - Contact Imports management +- Suppressions + - Suppressions management (find and delete) +- Templates + - Templates management +- General + - Account access management + - Permissions management + - List accounts you have access to + - Get current billing information + ## Contributing -Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-python). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md). +Bug reports and pull requests are welcome on [GitHub](https://github.com/mailtrap/mailtrap-python). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md). ### Development Environment #### Clone the repo ```bash -https://github.com/railsware/mailtrap-python.git +git clone https://github.com/mailtrap/mailtrap-python.git cd mailtrap-python ``` diff --git a/examples/sending.py b/examples/sending.py deleted file mode 100644 index 9f0ecb6..0000000 --- a/examples/sending.py +++ /dev/null @@ -1,83 +0,0 @@ -import mailtrap as mt - -API_TOKEN = "" -INBOX_ID = "" - - -default_client = mt.MailtrapClient(token=API_TOKEN) -bulk_client = mt.MailtrapClient(token=API_TOKEN, bulk=True) -sandbox_client = mt.MailtrapClient(token=API_TOKEN, sandbox=True, inbox_id=INBOX_ID) - - -mail = mt.Mail( - sender=mt.Address(email="", name=""), - to=[mt.Address(email="")], - subject="You are awesome!", - text="Congrats for sending test email with Mailtrap!", - category="Integration Test", -) -mail_from_template = mt.MailFromTemplate( - sender=mt.Address(email="", name=""), - to=[mt.Address(email="")], - template_uuid="", - template_variables={ - "company_info_name": "Test_Company_info_name", - "name": "Test_Name", - "company_info_address": "Test_Company_info_address", - "company_info_city": "Test_Company_info_city", - "company_info_zip_code": "Test_Company_info_zip_code", - "company_info_country": "Test_Company_info_country", - }, -) - -batch_mail = mt.BatchSendEmailParams( - base=mt.BatchMail( - sender=mt.Address(email="", name=""), - subject="You are awesome!", - text="Congrats for sending test email with Mailtrap!", - category="Integration Test", - ), - requests=[ - mt.BatchEmailRequest( - to=[mt.Address(email="")], - ), - ], -) - -batch_mail_from_template = mt.BatchSendEmailParams( - base=mt.BatchMailFromTemplate( - sender=mt.Address(email="", name=""), - template_uuid="", - template_variables={ - "company_info_name": "Test_Company_info_name", - "name": "Test_Name", - "company_info_address": "Test_Company_info_address", - "company_info_city": "Test_Company_info_city", - "company_info_zip_code": "Test_Company_info_zip_code", - "company_info_country": "Test_Company_info_country", - }, - ), - requests=[ - mt.BatchEmailRequest( - to=[mt.Address(email="")], - subject="You are awesome!", - text="Congrats for sending test email with Mailtrap!", - category="Integration Test", - ), - ], -) - - -def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE: - return client.send(mail) - - -def batch_send( - client: mt.MailtrapClient, mail: mt.BatchSendEmailParams -) -> mt.BATCH_SEND_ENDPOINT_RESPONSE: - return client.batch_send(mail=mail) - - -if __name__ == "__main__": - print(send(default_client, mail)) - print(batch_send(default_client, batch_mail)) diff --git a/examples/sending/__init__.py b/examples/sending/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/sending/advanced_sending.py b/examples/sending/advanced_sending.py new file mode 100644 index 0000000..4155e70 --- /dev/null +++ b/examples/sending/advanced_sending.py @@ -0,0 +1,91 @@ +import base64 +from pathlib import Path + +import mailtrap as mt + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, + sandbox=True, + inbox_id="", + ) + + +# Image should be in the same level in directory like this python file. +welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes() + +mail = mt.Mail( + sender=mt.Address(email="", name=""), + to=[mt.Address(email="")], + cc=[mt.Address(email="cc@email.com", name="Copy to")], + bcc=[mt.Address(email="bcc@email.com", name="Hidden Recipient")], + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", + html=""" + + + + + + +
+

+ Congrats for sending test email with Mailtrap! +

+

+ Inspect it using the tabs you see above and + learn how this email can be improved. +

+ Inspect with Tabs +

+ Now send your email using our fake SMTP server and integration of your choice! +

+

Good luck! Hope it works.

+
+ + + + + """, + category="Test", + attachments=[ + mt.Attachment( + content=base64.b64encode(welcome_image), + filename="welcome.png", + disposition=mt.Disposition.INLINE, + mimetype="image/png", + content_id="welcome.png", + ) + ], + headers={"X-MT-Header": "Custom header"}, + custom_variables={"year": 2023}, +) + + +def send( + client: mt.MailtrapClient, + mail: mt.BaseMail, +) -> mt.SEND_ENDPOINT_RESPONSE: + return client.send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(send(client, mail)) diff --git a/examples/sending/batch_advanced_sending.py b/examples/sending/batch_advanced_sending.py new file mode 100644 index 0000000..7923133 --- /dev/null +++ b/examples/sending/batch_advanced_sending.py @@ -0,0 +1,101 @@ +import base64 +from pathlib import Path + +import mailtrap as mt + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, + sandbox=True, + inbox_id="", + ) + + +# Image should be in the same level in directory like this python file. +welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes() + +batch_mail = mt.BatchSendEmailParams( + base=mt.BatchMail( + sender=mt.Address(email="", name=""), + cc=[mt.Address(email="cc@email.com", name="Copy to")], + bcc=[mt.Address(email="bcc@email.com", name="Hidden Recipient")], + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", + html=""" + + + + + + +
+

+ Congrats for sending test email with Mailtrap! +

+

+ Inspect it using the tabs you see above and + learn how this email can be improved. +

+ Inspect with Tabs +

+ Now send your email using our fake SMTP server + and integration of your choice! +

+

Good luck! Hope it works.

+
+ + + + + """, + category="Test", + attachments=[ + mt.Attachment( + content=base64.b64encode(welcome_image), + filename="welcome.png", + disposition=mt.Disposition.INLINE, + mimetype="image/png", + content_id="welcome.png", + ) + ], + headers={"X-MT-Header": "Custom header"}, + custom_variables={"year": 2023}, + ), + requests=[ + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + ], +) + + +def batch_send( + client: mt.MailtrapClient, + mail: mt.BatchSendEmailParams, +) -> mt.BATCH_SEND_ENDPOINT_RESPONSE: + return client.batch_send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(batch_send(client, batch_mail)) diff --git a/examples/sending/batch_minimal_sending.py b/examples/sending/batch_minimal_sending.py new file mode 100644 index 0000000..06365d5 --- /dev/null +++ b/examples/sending/batch_minimal_sending.py @@ -0,0 +1,59 @@ +import mailtrap as mt +from mailtrap.models.mail.batch_mail import BatchSendResponse + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, sandbox=True, inbox_id="" + ) + + +batch_mail = mt.BatchSendEmailParams( + base=mt.BatchMail( + sender=mt.Address(email="", name=""), + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", + category="Integration Test", + ), + requests=[ + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + ], +) + + +def batch_send( + client: mt.MailtrapClient, + mail: mt.BatchSendEmailParams, +) -> mt.BATCH_SEND_ENDPOINT_RESPONSE: + return client.batch_send(mail) + + +def batch_send_via_sending_api( + client: mt.MailtrapClient, mail: mt.BatchSendEmailParams +) -> BatchSendResponse: + """Another way to batch_send email via Sending API""" + return client.sending_api.batch_send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(batch_send(client, batch_mail)) + print(batch_send_via_sending_api(client, batch_mail)) diff --git a/examples/sending/batch_sending_with_template.py b/examples/sending/batch_sending_with_template.py new file mode 100644 index 0000000..6996c33 --- /dev/null +++ b/examples/sending/batch_sending_with_template.py @@ -0,0 +1,56 @@ +import mailtrap as mt + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, sandbox=True, inbox_id="" + ) + + +batch_mail = mt.BatchSendEmailParams( + base=mt.BatchMailFromTemplate( + sender=mt.Address(email="", name=""), + template_uuid="", + template_variables={ + "company_info_name": "Test_Company_info_name", + "name": "Test_Name", + "company_info_address": "Test_Company_info_address", + "company_info_city": "Test_Company_info_city", + "company_info_zip_code": "Test_Company_info_zip_code", + "company_info_country": "Test_Company_info_country", + }, + ), + requests=[ + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + mt.BatchEmailRequest( + to=[mt.Address(email="")], + ), + ], +) + + +def batch_send( + client: mt.MailtrapClient, + mail: mt.BatchSendEmailParams, +) -> mt.BATCH_SEND_ENDPOINT_RESPONSE: + return client.batch_send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(batch_send(client, batch_mail)) diff --git a/examples/sending/minimal_sending.py b/examples/sending/minimal_sending.py new file mode 100644 index 0000000..17a5e8f --- /dev/null +++ b/examples/sending/minimal_sending.py @@ -0,0 +1,46 @@ +import mailtrap as mt +from mailtrap.models.mail.mail import SendingMailResponse + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, sandbox=True, inbox_id="" + ) + + +mail = mt.Mail( + sender=mt.Address(email="", name=""), + to=[mt.Address(email="")], + subject="You are awesome!", + text="Congrats for sending test email with Mailtrap!", +) + + +def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE: + return client.send(mail) + + +def send_via_sending_api( + client: mt.MailtrapClient, mail: mt.BaseMail +) -> SendingMailResponse: + """Another way to send email via Sending API""" + return client.sending_api.send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(send(client, mail)) + print(send_via_sending_api(client, mail)) diff --git a/examples/sending/sending_with_template.py b/examples/sending/sending_with_template.py new file mode 100644 index 0000000..795d872 --- /dev/null +++ b/examples/sending/sending_with_template.py @@ -0,0 +1,44 @@ +import mailtrap as mt + +API_TOKEN = "" + + +class SendingType: + DEFAULT = "default" + BULK = "bulk" + SANDBOX = "sandbox" + + +def get_client(type_: SendingType) -> mt.MailtrapClient: + if type_ == SendingType.DEFAULT: + return mt.MailtrapClient(token=API_TOKEN) + elif type_ == SendingType.BULK: + return mt.MailtrapClient(token=API_TOKEN, bulk=True) + elif type_ == SendingType.SANDBOX: + return mt.MailtrapClient( + token=API_TOKEN, sandbox=True, inbox_id="" + ) + + +mail = mt.MailFromTemplate( + sender=mt.Address(email="", name=""), + to=[mt.Address(email="")], + template_uuid="", + template_variables={ + "company_info_name": "Test_Company_info_name", + "name": "Test_Name", + "company_info_address": "Test_Company_info_address", + "company_info_city": "Test_Company_info_city", + "company_info_zip_code": "Test_Company_info_zip_code", + "company_info_country": "Test_Company_info_country", + }, +) + + +def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE: + return client.send(mail) + + +if __name__ == "__main__": + client = get_client(SendingType.DEFAULT) + print(send(client, mail)) diff --git a/mailtrap/models/contacts.py b/mailtrap/models/contacts.py index 562f647..5a4fcea 100644 --- a/mailtrap/models/contacts.py +++ b/mailtrap/models/contacts.py @@ -1,5 +1,4 @@ from datetime import datetime -from enum import Enum from typing import Any from typing import Optional from typing import Union @@ -45,11 +44,6 @@ class ContactList: name: str -class ContactStatus(str, Enum): - SUBSCRIBED = "subscribed" - UNSUBSCRIBED = "unsubscribed" - - @dataclass class CreateContactParams(RequestParams): email: str @@ -89,7 +83,7 @@ class Contact: email: str fields: dict[str, Union[str, int, float, bool]] # field_merge_tag: value list_ids: list[int] - status: ContactStatus + status: str created_at: int updated_at: int @@ -99,17 +93,10 @@ class ContactResponse: data: Contact -class ContactImportStatus(str, Enum): - CREATED = "created" - STARTED = "started" - FINISHED = "finished" - FAILED = "failed" - - @dataclass class ContactImport: id: int - status: ContactImportStatus + status: str created_contacts_count: Optional[int] = None updated_contacts_count: Optional[int] = None contacts_over_limit_count: Optional[int] = None diff --git a/mailtrap/models/messages.py b/mailtrap/models/messages.py index 6b6f6da..f980b34 100644 --- a/mailtrap/models/messages.py +++ b/mailtrap/models/messages.py @@ -1,5 +1,4 @@ from datetime import datetime -from enum import Enum from typing import Any from typing import Optional from typing import Union @@ -10,12 +9,6 @@ from mailtrap.models.common import RequestParams -class BlacklistsResult(str, Enum): - SUCCESS = "success" - PENDING = "pending" - ERROR = "error" - - @dataclass class BlacklistsReport: name: str @@ -25,7 +18,7 @@ class BlacklistsReport: @dataclass class Blacklists: - result: BlacklistsResult + result: str domain: str ip: str report: list[BlacklistsReport] @@ -117,14 +110,9 @@ class ErrorItem: email_clients: EmailClients -class AnalysisReportStatus(str, Enum): - SUCCESS = "success" - ERROR = "error" - - @dataclass class AnalysisReport: - status: AnalysisReportStatus + status: str @dataclass diff --git a/mailtrap/models/suppressions.py b/mailtrap/models/suppressions.py index ad3d15c..f4660c0 100644 --- a/mailtrap/models/suppressions.py +++ b/mailtrap/models/suppressions.py @@ -1,30 +1,17 @@ from datetime import datetime -from enum import Enum from typing import Optional from typing import Union from pydantic.dataclasses import dataclass -class SuppressionType(str, Enum): - HARD_BOUNCE = "hard bounce" - SPAM_COMPLAINT = "spam complaint" - UNSUBSCRIPTION = "unsubscription" - MANUAL_IMPORT = "manual import" - - -class SendingStream(str, Enum): - TRANSACTIONAL = "transactional" - BULK = "bulk" - - @dataclass class Suppression: id: str - type: SuppressionType + type: str created_at: datetime email: str - sending_stream: SendingStream + sending_stream: str domain_name: Optional[str] = None message_bounce_category: Optional[str] = None message_category: Optional[str] = None diff --git a/tests/unit/api/contacts/test_contact_imports.py b/tests/unit/api/contacts/test_contact_imports.py index a7074a7..e264b6f 100644 --- a/tests/unit/api/contacts/test_contact_imports.py +++ b/tests/unit/api/contacts/test_contact_imports.py @@ -8,7 +8,6 @@ from mailtrap.exceptions import APIError from mailtrap.http import HttpClient from mailtrap.models.contacts import ContactImport -from mailtrap.models.contacts import ContactImportStatus from mailtrap.models.contacts import ImportContactParams from tests import conftest @@ -135,7 +134,7 @@ def test_import_contacts_should_return_started_import( assert isinstance(contact_import, ContactImport) assert contact_import.id == IMPORT_ID - assert contact_import.status == ContactImportStatus.STARTED + assert contact_import.status == "started" @pytest.mark.parametrize( "status_code,response_json,expected_error_message", @@ -190,7 +189,7 @@ def test_get_contact_import_should_return_started_import( assert isinstance(contact_import, ContactImport) assert contact_import.id == IMPORT_ID - assert contact_import.status == ContactImportStatus.STARTED + assert contact_import.status == "started" @responses.activate def test_get_contact_import_should_return_finished_import( @@ -208,7 +207,7 @@ def test_get_contact_import_should_return_finished_import( assert isinstance(contact_import, ContactImport) assert contact_import.id == IMPORT_ID - assert contact_import.status == ContactImportStatus.FINISHED + assert contact_import.status == "finished" assert contact_import.created_contacts_count == 1 assert contact_import.updated_contacts_count == 3 assert contact_import.contacts_over_limit_count == 3 diff --git a/tests/unit/api/contacts/test_contacts.py b/tests/unit/api/contacts/test_contacts.py index 3d39bb7..1793c78 100644 --- a/tests/unit/api/contacts/test_contacts.py +++ b/tests/unit/api/contacts/test_contacts.py @@ -9,7 +9,6 @@ from mailtrap.http import HttpClient from mailtrap.models.common import DeletedObject from mailtrap.models.contacts import Contact -from mailtrap.models.contacts import ContactStatus from mailtrap.models.contacts import CreateContactParams from mailtrap.models.contacts import UpdateContactParams from tests import conftest @@ -122,7 +121,7 @@ def test_get_contact_should_return_contact( assert isinstance(contact, Contact) assert contact.id == CONTACT_ID assert contact.email == "john.smith@example.com" - assert contact.status == ContactStatus.SUBSCRIBED + assert contact.status == "subscribed" assert contact.fields["first_name"] == "John" assert contact.fields["last_name"] == "Smith" assert contact.list_ids == [1, 2, 3] @@ -210,7 +209,7 @@ def test_create_contact_should_return_created_contact( assert isinstance(contact, Contact) assert contact.id == CONTACT_ID assert contact.email == "john.smith@example.com" - assert contact.status == ContactStatus.SUBSCRIBED + assert contact.status == "subscribed" assert contact.fields["first_name"] == "John" assert contact.fields["last_name"] == "Smith" assert contact.list_ids == [1, 2, 3] @@ -297,7 +296,7 @@ def test_update_contact_should_return_updated_contact( assert isinstance(contact, Contact) assert contact.id == CONTACT_ID assert contact.email == "john.updated@example.com" - assert contact.status == ContactStatus.SUBSCRIBED + assert contact.status == "subscribed" assert contact.fields["first_name"] == "John Updated" assert contact.fields["last_name"] == "Smith Updated" diff --git a/tests/unit/api/suppressions/test_suppressions.py b/tests/unit/api/suppressions/test_suppressions.py index ee40ba4..10fd659 100644 --- a/tests/unit/api/suppressions/test_suppressions.py +++ b/tests/unit/api/suppressions/test_suppressions.py @@ -8,9 +8,7 @@ from mailtrap.config import GENERAL_HOST from mailtrap.exceptions import APIError from mailtrap.http import HttpClient -from mailtrap.models.suppressions import SendingStream from mailtrap.models.suppressions import Suppression -from mailtrap.models.suppressions import SuppressionType from tests import conftest ACCOUNT_ID = "321" @@ -168,6 +166,6 @@ def test_delete_suppression_should_return_deleted_suppression( assert isinstance(deleted_suppression, Suppression) assert deleted_suppression.id == SUPPRESSION_ID - assert deleted_suppression.type == SuppressionType.UNSUBSCRIPTION + assert deleted_suppression.type == "unsubscription" assert deleted_suppression.email == "recipient@example.com" - assert deleted_suppression.sending_stream == SendingStream.TRANSACTIONAL + assert deleted_suppression.sending_stream == "transactional" diff --git a/tests/unit/api/testing/test_messages.py b/tests/unit/api/testing/test_messages.py index 24a007e..9828069 100644 --- a/tests/unit/api/testing/test_messages.py +++ b/tests/unit/api/testing/test_messages.py @@ -543,7 +543,7 @@ def test_get_html_analysis_should_return_analysis_report( result = client.get_html_analysis(INBOX_ID, MESSAGE_ID) - assert result.status.value == "success" + assert result.status == "success" assert len(result.errors) == 1 assert result.errors[0].error_line == 15