No project description provided
Project description
This is the official Python package for sending notifications with the Courier REST API.
Installation
Install from PyPI
pip install trycourier
# or
poetry add trycourier
Python 3.7 or later is required.
Usage
Use the Courier
class to access all of our endpoints.
import courier
from courier.client import Courier
client = Courier(
authorization_token="YOUR_TOKENY" # Defaults to COURIER_AUTH_TOKEN
)
response = client.send(
message=courier.ContentMessage(
to=courier.UserRecipient(
email="marty_mcfly@email.com",
data={
"name": "Marty",
}
),
content=courier.ElementalContentSugar(
title="Back to the Future",
body="Oh my {name}, we need 1.21 Gigawatts!",
),
routing=courier.Routing(method="all", channels=["inbox", "email"]),
)
)
print(response)
Async Client
The SDK also exports an asynchronous client, AsyncCourier
.
import os
import courier
import asyncio
from courier.client import AsyncCourier
client = AsyncCourier(
authorization_token="YOUR_TOKEN" # Defaults to COURIER_AUTH_TOKEN
)
async def main() -> None:
response = await client.send(
message=courier.ContentMessage(
to=courier.UserRecipient(
email="marty_mcfly@email.com",
data={
"name": "Marty",
}
),
content=courier.ElementalContentSugar(
title="Back to the Future",
body="Oh my {name}, we need 1.21 Gigawatts!",
),
routing=courier.Routing(method="all", channels=["email", "inbox"]),
)
)
asyncio.run(main())
Timeouts
By default, the client is configured to have a timeout of 60 seconds. You can customize this value at client instantiation.
from courier.client import Courier
client = Courier(
authorization_token="YOUR_TOKEN",
timeout=30
)
Handling Exceptions
All exceptions thrown by the SDK will subclass courier.ApiError.
import courier
from courier.core import ApiError
try:
courier.send(...)
except APIError as e:
# handle any api related error
Idempotency Keys
For POST
methods, you can supply an idempotencyKey
in the config parameter to
ensure the idempotency of the API Call. We recommend that you use a V4 UUID
for the key.
Keys are eligible to be removed from the system after they're at least 24 hours old,
and a new request is generated if a key is reused after the original has been removed.
For more info, see Idempotent Requests
in the Courier documentation.
import courier
courier.send(
message={...},
idempotency_key"YOUR_KEY",
idempotency_expiration="YOUR_EXPIRATION")
Additional Usage Examples
Send Template Message
import courier
from courier.client import Courier
client = Courier(
authorization_token="YOUR_TOKEN")
response = client.send(
message=courier.TemplateMessage(
template="my-template",
to=courier.UserRecipient(user_id="abc-123")
)
)
print(response.request_id)
UTM Metadata with Message
import courier
from courier.client import Courier
client = Courier(
authorization_token="YOUR_TOKEN")
response = client.send(
message=courier.ContentMessage(
content=courier.ElementalContent(
version='2020-01-01',
elements=[
courier.ElementalNode_Action(
content="ELEMENTAL",
href="courier.com",
style="button",
align="center"
)
]
),
to=courier.UserRecipient(email="foo@bar.com"),
routing=courier.Routing(method="all", channels=["email"]),
metadata=courier.MessageMetadata(
utm=courier.Utm(source="python")
)
)
)
print(response.request_id)
Advanced
Overriding HTTP Client
You can override the httpx client to customize it for your use case. Some common usecases include support for proxies and transports.
import httpx
from courier.client import Courier
client = Courier(
http_client=httpx.Client(
proxies="http://my.test.proxy.example.com",
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
),
)
Contributing
While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
License
Author
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file trycourier-6.2.0.tar.gz
.
File metadata
- Download URL: trycourier-6.2.0.tar.gz
- Upload date:
- Size: 89.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.8.18 Linux/5.15.0-1071-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2baa650ebe0d5d986e9fa9388208e431d60768e69a5790a51784e00fb8df1150 |
|
MD5 | c4e3e258773756180d3dbbda2a679447 |
|
BLAKE2b-256 | 4fa46ce83eba537d970a79b25acb5ca79aa718e9ddf36af9cc1e7bec33c9795e |
File details
Details for the file trycourier-6.2.0-py3-none-any.whl
.
File metadata
- Download URL: trycourier-6.2.0-py3-none-any.whl
- Upload date:
- Size: 277.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.8.18 Linux/5.15.0-1071-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a04f4548f8e6112f40ca2a1d3d22bc6e10e3a8f1afa3212c4b20e615c01c83d |
|
MD5 | 5b1e3f7b7e6fe67ad0164562239a9f3a |
|
BLAKE2b-256 | 4f46069267c0bd8d200442b663fea1e68d59c6b8b196a601b0388d4d5fbd0786 |