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)
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))
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)
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)
def authenticate(self, channel, socket_id, custom_data=None): """Used to generate delegated client subscription token. :param channel: name of the channel to authorize subscription to :param socket_id: id of the socket that requires authorization :param custom_data: used on presence channels to provide user info """ channel = validate_channel(channel) if not channel_name_re.match(channel): raise ValueError('Channel should be a valid channel, got: %s' % channel) socket_id = validate_socket_id(socket_id) if custom_data: custom_data = json.dumps(custom_data, cls=self._json_encoder) string_to_sign = "%s:%s" % (socket_id, channel) if custom_data: string_to_sign += ":%s" % custom_data signature = sign(self.secret, string_to_sign) auth = "%s:%s" % (self.key, signature) result = {'auth': auth} if custom_data: result['channel_data'] = custom_data return result
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) or not isinstance( channels, (collections.Sized, collections.Iterable)): raise TypeError( "Expected a collection of channels (each channel should be %s)" % text) if len(channels) > 10: raise ValueError("Too many channels") for channel in channels: validate_channel(channel) if not isinstance(event_name, six.text_type): raise TypeError("event_name should be %s" % text) if len(event_name) > 200: raise ValueError("event_name too long") if not isinstance(data, six.text_type): data = json.dumps(data) if len(data) > 10240: raise ValueError("Too much data") params = {'name': event_name, 'channels': channels, 'data': data} if socket_id: if not isinstance(socket_id, six.text_type): raise TypeError("Socket ID should be %s" % text) params['socket_id'] = socket_id return Request(self.config, POST, "/apps/%s/events" % self.config.app_id, params)
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) or not isinstance(channels, (collections.Sized, collections.Iterable)): raise TypeError("Expected a collection of channels (each channel should be %s)" % text) if len(channels) > 10: raise ValueError("Too many channels") for channel in channels: validate_channel(channel) if not isinstance(event_name, six.text_type): raise TypeError("event_name should be %s" % text) if len(event_name) > 200: raise ValueError("event_name too long") if not isinstance(data, six.text_type): data = json.dumps(data) if len(data) > 10240: raise ValueError("Too much data") params = { 'name': event_name, 'channels': channels, 'data': data } if socket_id: if not isinstance(socket_id, six.text_type): raise TypeError("Socket ID should be %s" % text) params['socket_id'] = socket_id return Request(self.config, POST, "/apps/%s/events" % self.config.app_id, params)
def authenticate(self, channel, socket_id, custom_data=None): """Used to generate delegated client subscription token. :param channel: name of the channel to authorize subscription to :param socket_id: id of the socket that requires authorization :param custom_data: used on presence channels to provide user info """ channel = validate_channel(channel) if not channel_name_re.match(channel): raise ValueError('Channel should be a valid channel, got: %s' % channel) socket_id = validate_socket_id(socket_id) if custom_data: custom_data = json.dumps(custom_data, cls=self._json_encoder) string_to_sign = "%s:%s" % (socket_id, channel) if custom_data: string_to_sign += ":%s" % custom_data signature = sign(self.secret, string_to_sign) auth = "%s:%s" % (self.key, signature) response_payload = {"auth": auth} if is_encrypted_channel(channel): shared_secret = generate_shared_secret( ensure_binary(channel, "channel"), self._encryption_master_key) shared_secret_b64 = base64.b64encode(shared_secret) response_payload["shared_secret"] = shared_secret_b64 if custom_data: response_payload['channel_data'] = custom_data return response_payload