1
1
from typing import NoReturn
2
2
from typing import Optional
3
3
from typing import Union
4
+ from typing import cast
4
5
5
6
import requests
6
7
7
8
from mailtrap .api .testing import TestingApi
8
- from mailtrap .config import GENERAL_ENDPOINT
9
+ from mailtrap .config import GENERAL_HOST
9
10
from mailtrap .exceptions import APIError
10
11
from mailtrap .exceptions import AuthorizationError
11
12
from mailtrap .exceptions import ClientConfigurationError
@@ -26,17 +27,28 @@ def __init__(
26
27
api_port : int = DEFAULT_PORT ,
27
28
bulk : bool = False ,
28
29
sandbox : bool = False ,
30
+ account_id : Optional [str ] = None ,
29
31
inbox_id : Optional [str ] = None ,
30
32
) -> None :
31
33
self .token = token
32
34
self .api_host = api_host
33
35
self .api_port = api_port
34
36
self .bulk = bulk
35
37
self .sandbox = sandbox
38
+ self .account_id = account_id
36
39
self .inbox_id = inbox_id
37
40
38
41
self ._validate_itself ()
39
42
43
+ @property
44
+ def testing_api (self ) -> TestingApi :
45
+ self ._validate_account_id ()
46
+ return TestingApi (
47
+ account_id = cast (str , self .account_id ),
48
+ inbox_id = self .inbox_id ,
49
+ client = HttpClient (host = GENERAL_HOST , headers = self .headers ),
50
+ )
51
+
40
52
def send (self , mail : BaseMail ) -> dict [str , Union [bool , list [str ]]]:
41
53
response = requests .post (
42
54
self .api_send_url , headers = self .headers , json = mail .api_data
@@ -102,21 +114,6 @@ def _validate_itself(self) -> None:
102
114
if self .bulk and self .sandbox :
103
115
raise ClientConfigurationError ("bulk mode is not allowed in sandbox mode" )
104
116
105
-
106
- class MailtrapApiClient :
107
- def __init__ (self , token : str ) -> None :
108
- self .token = token
109
-
110
- def testing_api (self , account_id : str , inbox_id : Optional [str ] = None ) -> TestingApi :
111
- http_client = HttpClient (host = GENERAL_ENDPOINT , headers = self .headers )
112
- return TestingApi (account_id = account_id , inbox_id = inbox_id , client = http_client )
113
-
114
- @property
115
- def headers (self ) -> dict [str , str ]:
116
- return {
117
- "Authorization" : f"Bearer { self .token } " ,
118
- "Content-Type" : "application/json" ,
119
- "User-Agent" : (
120
- "mailtrap-python (https://github.com/railsware/mailtrap-python)"
121
- ),
122
- }
117
+ def _validate_account_id (self ) -> None :
118
+ if not self .account_id :
119
+ raise ClientConfigurationError ("`account_id` is required for Testing API" )
0 commit comments