Skip to main content

Expo Server SDK for Python

Project description

exponent-server-sdk-python

This repo is maintained by Expo's awesome community :heart_eyes:! So, if you have problems with the code in this repository, please feel free to open an issue, and make a PR. Thanks!

Installation

pip install exponent_server_sdk

Usage

Use to send push notifications to Exponent Experiences from a Python server.

Full documentation 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.

from exponent_server_sdk import (
    DeviceNotRegisteredError,
    PushClient,
    PushMessage,
    PushServerError,
    PushTicketError,
)
import os
import requests
from requests.exceptions import ConnectionError, HTTPError

# Optionally providing an access token within a session if you have enabled push security
session = requests.Session()
session.headers.update(
    {
        "Authorization": f"Bearer {os.getenv('EXPO_TOKEN')}",
        "accept": "application/json",
        "accept-encoding": "gzip, deflate",
        "content-type": "application/json",
    }
)

# 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(session=session).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 PushTicketError 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exponent_server_sdk-2.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

exponent_server_sdk-2.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file exponent_server_sdk-2.1.0.tar.gz.

File metadata

  • Download URL: exponent_server_sdk-2.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for exponent_server_sdk-2.1.0.tar.gz
Algorithm Hash digest
SHA256 089074b944e656ca009c4e2e66a4399b81db3fd104bc4c8ed973a930331973f9
MD5 084fd314bee38795c55b5d6cbe689e9b
BLAKE2b-256 6df3d4b219d50f79068a403e90ed53015c713995e377b7b63c68dc6fc71ea771

See more details on using hashes here.

File details

Details for the file exponent_server_sdk-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for exponent_server_sdk-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c69058aaf14d2b788074afd03252c9e7d15db6bf434741282d4c189b3d54df0
MD5 4e767aff8464ee01b9e4edfe52603339
BLAKE2b-256 cf4fd480d4efbda907d6ea0ced27029ecb4726b1736bd189be59e383939e7f82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page