1
1
from typing import NoReturn
2
+ from typing import Optional
2
3
from typing import Union
3
4
4
5
import requests
5
6
6
7
from mailtrap .exceptions import APIError
7
8
from mailtrap .exceptions import AuthorizationError
9
+ from mailtrap .exceptions import ClientConfigurationError
8
10
from mailtrap .mail .base import BaseMail
9
11
10
12
11
13
class MailtrapClient :
12
14
DEFAULT_HOST = "send.api.mailtrap.io"
13
15
DEFAULT_PORT = 443
16
+ BULK_HOST = "bulk.api.mailtrap.io"
17
+ SANDBOX_HOST = "sandbox.api.mailtrap.io"
14
18
15
19
def __init__ (
16
20
self ,
17
21
token : str ,
18
- api_host : str = DEFAULT_HOST ,
22
+ api_host : Optional [ str ] = None ,
19
23
api_port : int = DEFAULT_PORT ,
24
+ bulk : bool = False ,
25
+ sandbox : bool = False ,
26
+ inbox_id : Optional [str ] = None ,
20
27
) -> None :
21
28
self .token = token
22
29
self .api_host = api_host
23
30
self .api_port = api_port
31
+ self .bulk = bulk
32
+ self .sandbox = sandbox
33
+ self .inbox_id = inbox_id
34
+
35
+ self ._validate_itself ()
24
36
25
37
def send (self , mail : BaseMail ) -> dict [str , Union [bool , list [str ]]]:
26
- url = f"{ self .base_url } /api/send"
27
- response = requests .post (url , headers = self .headers , json = mail .api_data )
38
+ response = requests .post (
39
+ self .api_send_url , headers = self .headers , json = mail .api_data
40
+ )
28
41
29
42
if response .ok :
30
43
data : dict [str , Union [bool , list [str ]]] = response .json ()
@@ -34,7 +47,15 @@ def send(self, mail: BaseMail) -> dict[str, Union[bool, list[str]]]:
34
47
35
48
@property
36
49
def base_url (self ) -> str :
37
- return f"https://{ self .api_host .rstrip ('/' )} :{ self .api_port } "
50
+ return f"https://{ self ._host .rstrip ('/' )} :{ self .api_port } "
51
+
52
+ @property
53
+ def api_send_url (self ) -> str :
54
+ url = f"{ self .base_url } /api/send"
55
+ if self .sandbox and self .inbox_id :
56
+ return f"{ url } /{ self .inbox_id } "
57
+
58
+ return url
38
59
39
60
@property
40
61
def headers (self ) -> dict [str , str ]:
@@ -46,6 +67,16 @@ def headers(self) -> dict[str, str]:
46
67
),
47
68
}
48
69
70
+ @property
71
+ def _host (self ) -> str :
72
+ if self .api_host :
73
+ return self .api_host
74
+ if self .sandbox :
75
+ return self .SANDBOX_HOST
76
+ if self .bulk :
77
+ return self .BULK_HOST
78
+ return self .DEFAULT_HOST
79
+
49
80
@staticmethod
50
81
def _handle_failed_response (response : requests .Response ) -> NoReturn :
51
82
status_code = response .status_code
@@ -55,3 +86,15 @@ def _handle_failed_response(response: requests.Response) -> NoReturn:
55
86
raise AuthorizationError (data ["errors" ])
56
87
57
88
raise APIError (status_code , data ["errors" ])
89
+
90
+ def _validate_itself (self ) -> None :
91
+ if self .sandbox and not self .inbox_id :
92
+ raise ClientConfigurationError ("`inbox_id` is required for sandbox mode" )
93
+
94
+ if not self .sandbox and self .inbox_id :
95
+ raise ClientConfigurationError (
96
+ "`inbox_id` is not allowed in non-sandbox mode"
97
+ )
98
+
99
+ if self .bulk and self .sandbox :
100
+ raise ClientConfigurationError ("bulk mode is not allowed in sandbox mode" )
0 commit comments