Skip to content

Commit 0e53f52

Browse files
committed
Add attachment model
1 parent 267e6ee commit 0e53f52

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
exclude: '.git'
2-
default_stages: [ commit ]
2+
default_stages: [ pre-commit ]
33

44
repos:
55
- repo: https://github.com/pre-commit/pre-commit-hooks

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,19 @@ try:
201201
email = client.create_email()
202202
print(f"Created email: {email.email}")
203203
print(f"TTL: {email.ttl} seconds")
204-
204+
205205
# List available domains
206206
domains = client.list_domains()
207207
print(f"Available domains: {len(domains)}")
208-
208+
209209
# Check for messages (would be empty initially)
210210
messages = client.list_email_messages(email.email)
211211
print(f"Messages: {len(messages)}")
212-
212+
213213
# Check rate limit
214214
rate_limit = client.get_rate_limit()
215215
print(f"Requests remaining: {rate_limit.remaining}/{rate_limit.limit}")
216-
216+
217217
except AuthenticationError:
218218
print("Invalid API key")
219219
except RateLimitError:

tempmail/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Domain,
1111
EmailAddress,
1212
EmailMessage,
13+
Attachment,
1314
)
1415
from .exceptions import (
1516
TempMailError,
@@ -23,6 +24,7 @@
2324
"RateLimit",
2425
"Domain",
2526
"EmailAddress",
27+
"Attachment",
2628
"EmailMessage",
2729
"TempMailError",
2830
"AuthenticationError",

tempmail/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def list_email_messages(
148148
data = self._make_request("GET", f"/v1/emails/{email}/messages")
149149

150150
messages = []
151-
for msg_data in data.get("messages", []):
151+
for msg_data in data["messages"]:
152152
messages.append(EmailMessage.from_json(msg_data))
153153

154154
return messages

tempmail/models.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import enum
44
from dataclasses import dataclass
55
from datetime import datetime
6-
from typing import Optional, List, Dict, Any
6+
from typing import List, Dict, Any
77

88

99
@dataclass
@@ -59,10 +59,18 @@ def from_json(cls, data: Dict[str, Any]) -> "EmailAddress":
5959
class Attachment:
6060
"""Attachment information for an email message."""
6161

62-
filename: str
63-
content_type: str
62+
id: str
63+
name: str
6464
size: int # Size in bytes
6565

66+
@classmethod
67+
def from_json(cls, data: Dict[str, Any]) -> "Attachment":
68+
return cls(
69+
id=data["id"],
70+
name=data["name"],
71+
size=data["size"],
72+
)
73+
6674

6775
@dataclass
6876
class EmailMessage:
@@ -74,9 +82,9 @@ class EmailMessage:
7482
subject: str
7583
body_text: str
7684
created_at: datetime
77-
cc: Optional[List[str]] = None
78-
body_html: Optional[str] = None
79-
attachments: Optional[List[Dict[str, Any]]] = None
85+
cc: List[str]
86+
body_html: str
87+
attachments: List[Attachment]
8088

8189
@classmethod
8290
def from_json(cls, data: Dict[str, Any]) -> "EmailMessage":
@@ -89,9 +97,9 @@ def from_json(cls, data: Dict[str, Any]) -> "EmailMessage":
8997
created_at=datetime.fromisoformat(
9098
data["created_at"].replace("Z", "+00:00")
9199
),
92-
cc=data.get("cc", []),
93-
body_html=data.get("body_html"),
94-
attachments=data.get("attachments", []),
100+
cc=data["cc"],
101+
body_html=data["body_html"],
102+
attachments=[Attachment.from_json(v) for v in data["attachments"]],
95103
)
96104

97105

tests/test_client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
TempMailError,
1515
)
1616
from requests.exceptions import ConnectionError
17-
from tempmail.models import DomainType, RateLimit
17+
from tempmail.models import DomainType, RateLimit, Attachment
1818

1919

2020
class TestTempMailClient:
@@ -116,7 +116,18 @@ def test_list_email_messages_success(self, mock_request):
116116
"body_text": "Test body",
117117
"body_html": "<p>Test body</p>",
118118
"created_at": "2023-01-01T00:00:00Z",
119-
"attachments": [],
119+
"attachments": [
120+
{
121+
"id": "att1",
122+
"name": "file.txt",
123+
"size": 1234,
124+
},
125+
{
126+
"id": "att2",
127+
"name": "image.png",
128+
"size": 4567,
129+
},
130+
],
120131
}
121132
]
122133
}
@@ -138,7 +149,10 @@ def test_list_email_messages_success(self, mock_request):
138149
created_at=datetime.datetime(
139150
2023, 1, 1, 0, 0, tzinfo=datetime.timezone.utc
140151
),
141-
attachments=[],
152+
attachments=[
153+
Attachment(id="att1", name="file.txt", size=1234),
154+
Attachment(id="att2", name="image.png", size=4567),
155+
],
142156
)
143157

144158
@patch("tempmail.client.requests.Session.request")

0 commit comments

Comments
 (0)