def trigger(self, channels, event_name, data, socket_id=None): """Trigger an event on one or more channels, see: http://eventflit.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) > 100: 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)
def users_info(self, channel): """Fetch user ids currently subscribed to a presence channel http://eventflit.com/docs/rest_api#method-get-users """ validate_channel(channel) return Request(self, GET, "/apps/%s/channels/%s/users" % (self.app_id, channel))
def channel_info(self, channel, attributes=[]): """Get information on a specific channel, see: http://eventflit.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)
def trigger_batch(self, batch=[], already_encoded=False): """Trigger multiple events with a single HTTP call. http://eventflit.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)
def test_get_signature_generation(self): conf = Eventflit.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._eventflit_client, u'GET', u'/some/obscure/api', {u'foo': u'bar'}) self.assertEqual(req.query_params, expected)
def channels_info(self, prefix_filter=None, attributes=[]): """Get information on multiple channels, see: http://eventflit.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_post_signature_generation(self): conf = Eventflit.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._eventflit_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"})
def notify(self, interests, notification): """Send push notifications, see: https://github.com/eventflit/eventflit-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) is 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/publishes" % (API_PREFIX, self.app_id)) return Request(self, POST, path, params)
def test_x_eventflit_library_header(self): conf = Eventflit.from_url(u'http://*****:*****@somehost/apps/4') req = Request(conf._eventflit_client, u'GET', u'/some/obscure/api', {u'foo': u'bar'}) self.assertTrue('X-Eventflit-Library' in req.headers) eventflitLib = req.headers['X-Eventflit-Library'] self.assertRegexpMatches(eventflitLib, r'^eventflit-http-python \d+(\.\d+)+(rc\d+)?$')