Esempio n. 1
0
    def trigger_batch(self, batch=[], already_encoded=False):
        """Trigger multiple events with a single HTTP call.

        http://pusher.com/docs/rest_api#method-post-batch-events
        """
        if not already_encoded:
            for event in batch:
                validate_channel(event['channel'])

                event_name = ensure_text(event['name'], "event_name")
                if len(event['name']) > 200:
                    raise ValueError("event_name too long")

                event['data'] = data_to_string(event['data'],
                                               self._json_encoder)

                if len(event['data']) > 10240:
                    raise ValueError("Too much data")

                if is_encrypted_channel(event['channel']):
                    event['data'] = json.dumps(encrypt(
                        event['channel'], event['data'],
                        self._encryption_master_key),
                                               ensure_ascii=False)

        params = {'batch': batch}

        return Request(self, POST, "/apps/%s/batch_events" % self.app_id,
                       params)
Esempio n. 2
0
    def trigger(self, channels, event_name, data, socket_id=None):
        """Trigger an event on one or more channels, see:

        http://pusher.com/docs/rest_api#method-post-event
        """
        if isinstance(channels, six.string_types):
            channels = [channels]

        if isinstance(channels, dict) or not isinstance(
                channels, (collections.Sized, collections.Iterable)):
            raise TypeError("Expected a single or a list of channels")

        if len(channels) > 10:
            raise ValueError("Too many channels")

        channels = list(map(validate_channel, channels))

        event_name = ensure_text(event_name, "event_name")

        if len(event_name) > 200:
            raise ValueError("event_name too long")

        data = data_to_string(data, self._json_encoder)

        if len(data) > 10240:
            raise ValueError("Too much data")

        params = {'name': event_name, 'channels': channels, 'data': data}

        if socket_id:
            params['socket_id'] = validate_socket_id(socket_id)

        return Request(self, POST, "/apps/%s/events" % self.app_id, params)
Esempio n. 3
0
 def test_x_pusher_library_header(self):
     conf = Pusher.from_url(u'http://*****:*****@somehost/apps/4')
     req = Request(conf._pusher_client, u'GET', u'/some/obscure/api',
                   {u'foo': u'bar'})
     self.assertTrue('X-Pusher-Library' in req.headers)
     pusherLib = req.headers['X-Pusher-Library']
     self.assertRegexpMatches(pusherLib,
                              r'^pusher-http-python \d+(\.\d+)+(rc\d+)?$')
Esempio n. 4
0
    def users_info(self, channel):
        """Fetch user ids currently subscribed to a presence channel

        http://pusher.com/docs/rest_api#method-get-users
        """
        validate_channel(channel)

        return Request(self, GET,
                       "/apps/%s/channels/%s/users" % (self.app_id, channel))
Esempio n. 5
0
    def channel_info(self, channel, attributes=[]):
        """Get information on a specific channel, see:

        http://pusher.com/docs/rest_api#method-get-channel
        """
        validate_channel(channel)

        params = {}
        if attributes:
            params['info'] = join_attributes(attributes)

        return Request(self, GET,
                       "/apps/%s/channels/%s" % (self.app_id, channel), params)
Esempio n. 6
0
    def trigger_batch(self, batch=[], already_encoded=False):
        """Trigger multiple events with a single HTTP call.

        http://pusher.com/docs/rest_api#method-post-batch-events
        """
        if not already_encoded:
            for event in batch:
                event['data'] = data_to_string(event['data'],
                                               self._json_encoder)

        params = {'batch': batch}

        return Request(self, POST, "/apps/%s/batch_events" % self.app_id,
                       params)
Esempio n. 7
0
    def channels_info(self, prefix_filter=None, attributes=[]):
        """Get information on multiple channels, see:

        http://pusher.com/docs/rest_api#method-get-channels
        """
        params = {}
        if attributes:
            params['info'] = join_attributes(attributes)

        if prefix_filter:
            params['filter_by_prefix'] = ensure_text(
                prefix_filter, "prefix_filter")

        return Request(
            self, GET, six.text_type("/apps/%s/channels") % self.app_id, params)
    def test_get_signature_generation(self):
        conf = Pusher.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature': u'5c49f04a95eedc9028b1e0e8de7c2c7ad63504a0e3b5c145d2accaef6c14dbac',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'd41d8cd98f00b204e9800998ecf8427e',
            u'foo': u'bar'
        }

        with mock.patch('time.time', return_value=1000):
            req = Request(conf._pusher_client, u'GET', u'/some/obscure/api', {u'foo': u'bar'})
            self.assertEqual(req.query_params, expected)
    def test_post_signature_generation(self):
        conf = Pusher.from_url(u'http://*****:*****@somehost/apps/4')

        expected = {
            u'auth_key': u'key',
            u'auth_signature': u'e05fa4cafee86311746ee3981d5581a5e4e87c27bbab0aeb1059e2df5c90258b',
            u'auth_timestamp': u'1000',
            u'auth_version': u'1.0',
            u'body_md5': u'94232c5b8fc9272f6f73a1e36eb68fcf'
        }

        with mock.patch('time.time', return_value=1000):
            # patching this, because json can be unambiguously parsed, but not
            # unambiguously generated (think whitespace).
            with mock.patch('json.dumps', return_value='{"foo": "bar"}') as json_dumps_mock:
                req = Request(conf._pusher_client, u'POST', u'/some/obscure/api', {u'foo': u'bar'})
                self.assertEqual(req.query_params, expected)

            json_dumps_mock.assert_called_once_with({u"foo": u"bar"})
Esempio n. 10
0
    def trigger(self, channels, event_name, data, socket_id=None):
        """Trigger an event on one or more channels, see:

        http://pusher.com/docs/rest_api#method-post-event
        """
        if isinstance(channels, six.string_types):
            channels = [channels]

        if isinstance(channels, dict) or not isinstance(
                channels, (collections.Sized, collections.Iterable)):
            raise TypeError("Expected a single or a list of channels")

        if len(channels) > self.max_num_channels:
            raise ValueError("Too many channels")

        event_name = ensure_text(event_name, "event_name")
        if len(event_name) > self.max_len_event_name:
            raise ValueError("event_name too long")

        data = data_to_string(data, self._json_encoder)
        if len(data) > self.max_len_data:
            raise ValueError("Too much data")

        channels = list(map(validate_channel, channels))

        if len(channels) > 1:
            for chan in channels:
                if is_encrypted_channel(chan):
                    raise ValueError(
                        "You cannot trigger to multiple channels when using encrypted channels"
                    )

        if is_encrypted_channel(channels[0]):
            data = json.dumps(encrypt(channels[0], data,
                                      self._encryption_master_key),
                              ensure_ascii=False)

        params = {'name': event_name, 'channels': channels, 'data': data}

        if socket_id:
            params['socket_id'] = validate_socket_id(socket_id)

        return Request(self, POST, "/apps/%s/events" % self.app_id, params)
Esempio n. 11
0
    def notify(self, interests, notification):
        """Send push notifications, see:

        https://github.com/pusher/pusher-http-python#push-notifications-beta
        """
        if not isinstance(interests, list) and not isinstance(interests, set):
            raise TypeError("Interests must be a list or a set")

        if len(interests) == 0:
            raise ValueError("Interests must not be empty")

        if not isinstance(notification, dict):
            raise TypeError("Notification must be a dictionary")

        params = {'interests': interests}

        params.update(notification)
        path = ("/%s/%s/apps/%s/notifications" %
                (API_PREFIX, API_VERSION, self.app_id))

        return Request(self, POST, path, params)