Expo Server SDK for Python Asyncio
Project description
# aioexponent
If you have problems with the code in this repository, please file an issue or a pull-request. Thanks!
## Installation
```
pip install aioexponent
```
## Usage
Use to send push notifications to Exponent Experiences from a Python server.
[Full documentation](https://docs.expo.io/versions/latest/guides/push-notifications.html#http2-api) on the API is available if you want to dive into the details.
Here's an example on how to use this with retries and reporting via [pyrollbar](https://github.com/rollbar/pyrollbar).
```python
from aioexponent import DeviceNotRegisteredError
from aioexponent import PushClient
from aioexponent import PushMessage
from aioexponent import PushResponseError
from aioexponent import PushServerError
from aiohttp import ClientError
# Basic arguments. You should extend this function with the push features you
# want to use, or simply pass in a `PushMessage` object.
async def send_push_message(tokens, message, extra=None):
client = PushClient()
try:
response = await client.publish_multiple([
PushMessage(to=token,
body=message,
data=extra) for token in tokens])
except PushServerError as exc:
# Encountered some likely formatting/validation error.
rollbar.report_exc_info(
extra_data={
'tokens': tokens, 'message': message, 'extra': extra
'errors': exc.errors,
'response_data': exc.response_data,
})
raise
except (ClientError) as exc:
# Encountered some Connection or HTTP error - retry a few times in
# case it is transient.
rollbar.report_exc_info(
extra_data={'tokens': tokens, 'message': message, 'extra': extra})
raise retry(exc=exc)
try:
# We got a response back, but we don't know whether it's an error yet.
# This call raises errors so we can handle them with normal exception
# flows.
response.validate_response()
except DeviceNotRegisteredError:
# Mark the push token as inactive
from notifications.models import PushToken
PushToken.objects.filter(token=token).update(active=False)
except PushResponseError as exc:
# Encountered some other per-notification error.
rollbar.report_exc_info(
extra_data={
'tokens': tokens,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raise retry(exc=exc)
```
If you have problems with the code in this repository, please file an issue or a pull-request. Thanks!
## Installation
```
pip install aioexponent
```
## Usage
Use to send push notifications to Exponent Experiences from a Python server.
[Full documentation](https://docs.expo.io/versions/latest/guides/push-notifications.html#http2-api) on the API is available if you want to dive into the details.
Here's an example on how to use this with retries and reporting via [pyrollbar](https://github.com/rollbar/pyrollbar).
```python
from aioexponent import DeviceNotRegisteredError
from aioexponent import PushClient
from aioexponent import PushMessage
from aioexponent import PushResponseError
from aioexponent import PushServerError
from aiohttp import ClientError
# Basic arguments. You should extend this function with the push features you
# want to use, or simply pass in a `PushMessage` object.
async def send_push_message(tokens, message, extra=None):
client = PushClient()
try:
response = await client.publish_multiple([
PushMessage(to=token,
body=message,
data=extra) for token in tokens])
except PushServerError as exc:
# Encountered some likely formatting/validation error.
rollbar.report_exc_info(
extra_data={
'tokens': tokens, 'message': message, 'extra': extra
'errors': exc.errors,
'response_data': exc.response_data,
})
raise
except (ClientError) as exc:
# Encountered some Connection or HTTP error - retry a few times in
# case it is transient.
rollbar.report_exc_info(
extra_data={'tokens': tokens, 'message': message, 'extra': extra})
raise retry(exc=exc)
try:
# We got a response back, but we don't know whether it's an error yet.
# This call raises errors so we can handle them with normal exception
# flows.
response.validate_response()
except DeviceNotRegisteredError:
# Mark the push token as inactive
from notifications.models import PushToken
PushToken.objects.filter(token=token).update(active=False)
except PushResponseError as exc:
# Encountered some other per-notification error.
rollbar.report_exc_info(
extra_data={
'tokens': tokens,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raise retry(exc=exc)
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aioexponent-0.5.1.tar.gz
(6.0 kB
view details)
Built Distribution
File details
Details for the file aioexponent-0.5.1.tar.gz
.
File metadata
- Download URL: aioexponent-0.5.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.7.1 requests-toolbelt/0.8.0 tqdm/4.19.7 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e341065fac25860cbdc578aa951451d50903141806ba65091c2c59b8f77b730 |
|
MD5 | 761d5caa2bb8950fba76fc43e7f238fb |
|
BLAKE2b-256 | 3a0ed305e335da4896422e66a7fd887f744ad2a31854aa075735aaf5b3320259 |
File details
Details for the file aioexponent-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: aioexponent-0.5.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.7.1 requests-toolbelt/0.8.0 tqdm/4.19.7 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d0cbad53dd20d1418f5747cb6eafb71af58b3e94a55fd0205406d32c6439508 |
|
MD5 | 01c827d515bcd5ae7ff2a377213644ae |
|
BLAKE2b-256 | 903cd1f4c45da492ad06334f16963026ab20e57e77af36928c8e9358f9d65569 |