13
13
class OneSecMail :
14
14
"""1secmail.com API wrapper"""
15
15
16
- def __init__ (self , email : str | None = None , username : str | None = None , domain : str | None = None ) -> None :
17
- if email is not None :
18
- username , domain = email .split ('@' )
16
+ def __init__ (self , address : str | None = None , username : str | None = None , domain : str | None = None ) -> None :
17
+ """Create a new 1secmail.com email address
18
+
19
+ :param address: The full email address (username@domain)
20
+ :param username: The username of the email address (before the @)
21
+ :param domain: The domain of the email address (after the @)
22
+ """
23
+
24
+ if address is not None :
25
+ username , domain = address .split ('@' )
19
26
20
27
if domain is not None and domain not in self .get_domains ():
21
28
raise ValueError (f'Invalid domain: { domain } ' )
22
29
23
- self .session = requests .Session ()
30
+ self ._session = requests .Session ()
24
31
self .username = username or utils .random_string (10 )
32
+ """The username of the email address (before the @)"""
25
33
self .domain = domain or random .choice (self .get_domains ())
34
+ """The domain of the email address (after the @)"""
26
35
27
36
def get_inbox (self ) -> list ['OneSecMail.MessageInfo' ]:
28
37
"""Get the inbox of the email address"""
29
- resp = self .session .get (f'https://www.1secmail.com/api/v1/?action=getMessages&login={ self .username } &domain={ self .domain } ' )
38
+ resp = self ._session .get (f'https://www.1secmail.com/api/v1/?action=getMessages&login={ self .username } &domain={ self .domain } ' )
30
39
resp .raise_for_status ()
31
40
return [OneSecMail .MessageInfo .from_dict (self , msg_info ) for msg_info in resp .json ()]
32
41
33
42
@utils .cache
34
43
def get_message (self , id : int ) -> 'OneSecMail.Message' :
35
44
"""Get a message from the inbox"""
36
- resp = self .session .get (f'https://www.1secmail.com/api/v1/?action=readMessage&login={ self .username } &domain={ self .domain } &id={ id } ' )
45
+ resp = self ._session .get (f'https://www.1secmail.com/api/v1/?action=readMessage&login={ self .username } &domain={ self .domain } &id={ id } ' )
37
46
resp .raise_for_status ()
38
47
return OneSecMail .Message .from_dict (self , resp .json ())
39
48
40
49
@utils .cache
41
50
def download_attachment (self , id : int , file : str ) -> bytes :
42
51
"""Download an attachment from a message as bytes"""
43
- resp = self .session .get (f'https://www.1secmail.com/api/v1/?action=download&login={ self .username } &domain={ self .domain } &id={ id } &file={ file } ' )
52
+ resp = self ._session .get (f'https://www.1secmail.com/api/v1/?action=download&login={ self .username } &domain={ self .domain } &id={ id } &file={ file } ' )
44
53
resp .raise_for_status ()
45
54
return resp .content
46
55
@@ -83,24 +92,33 @@ def __str__(self) -> str:
83
92
84
93
@dataclass
85
94
class MessageInfo :
95
+ """Information about a message in the inbox"""
96
+
86
97
id : int
98
+ "Message ID"
87
99
from_addr : str
100
+ "Sender email address"
88
101
subject : str
102
+ "Subject of the message"
89
103
date_str : str
90
- _mail : 'OneSecMail'
104
+ "Date the message was received in format YYYY-MM-DD HH:MM:SS"
105
+ _mail_instance : 'OneSecMail'
91
106
92
107
@property
93
108
def date (self ) -> datetime :
109
+ """Date the message was received"""
94
110
return datetime .fromisoformat (self .date_str )
95
111
96
112
@property
97
113
def message (self ) -> 'OneSecMail.Message' :
98
- return self ._mail .get_message (self .id )
114
+ """The full message"""
115
+ return self ._mail_instance .get_message (self .id )
99
116
100
117
@classmethod
101
- def from_dict (cls , mail : 'OneSecMail' , msg_info : dict [str , any ]) -> 'OneSecMail.MessageInfo' :
118
+ def from_dict (cls , mail_instance : 'OneSecMail' , msg_info : dict [str , any ]) -> 'OneSecMail.MessageInfo' :
119
+ """Create a MessageInfo from a raw api response"""
102
120
return cls (
103
- _mail = mail ,
121
+ _mail_instance = mail_instance ,
104
122
id = msg_info ['id' ],
105
123
from_addr = msg_info ['from' ],
106
124
subject = msg_info ['subject' ],
@@ -109,28 +127,40 @@ def from_dict(cls, mail: 'OneSecMail', msg_info: dict[str, any]) -> 'OneSecMail.
109
127
110
128
@dataclass
111
129
class Message :
130
+ """Email message"""
131
+
112
132
id : int
133
+ "Message ID"
113
134
from_addr : str
135
+ "Sender email address"
114
136
subject : str
137
+ "Subject of the message"
115
138
date_str : str
139
+ "Date the message was received in format YYYY-MM-DD HH:MM:SS"
116
140
body : str
141
+ "Message body (html if exists, text otherwise)"
117
142
text_body : str
143
+ "Message body (text format)"
118
144
html_body : str
119
- _mail : 'OneSecMail'
145
+ "Message body (html format)"
146
+ _mail_instance : 'OneSecMail'
120
147
_attachments : list [dict [str , any ]]
121
148
122
149
@property
123
150
def date (self ) -> datetime :
151
+ """Date the message was received"""
124
152
return datetime .fromisoformat (self .date_str )
125
153
126
154
@property
127
155
def attachments (self ) -> list ['OneSecMail.Attachment' ]:
128
- return [OneSecMail .Attachment .from_dict (self ._mail , self .id , attachment ) for attachment in self ._attachments ]
156
+ """List of attachments in the message (files)"""
157
+ return [OneSecMail .Attachment .from_dict (self ._mail_instance , self .id , attachment ) for attachment in self ._attachments ]
129
158
130
159
@classmethod
131
- def from_dict (cls , mail : 'OneSecMail' , msg : dict [str , any ]) -> 'OneSecMail.Message' :
160
+ def from_dict (cls , mail_instance : 'OneSecMail' , msg : dict [str , any ]) -> 'OneSecMail.Message' :
161
+ """Create a Message from a raw api response"""
132
162
return cls (
133
- _mail = mail ,
163
+ _mail_instance = mail_instance ,
134
164
_attachments = msg ['attachments' ],
135
165
id = msg ['id' ],
136
166
from_addr = msg ['from' ],
@@ -143,20 +173,26 @@ def from_dict(cls, mail: 'OneSecMail', msg: dict[str, any]) -> 'OneSecMail.Messa
143
173
144
174
@dataclass
145
175
class Attachment :
176
+ """Email attachment"""
177
+
146
178
filename : str
179
+ "Name of the file of the attachment"
147
180
content_type : str
181
+ "MIME type of the attachment"
148
182
size : int
149
- _mail : 'OneSecMail'
183
+ "Size of the attachment in bytes"
184
+ _mail_instance : 'OneSecMail'
150
185
_message_id : int
151
186
152
187
def download (self ) -> bytes :
153
188
"""Download the attachment as bytes"""
154
- return self ._mail .download_attachment (self ._message_id , self .filename )
189
+ return self ._mail_instance .download_attachment (self ._message_id , self .filename )
155
190
156
191
@classmethod
157
- def from_dict (cls , mail : 'OneSecMail' , message_id : int , attachment : dict [str , any ]) -> 'OneSecMail.Attachment' :
192
+ def from_dict (cls , mail_instance : 'OneSecMail' , message_id : int , attachment : dict [str , any ]) -> 'OneSecMail.Attachment' :
193
+ """Create an Attachment from a raw api response"""
158
194
return cls (
159
- _mail = mail ,
195
+ _mail_instance = mail_instance ,
160
196
_message_id = message_id ,
161
197
filename = attachment ['filename' ],
162
198
content_type = attachment ['contentType' ],
0 commit comments