Expo Server SDK for Python
Project description
# exponent-server-sdk-python
This project (the repository and Python egg) are not actively maintained. If you use Expo and this egg in production and would like to become a maintainer of this project, please open an issue at https://github.com/expo/expo.
## Installation
```
pip install exponent_server_sdk
```
## Usage
Use to send push notifications to Expo apps from a Python server.
[Full documentation](https://docs.expo.io/versions/latest/guides/push-notifications.html#http-2-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 exponent_server_sdk import DeviceNotRegisteredError
from exponent_server_sdk import PushClient
from exponent_server_sdk import PushMessage
from exponent_server_sdk import PushResponseError
from exponent_server_sdk import PushServerError
from requests.exceptions import ConnectionError
from requests.exceptions import HTTPError
# Basic arguments. You should extend this function with the push features you
# want to use, or simply pass in a `PushMessage` object.
def send_push_message(token, message, extra=None):
try:
response = PushClient().publish(
PushMessage(to=token,
body=message,
data=extra))
except PushServerError as exc:
# Encountered some likely formatting/validation error.
rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'errors': exc.errors,
'response_data': exc.response_data,
})
raise
except (ConnectionError, HTTPError) as exc:
# Encountered some Connection or HTTP error - retry a few times in
# case it is transient.
rollbar.report_exc_info(
extra_data={'token': token, 'message': message, 'extra': extra})
raise self.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={
'token': token,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raise self.retry(exc=exc)
```
This project (the repository and Python egg) are not actively maintained. If you use Expo and this egg in production and would like to become a maintainer of this project, please open an issue at https://github.com/expo/expo.
## Installation
```
pip install exponent_server_sdk
```
## Usage
Use to send push notifications to Expo apps from a Python server.
[Full documentation](https://docs.expo.io/versions/latest/guides/push-notifications.html#http-2-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 exponent_server_sdk import DeviceNotRegisteredError
from exponent_server_sdk import PushClient
from exponent_server_sdk import PushMessage
from exponent_server_sdk import PushResponseError
from exponent_server_sdk import PushServerError
from requests.exceptions import ConnectionError
from requests.exceptions import HTTPError
# Basic arguments. You should extend this function with the push features you
# want to use, or simply pass in a `PushMessage` object.
def send_push_message(token, message, extra=None):
try:
response = PushClient().publish(
PushMessage(to=token,
body=message,
data=extra))
except PushServerError as exc:
# Encountered some likely formatting/validation error.
rollbar.report_exc_info(
extra_data={
'token': token,
'message': message,
'extra': extra,
'errors': exc.errors,
'response_data': exc.response_data,
})
raise
except (ConnectionError, HTTPError) as exc:
# Encountered some Connection or HTTP error - retry a few times in
# case it is transient.
rollbar.report_exc_info(
extra_data={'token': token, 'message': message, 'extra': extra})
raise self.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={
'token': token,
'message': message,
'extra': extra,
'push_response': exc.push_response._asdict(),
})
raise self.retry(exc=exc)
```
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
Close
Hashes for exponent_server_sdk-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4797f1f97f41f7675b11af6d002697e1c550dcf6460705df78fbe8fc024efe3e |
|
MD5 | 48e3799ec4b15b429eaa9cf5fca0955a |
|
BLAKE2b-256 | a61c2a612f9912e9488829d8ada7e254c7c0088beadcc4703c0fea461e28db76 |