Announcing v1 of the Python Library

python.png

We are excited to announce that we have published a major release of our Python HTTP library. We’re the first to admit that the previous version of the library didn’t get as much love as it should have. So, after attending PyCon 2015 and ahead of our sponsorship of DjangoCon, we decided to finish off \[…\]

Introduction

We are excited to announce that we have published a major release of our Python HTTP library. We’re the first to admit that the previous version of the library didn’t get as much love as it should have. So, after attending PyCon 2015 and ahead of our sponsorship of DjangoCon, we decided to finish off a version we’ve been working on for a while and are very pleased to ship v1. You can fork it on GitHub and pip install pusher via PyPI.

There are a number of major differences from the previous version of the library (0.8), namely that the new version offers far more core interactions with our HTTP API. As well as triggering events and authenticating private and presence channels subscriptions, you are now able to:

This version of the library is now compatible with Python versions 2.6, 2.7 and 3.3. We’ve also spent some time making sure the docs are much more complete so the Pusher Python HTTP library README has been updated as have the Pusher site docs.

What’s Changed?

Triggering Events

Whereas in the previous version one would trigger events using the indexing operator (pusher['channel_name'].trigger('event_name', payload), one now passes the channel_name as an argument to the Pusher::trigger method.

1from pusher import Pusher
2
3pusher = Pusher(app_id=u'app_id', key=u'key', secret=u'secret')
4
5pusher.trigger(u'a_channel', u'an_event', {u'some': u'data'})

Pusher::trigger is documented here.

Querying Application State

We’ve introduced a bunch of new methods for getting information from your Pusher channels. These are:

  • Pusher::channels_info: for getting information about all your channels.
  • Pusher::channel_info: for getting information about a specific channel.
  • Pusher::users_info: for getting information about users subscribed to a presence-channel.

These methods are documented here.

Authenticating Private- and Presence-Channels

When authenticating private- or presence- channels you now use the Pusher::authenticate method instead of using the indexing operator we mentioned earlier. This example uses Flask to demonstrate how to authenticate private-channels:

1@app.route("/pusher/auth", methods=['POST'])
2def pusher_authentication():
3
4  auth = pusher.authenticate(
5    channel=request.form['channel_name'],
6    socket_id=request.form['socket_id']
7  )
8  return json.dumps(auth)

For presence-channels, simply pass in a dictionary containing the user’s user_id, along with any optional user_info, to a custom_data field.

Validating Webhooks

Pusher::validate_webhook can check that an incoming webhook is indeed from Pusher, and not from an unknown origin, as shown in this example:

1@app.route("/webhook", methods=['POST'])
2def pusher_webhook():
3  webhook = pusher.validate_webhook(
4    key=request.headers.get('X-Pusher-Key'),
5    signature=request.headers.get('X-Pusher-Signature'),
6    body=request.data
7  )
8
9  for event in webhook['events']:
10    if event['name'] == "channel_occupied":
11      print("Channel occupied: %s" % event["channel"])
12    elif event['name'] == "channel_vacated":
13      print("Channel vacated: %s" % event["channel"])
14
15  return "ok"

Webhook validation is documented here.

Contributing

You can find out much more via the Github repo. If you find anything lacking in the library, or room for improvement, please do send us a pull request!