Skip to content

Commit 398ed79

Browse files
authored
Merge pull request #33 from railsware/ISSUE-25
Issue 25
2 parents ac4ca01 + 2f2a229 commit 398ed79

25 files changed

+347
-342
lines changed

examples/sending.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import mailtrap as mt
2+
3+
API_TOKEN = "<YOU_API_TOKEN>"
4+
INBOX_ID = "<YOUR_INBOX_ID>"
5+
6+
7+
default_client = mt.MailtrapClient(token=API_TOKEN)
8+
bulk_client = mt.MailtrapClient(token=API_TOKEN, bulk=True)
9+
sandbox_client = mt.MailtrapClient(token=API_TOKEN, sandbox=True, inbox_id=INBOX_ID)
10+
11+
12+
mail = mt.Mail(
13+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
14+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
15+
subject="You are awesome!",
16+
text="Congrats for sending test email with Mailtrap!",
17+
category="Integration Test",
18+
)
19+
mail_from_template = mt.MailFromTemplate(
20+
sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"),
21+
to=[mt.Address(email="<RECEIVER_EMAIL>")],
22+
template_uuid="<YOUT_TEMPLATE_UUID>",
23+
template_variables={
24+
"company_info_name": "Test_Company_info_name",
25+
"name": "Test_Name",
26+
"company_info_address": "Test_Company_info_address",
27+
"company_info_city": "Test_Company_info_city",
28+
"company_info_zip_code": "Test_Company_info_zip_code",
29+
"company_info_country": "Test_Company_info_country",
30+
},
31+
)
32+
33+
34+
def send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE:
35+
return client.send(mail)
36+
37+
38+
def batch_send(client: mt.MailtrapClient, mail: mt.BaseMail) -> mt.SEND_ENDPOINT_RESPONSE:
39+
# will be added soon
40+
pass
41+
42+
43+
if __name__ == "__main__":
44+
print(send(default_client, mail))

mailtrap/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
from .client import SEND_ENDPOINT_RESPONSE
12
from .client import MailtrapClient
23
from .exceptions import APIError
34
from .exceptions import AuthorizationError
45
from .exceptions import ClientConfigurationError
56
from .exceptions import MailtrapError
6-
from .mail import Address
7-
from .mail import Attachment
8-
from .mail import BaseEntity
9-
from .mail import BaseMail
10-
from .mail import Disposition
11-
from .mail import Mail
12-
from .mail import MailFromTemplate
7+
from .models.mail import Address
8+
from .models.mail import Attachment
9+
from .models.mail import BaseMail
10+
from .models.mail import Disposition
11+
from .models.mail import Mail
12+
from .models.mail import MailFromTemplate

mailtrap/api/sending.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional
2+
3+
from mailtrap.http import HttpClient
4+
from mailtrap.models.mail.base import BaseMail
5+
from mailtrap.models.mail.base import SendingMailResponse
6+
7+
8+
class SendingApi:
9+
def __init__(self, client: HttpClient, inbox_id: Optional[str] = None) -> None:
10+
self._inbox_id = inbox_id
11+
self._client = client
12+
13+
@property
14+
def _api_url(self) -> str:
15+
url = "/api/send"
16+
if self._inbox_id:
17+
return f"{url}/{self._inbox_id}"
18+
return url
19+
20+
def send(self, mail: BaseMail) -> SendingMailResponse:
21+
response = self._client.post(self._api_url, json=mail.api_data)
22+
return SendingMailResponse(**response)

mailtrap/client.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
from typing import NoReturn
1+
import warnings
22
from typing import Optional
33
from typing import Union
44
from typing import cast
55

6-
import requests
6+
from pydantic import TypeAdapter
77

8+
from mailtrap.api.sending import SendingApi
89
from mailtrap.api.testing import TestingApi
10+
from mailtrap.config import BULK_HOST
911
from mailtrap.config import GENERAL_HOST
10-
from mailtrap.exceptions import APIError
11-
from mailtrap.exceptions import AuthorizationError
12+
from mailtrap.config import SANDBOX_HOST
13+
from mailtrap.config import SENDING_HOST
1214
from mailtrap.exceptions import ClientConfigurationError
1315
from mailtrap.http import HttpClient
14-
from mailtrap.mail.base import BaseMail
16+
from mailtrap.models.mail import BaseMail
17+
from mailtrap.models.mail.base import SendingMailResponse
18+
19+
SEND_ENDPOINT_RESPONSE = dict[str, Union[bool, list[str]]]
1520

