1313class OneSecMail :
1414 """1secmail.com API wrapper"""
1515
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 ('@' )
1926
2027 if domain is not None and domain not in self .get_domains ():
2128 raise ValueError (f'Invalid domain: { domain } ' )
2229
23- self .session = requests .Session ()
30+ self ._session = requests .Session ()
2431 self .username = username or utils .random_string (10 )
32+ """The username of the email address (before the @)"""
2533 self .domain = domain or random .choice (self .get_domains ())
34+ """The domain of the email address (after the @)"""
2635
2736 def get_inbox (self ) -> list ['OneSecMail.MessageInfo' ]:
2837 """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 } ' )
3039 resp .raise_for_status ()
3140 return [OneSecMail .MessageInfo .from_dict (self , msg_info ) for msg_info in resp .json ()]
3241
3342 @utils .cache
3443 def get_message (self , id : int ) -> 'OneSecMail.Message' :
3544 """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 } ' )
3746 resp .raise_for_status ()
3847 return OneSecMail .Message .from_dict (self , resp .json ())
3948
4049 @utils .cache
4150 def download_attachment (self , id : int , file : str ) -> bytes :
4251 """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 } ' )
4453 resp .raise_for_status ()
4554 return resp .content
4655
@@ -83,24 +92,33 @@ def __str__(self) -> str:
8392
8493 @dataclass
8594 class MessageInfo :
95+ """Information about a message in the inbox"""
96+
8697 id : int
98+ "Message ID"
8799 from_addr : str
100+ "Sender email address"
88101 subject : str
102+ "Subject of the message"
89103 date_str : str
90- _mail : 'OneSecMail'
104+ "Date the message was received in format YYYY-MM-DD HH:MM:SS"
105+ _mail_instance : 'OneSecMail'
91106
92107 @property
93108 def date (self ) -> datetime :
109+ """Date the message was received"""
94110 return datetime .fromisoformat (self .date_str )
95111
96112 @property
97113 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 )
99116
100117 @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"""
102120 return cls (
103- _mail = mail ,
121+ _mail_instance = mail_instance ,
104122 id = msg_info ['id' ],
105123 from_addr = msg_info ['from' ],
106124 subject = msg_info ['subject' ],
@@ -109,28 +127,40 @@ def from_dict(cls, mail: 'OneSecMail', msg_info: dict[str, any]) -> 'OneSecMail.
109127
110128 @dataclass
111129 class Message :
130+ """Email message"""
131+
112132 id : int
133+ "Message ID"
113134 from_addr : str
135+ "Sender email address"
114136 subject : str
137+ "Subject of the message"
115138 date_str : str
139+ "Date the message was received in format YYYY-MM-DD HH:MM:SS"
116140 body : str
141+ "Message body (html if exists, text otherwise)"
117142 text_body : str
143+ "Message body (text format)"
118144 html_body : str
119- _mail : 'OneSecMail'
145+ "Message body (html format)"
146+ _mail_instance : 'OneSecMail'
120147 _attachments : list [dict [str , any ]]
121148
122149 @property
123150 def date (self ) -> datetime :
151+ """Date the message was received"""
124152 return datetime .fromisoformat (self .date_str )
125153
126154 @property
127155 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 ]
129158
130159 @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"""
132162 return cls (
133- _mail = mail ,
163+ _mail_instance = mail_instance ,
134164 _attachments = msg ['attachments' ],
135165 id = msg ['id' ],
136166 from_addr = msg ['from' ],
@@ -143,20 +173,26 @@ def from_dict(cls, mail: 'OneSecMail', msg: dict[str, any]) -> 'OneSecMail.Messa
143173
144174 @dataclass
145175 class Attachment :
176+ """Email attachment"""
177+
146178 filename : str
179+ "Name of the file of the attachment"
147180 content_type : str
181+ "MIME type of the attachment"
148182 size : int
149- _mail : 'OneSecMail'
183+ "Size of the attachment in bytes"
184+ _mail_instance : 'OneSecMail'
150185 _message_id : int
151186
152187 def download (self ) -> bytes :
153188 """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 )
155190
156191 @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"""
158194 return cls (
159- _mail = mail ,
195+ _mail_instance = mail_instance ,
160196 _message_id = message_id ,
161197 filename = attachment ['filename' ],
162198 content_type = attachment ['contentType' ],
0 commit comments