Skip to content

Commit

Permalink
Supporting pusher>=1.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed May 9, 2015
1 parent 80f5457 commit 0314443
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -11,6 +11,7 @@ env:
- FLASK="==0.9" PUSHER="pusher<1.0"
- FLASK="==0.9"
- FLASK="==0.10.1" PUSHER="pusher<1.0"
- FLASK="==0.10.1" PUSHER="pusher>=1.0,<1.1"
- FLASK="==0.10.1"
- FLASK="==0.10.1" PUSHER="git+https://github.com/pusher/pusher-http-python.git"

Expand Down
16 changes: 14 additions & 2 deletions flask_pusher.py
@@ -1,5 +1,6 @@
import hashlib
import hmac
import inspect

from flask import Blueprint, current_app, request, abort, json
try:
Expand All @@ -13,10 +14,16 @@
from pusher.signature import sign, verify
__v1__ = True

argspec = inspect.getargspec(_pusher.Pusher.__init__)
if "json_encoder" in argspec.args:
_json_encoder_support = True
# monkey patch pusher json module because they don't have
# any option to define my own json encoder
_pusher.pusher.json = json
else:
_json_encoder_support = False
_pusher.pusher.json = json
except ImportError:
_json_encoder_support = True
__v1__ = False

def sign(key, message):
Expand Down Expand Up @@ -54,7 +61,12 @@ def init_app(self, app):
host=app.config["PUSHER_HOST"],
port=app.config["PUSHER_PORT"],
)
if not __v1__:

if __v1__:
if _json_encoder_support:
pusher_kwargs["json_encoder"] = getattr(app, "json_encoder", None)
pusher_kwargs["json_decoder"] = getattr(app, "json_decoder", None)
else:
pusher_kwargs["encoder"] = getattr(app, "json_encoder", None)

client = _pusher.Pusher(**pusher_kwargs)
Expand Down
22 changes: 17 additions & 5 deletions tests.py
Expand Up @@ -4,8 +4,9 @@
import unittest
from decimal import Decimal

import pusher as _pusher
from flask import Flask, json, render_template_string, url_for
from flask.ext.pusher import Pusher
from flask.ext.pusher import Pusher, _json_encoder_support, __v1__


pusher_conf = {
Expand Down Expand Up @@ -50,14 +51,25 @@ def test_create_extensions_map(self):
self.assertIsNotNone(pusher.client)

def test_json_encoder(self):
if not _json_encoder_support:
msg = u"JSON encoder override is not supported on pusher>=1.0,<1.1"
self.skipTest(msg)

self.app.json_encoder = CustomJSONEncoder
pusher = Pusher(self.app)

with self.app.test_request_context():
if not hasattr(pusher.client, "encoder"):
msg = u"JSON encoder override is not supported on pusher>=1.0"
self.skipTest(msg)
self.assertEqual(CustomJSONEncoder, pusher.client.encoder)
if __v1__:
enc = pusher.client._json_encoder
else:
enc = pusher.client.encoder
self.assertEqual(CustomJSONEncoder, enc)

def test_flask_json_patch(self):
if _json_encoder_support:
msg = u"Only pusher>=1.0,<1.1 is monkey patched"
self.skipTest(msg)
self.assertEqual(json, _pusher.pusher.json)

def test_configuration(self):
self.app.config["PUSHER_HOST"] = "example.com"
Expand Down

0 comments on commit 0314443

Please sign in to comment.