Skip to content

Commit 1819e9f

Browse files
committed
Final review updates
1 parent 8dc3593 commit 1819e9f

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ import postmark
3131
from dotenv import load_dotenv
3232

3333
load_dotenv()
34-
client = postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"])
3534

3635
async def main():
37-
response = await client.outbound.send({
38-
"sender": "sender@example.com",
39-
"to": "recipient@example.com",
40-
"subject": "Hello from Postmark",
41-
"text_body": "Sent with the Postmark Python SDK.",
42-
})
43-
print(f"Sent: {response.message_id}")
36+
async with postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"]) as client:
37+
response = await client.outbound.send({
38+
"sender": "sender@example.com",
39+
"to": "recipient@example.com",
40+
"subject": "Hello from Postmark",
41+
"text_body": "Sent with the Postmark Python SDK.",
42+
})
43+
print(f"Sent: {response.message_id}")
4444

4545
asyncio.run(main())
4646
```
@@ -55,8 +55,16 @@ asyncio.run(main())
5555
```python
5656
import postmark
5757

58-
client = postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"])
59-
account = postmark.AccountClient(os.environ["POSTMARK_ACCOUNT_TOKEN"])
58+
# Use as async context managers to ensure connections are closed
59+
async with postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"]) as client:
60+
...
61+
62+
async with postmark.AccountClient(os.environ["POSTMARK_ACCOUNT_TOKEN"]) as account:
63+
...
64+
65+
# Or call close() explicitly when done
66+
client = postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"])
67+
await client.close()
6068
```
6169

6270
## Development

postmark/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def __init__(
8080
super().__init__(message, error_code, http_status, request_id)
8181
match = re.search(r"Found inactive addresses: ([^.]+)", message)
8282
self.inactive_recipients: list[str] = (
83-
[addr.strip() for addr in match.group(1).split(",")] if match else []
83+
[a for addr in match.group(1).split(",") if (a := addr.strip())]
84+
if match
85+
else []
8486
)
8587

8688

postmark/models/outbound/manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ async def send_batch(
9797
Send up to 500 different emails in a single request.
9898
Use this when each recipient needs a **completely different** message.
9999
For sending the **same message** to many recipients, use send_bulk().
100+
101+
The API returns HTTP 200 even when individual messages fail. Check
102+
each ``SendResponse.success`` (or ``error_code``) in the returned list
103+
to detect per-message delivery errors.
100104
"""
101105
if len(messages) > 500:
102106
raise ValueError("Batch size cannot exceed 500 messages")

tests/test_account_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ async def test_close_calls_aclose(self, client):
137137
await client.close()
138138
mock_aclose.assert_called_once()
139139

140+
@pytest.mark.asyncio
141+
async def test_close_twice_does_not_raise(self, client):
142+
await client.close()
143+
await client.close()
144+
140145
@pytest.mark.asyncio
141146
async def test_async_context_manager(self):
142147
async with AccountClient(account_token="test-account-token") as client:

tests/test_server_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ async def test_close_calls_aclose(self, client):
120120
await client.close()
121121
mock_aclose.assert_called_once()
122122

123+
@pytest.mark.asyncio
124+
async def test_close_twice_does_not_raise(self, client):
125+
await client.close()
126+
await client.close()
127+
123128
@pytest.mark.asyncio
124129
async def test_async_context_manager(self):
125130
async with ServerClient(server_token="test-token") as client:

0 commit comments

Comments
 (0)