Python SDK
The Python SDK provides sync and async clients for the PING8 Send API.
Install
pip install workonwardRepository/local source:
pip install -e SDK/pythonPython 3.9+ is supported.
Sync Send
import os
from workonward import Workonward
with Workonward(api_key=os.environ["WORKONWARD_API_KEY"]) as client:
result = client.send(
recipient="user@example.com",
attribs={"first_name": "Ada"},
)
print(result.message_id, result.engine)Async Send
import asyncio
import os
from workonward import AsyncWorkonward
async def main() -> None:
async with AsyncWorkonward(api_key=os.environ["WORKONWARD_API_KEY"]) as client:
result = await client.send("user@example.com")
print(result.message_id)
asyncio.run(main())Batch Send
batch = client.send_batch(
recipients=["ada@example.com", "grace@example.com"],
attribs={"campaign": "launch"},
)
print(batch.accepted, batch.message_ids)If any recipient is malformed, the SDK rejects the entire batch before making an HTTP request.
Options
from workonward import RetryConfig, Workonward
client = Workonward(
api_key=os.environ["WORKONWARD_API_KEY"],
base_url="https://mail.gitdate.ink",
timeout=10.0,
retry_config=RetryConfig(max_retries=2),
headers={"X-Request-Source": "billing-service"},
)base_url may be the site root or the API root. Authentication headers cannot be overridden.
Error Handling
from workonward import AuthenticationError, InvalidRecipientError, WorkonwardError
try:
client.send("user@example.com")
except InvalidRecipientError as error:
print("Bad recipient:", error.message)
except AuthenticationError as error:
print("API key rejected:", error.code)
except WorkonwardError as error:
print(error.code, error.status_code, error.message)All SDK errors inherit from WorkonwardError.