Skip to content

Commit 8af37c2

Browse files
committed
Update code
1 parent a854c17 commit 8af37c2

File tree

3 files changed

+101
-42
lines changed

3 files changed

+101
-42
lines changed

README.md

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ for message in messages:
3333
- **Create temporary email addresses** - Generate disposable emails instantly
3434
- **List available domains** - Get domains you can use for email creation
3535
- **Receive emails** - Fetch messages sent to your temporary addresses
36-
- **Delete messages** - Clean up messages when done
36+
- **Get individual messages** - Retrieve specific messages by ID
37+
- **Get message source code** - Access raw email source
38+
- **Download attachments** - Download email attachments
39+
- **Delete messages** - Clean up individual messages
40+
- **Delete emails** - Remove email addresses and all their messages
3741
- **Rate limit monitoring** - Track your API usage
3842
- **Error handling** - Comprehensive exception handling
3943
- **Type hints** - Full typing support for better development experience
@@ -61,48 +65,68 @@ client = TempMailClient(
6165
### Creating Email Addresses
6266

6367
```python
64-
from tempmail import CreateEmailOptions
65-
6668
# Create random email
6769
email = client.create_email()
6870

69-
# Create with specific domain and prefix
70-
options = CreateEmailOptions(domain="example.com", prefix="mytest")
71-
email = client.create_email(options)
71+
# Create with specific domain
72+
email = client.create_email(domain="example.com")
73+
74+
# Create with specific email address
75+
email = client.create_email(email="[email protected]")
76+
77+
# Create with domain type preference
78+
email = client.create_email(domain_type="premium")
7279
```
7380

7481
### Listing Domains
7582

7683
```python
7784
domains = client.list_domains()
7885
for domain in domains:
79-
print(domain.domain)
86+
print(f"Domain: {domain.name}, Type: {domain.type.value}")
8087
```
8188

8289
### Managing Messages
8390

8491
```python
85-
from tempmail import ListMessagesOptions
86-
8792
# List all messages for an email
8893
messages = client.list_email_messages("[email protected]")
94+
for message in messages:
95+
print(f"From: {message.from_addr}")
96+
print(f"Subject: {message.subject}")
97+
print(f"CC: {message.cc}")
98+
print(f"Attachments: {len(message.attachments or [])}")
99+
100+
# Get a specific message
101+
message = client.get_message("message-id")
102+
103+
# Get message source code
104+
source_code = client.get_message_source_code("message-id")
89105

90-
# List with pagination
91-
options = ListMessagesOptions(limit=10, offset=0)
92-
messages = client.list_email_messages("[email protected]", options)
106+
# Download an attachment
107+
attachment_data = client.download_attachment("attachment-id")
93108

94109
# Delete a message
95110
client.delete_message("message-id")
111+
112+
# Delete an entire email address and all its messages
113+
client.delete_email("[email protected]")
96114
```
97115

98116
### Rate Limiting
99117

100118
```python
101119
# Get current rate limit status
102120
rate_limit = client.get_rate_limit()
103-
if rate_limit:
104-
print(f"Remaining requests: {rate_limit.remaining}")
105-
print(f"Reset time: {rate_limit.reset}")
121+
print(f"Limit: {rate_limit.limit}")
122+
print(f"Remaining: {rate_limit.remaining}")
123+
print(f"Used: {rate_limit.used}")
124+
print(f"Reset time: {rate_limit.reset}")
125+
126+
# Access last rate limit from any request
127+
last_rate_limit = client.last_rate_limit
128+
if last_rate_limit:
129+
print(f"Last known remaining: {last_rate_limit.remaining}")
106130
```
107131

108132
## Error Handling
@@ -115,7 +139,6 @@ from tempmail import (
115139
AuthenticationError, # Invalid API key
116140
RateLimitError, # Rate limit exceeded
117141
ValidationError, # Invalid parameters
118-
APIError # Server errors
119142
)
120143

121144
try:
@@ -127,8 +150,8 @@ except RateLimitError:
127150
print("Rate limit exceeded")
128151
except ValidationError as e:
129152
print(f"Invalid parameters: {e}")
130-
except APIError as e:
131-
print(f"API error: {e.status_code} - {e}")
153+
except TempMailError as e:
154+
print(f"API error: {e}")
132155
```
133156

134157
## Development
@@ -140,36 +163,68 @@ except APIError as e:
140163
git clone https://github.com/temp-mail-io/temp-mail-python
141164
cd temp-mail-python
142165

143-
# Install in development mode
144-
pip install -e ".[dev]"
166+
# Install uv (recommended)
167+
curl -LsSf https://astral.sh/uv/install.sh | sh
168+
169+
# Install dependencies
170+
uv sync --dev
145171
```
146172

147173
### Running Tests
148174

149175
```bash
150-
pytest tests/
176+
uv run pytest tests/
151177
```
152178

153-
### Code Formatting
179+
### Code Quality
154180

155181
```bash
156-
black tempmail/ tests/
157-
isort tempmail/ tests/
182+
# Run all pre-commit hooks
183+
uv run pre-commit run --all-files
184+
185+
# Or run individual tools
186+
uv run ruff check # Linting
187+
uv run ruff format # Formatting
188+
uv run mypy tempmail/ # Type checking
158189
```
159190

160-
### Type Checking
191+
## Complete Example
161192

162-
```bash
163-
mypy tempmail/
164-
```
193+
```python
194+
from tempmail import TempMailClient, AuthenticationError, RateLimitError
165195

166-
## Examples
196+
# Initialize client
197+
client = TempMailClient("your-api-key")
167198

168-
See the [examples/](examples/) directory for more detailed usage examples.
199+
try:
200+
# Create a temporary email
201+
email = client.create_email()
202+
print(f"Created email: {email.email}")
203+
print(f"TTL: {email.ttl} seconds")
204+
205+
# List available domains
206+
domains = client.list_domains()
207+
print(f"Available domains: {len(domains)}")
208+
209+
# Check for messages (would be empty initially)
210+
messages = client.list_email_messages(email.email)
211+
print(f"Messages: {len(messages)}")
212+
213+
# Check rate limit
214+
rate_limit = client.get_rate_limit()
215+
print(f"Requests remaining: {rate_limit.remaining}/{rate_limit.limit}")
216+
217+
except AuthenticationError:
218+
print("Invalid API key")
219+
except RateLimitError:
220+
print("Rate limit exceeded")
221+
except Exception as e:
222+
print(f"Error: {e}")
223+
```
169224

170225
## License
171226

172-
MIT License - see [LICENSE](LICENSE) file for details.
227+
MIT Licensesee [LICENSE](LICENSE) file for details.
173228

174229
## Links
175230

tempmail/client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def _make_request(
5252
params: Optional[Dict[str, Any]] = None,
5353
json_data: Optional[Dict[str, Any]] = None,
5454
return_content: bool = False,
55+
update_rate_limit: bool = True,
5556
) -> typing.Union[Dict[str, Any], bytes]:
5657
"""
5758
Make an HTTP request to the API.
@@ -73,7 +74,8 @@ def _make_request(
7374
)
7475

7576
if 200 <= response.status_code < 300:
76-
self._update_rate_limit_from_headers(response.headers)
77+
if update_rate_limit:
78+
self._update_rate_limit_from_headers(response.headers)
7779
if return_content:
7880
return response.content
7981
return response.json()
@@ -183,8 +185,11 @@ def get_rate_limit(self) -> RateLimit:
183185
Get current rate limit information.
184186
:return: RateLimit object
185187
"""
186-
data = self._make_request("GET", "/v1/rate_limit")
187-
return RateLimit.from_json(data)
188+
data = self._make_request("GET", "/v1/rate_limit", update_rate_limit=False)
189+
rate_limit: RateLimit = RateLimit.from_json(data)
190+
# Also update the last known rate limit since this method doesn't use headers
191+
self._last_rate_limit = rate_limit
192+
return rate_limit
188193

189194
@property
190195
def last_rate_limit(self) -> Optional[RateLimit]:

tests/test_client.py

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

1919

2020
class TestTempMailClient:
@@ -403,16 +403,15 @@ def test_get_rate_limit_success(self, mock_request):
403403
client = TempMailClient("test-api-key")
404404
rate_limit_data = client.get_rate_limit()
405405

406-
assert rate_limit_data.limit == 100
407-
assert rate_limit_data.remaining == 95
408-
assert rate_limit_data.used == 5
409-
assert rate_limit_data.reset == 1640995200
406+
assert rate_limit_data == RateLimit(
407+
limit=100, remaining=95, used=5, reset=1640995200
408+
)
410409

411410
# Verify the last rate limit was updated from headers
412411
assert client.last_rate_limit is not None
413-
assert client.last_rate_limit.limit == 100
414-
assert client.last_rate_limit.remaining == 95
415-
assert client.last_rate_limit.reset == 1640995200
412+
assert client.last_rate_limit == RateLimit(
413+
limit=100, remaining=95, used=5, reset=1640995200
414+
)
416415

417416
@patch("tempmail.client.requests.Session.request")
418417
def test_request_exception(self, mock_request):

0 commit comments

Comments
 (0)