1621

1722
class MailtrapClient:
18-
DEFAULT_HOST = "send.api.mailtrap.io"
23+
DEFAULT_HOST = SENDING_HOST
1924
DEFAULT_PORT = 443
20-
BULK_HOST = "bulk.api.mailtrap.io"
21-
SANDBOX_HOST = "sandbox.api.mailtrap.io"
25+
BULK_HOST = BULK_HOST
26+
SANDBOX_HOST = SANDBOX_HOST
2227

2328
def __init__(
2429
self,
@@ -49,27 +54,37 @@ def testing_api(self) -> TestingApi:
4954
client=HttpClient(host=GENERAL_HOST, headers=self.headers),
5055
)
5156

52-
def send(self, mail: BaseMail) -> dict[str, Union[bool, list[str]]]:
53-
response = requests.post(
54-
self.api_send_url, headers=self.headers, json=mail.api_data
57+
@property
58+
def sending_api(self) -> SendingApi:
59+
http_client = HttpClient(host=self._sending_api_host, headers=self.headers)
60+
return SendingApi(client=http_client, inbox_id=self.inbox_id)
61+
62+
def send(self, mail: BaseMail) -> SEND_ENDPOINT_RESPONSE:
63+
sending_response = self.sending_api.send(mail)
64+
return cast(
65+
SEND_ENDPOINT_RESPONSE,
66+
TypeAdapter(SendingMailResponse).dump_python(sending_response),
5567
)
5668

57-
if response.ok:
58-
data: dict[str, Union[bool, list[str]]] = response.json()
59-
return data
60-
61-
self._handle_failed_response(response)
62-
6369
@property
6470
def base_url(self) -> str:
65-
return f"https://{self._host.rstrip('/')}:{self.api_port}"
71+
warnings.warn(
72+
"base_url is deprecated and will be removed in a future release.",
73+
DeprecationWarning,
74+
stacklevel=2,
75+
)
76+
return f"https://{self._sending_api_host.rstrip('/')}:{self.api_port}"
6677

6778
@property
6879
def api_send_url(self) -> str:
80+
warnings.warn(
81+
"api_send_url is deprecated and will be removed in a future release.",
82+
DeprecationWarning,
83+
stacklevel=2,
84+
)
6985
url = f"{self.base_url}/api/send"
7086
if self.sandbox and self.inbox_id:
7187
return f"{url}/{self.inbox_id}"
72-
7388
return url
7489

7590
@property
@@ -83,24 +98,18 @@ def headers(self) -> dict[str, str]:
8398
}
8499

85100
@property
86-
def _host(self) -> str:
101+
def _sending_api_host(self) -> str:
87102
if self.api_host:
88103
return self.api_host
89104
if self.sandbox:
90-
return self.SANDBOX_HOST
105+
return SANDBOX_HOST
91106
if self.bulk:
92-
return self.BULK_HOST
93-
return self.DEFAULT_HOST
94-
95-
@staticmethod
96-
def _handle_failed_response(response: requests.Response) -> NoReturn:
97-
status_code = response.status_code
98-
data = response.json()
107+
return BULK_HOST
108+
return SENDING_HOST
99109

100-
if status_code == 401:
101-
raise AuthorizationError(data["errors"])
102-
103-
raise APIError(status_code, data["errors"])
110+
def _validate_account_id(self) -> None:
111+
if not self.account_id:
112+
raise ClientConfigurationError("`account_id` is required for Testing API")
104113

105114
def _validate_itself(self) -> None:
106115
if self.sandbox and not self.inbox_id:
@@ -113,7 +122,3 @@ def _validate_itself(self) -> None:
113122

114123
if self.bulk and self.sandbox:
115124
raise ClientConfigurationError("bulk mode is not allowed in sandbox mode")
116-
117-
def _validate_account_id(self) -> None:
118-
if not self.account_id:
119-
raise ClientConfigurationError("`account_id` is required for Testing API")

mailtrap/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
GENERAL_HOST = "mailtrap.io"
2+
BULK_HOST = "bulk.api.mailtrap.io"
3+
SANDBOX_HOST = "sandbox.api.mailtrap.io"
4+
SENDING_HOST = "send.api.mailtrap.io"
25

36
DEFAULT_REQUEST_TIMEOUT = 30 # in seconds

mailtrap/mail/__init__.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

mailtrap/mail/address.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

mailtrap/mail/attachment.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

mailtrap/mail/base.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

mailtrap/mail/base_entity.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)