class PushClient:

    @staticmethod
    def channel_for_user(user_id, salt):
        return hashlib.md5(':'.join([unicode(user_id), salt])).hexdigest()

    def __init__(self, channel_salt, app_id=None, key=None, secret=None, **options):
        self.channel_salt = channel_salt
        self.pusher = Pusher(app_id, key, secret)

    def send_to_user(self, user_id, event_name, data):
        channel = PushClient.channel_for_user(user_id, self.channel_salt)
        log.debug("Pushing to channel: %s", channel)
        self.pusher.trigger([channel], event_name, data)
Example #2
0
 def test_initialize_from_env(self):
     with mock.patch.object(os, 'environ', new={'PUSHER_URL':'https://plah:bob@somehost/apps/42'}):
         pusher = Pusher.from_env()
         self.assertEqual(pusher._pusher_client.ssl, True)
         self.assertEqual(pusher._pusher_client.key, u'plah')
         self.assertEqual(pusher._pusher_client.secret, u'bob')
         self.assertEqual(pusher._pusher_client.host, u'somehost')
         self.assertEqual(pusher._pusher_client.app_id, u'42')
Example #3
0
    def test_initialize_from_url(self):
        self.assertRaises(TypeError, lambda: Pusher.from_url(4))
        self.assertRaises(Exception, lambda: Pusher.from_url(u'httpsahsutaeh'))

        pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
        self.assertEqual(pusher._pusher_client.ssl, False)
        self.assertEqual(pusher._pusher_client.key, u'foo')
        self.assertEqual(pusher._pusher_client.secret, u'bar')
        self.assertEqual(pusher._pusher_client.host, u'host')
        self.assertEqual(pusher._pusher_client.app_id, u'4')

        pusher = Pusher.from_url(u'https://foo:bar@host/apps/4')
        self.assertEqual(pusher._pusher_client.ssl, True)
        self.assertEqual(pusher._pusher_client.key, u'foo')
        self.assertEqual(pusher._pusher_client.secret, u'bar')
        self.assertEqual(pusher._pusher_client.host, u'host')
        self.assertEqual(pusher._pusher_client.app_id, u'4')
 def __init__(self, channel_salt, app_id=None, key=None, secret=None, **options):
     self.channel_salt = channel_salt
     self.pusher = Pusher(app_id, key, secret)