diff options
Diffstat (limited to 'common/PubnubBase.py')
| -rw-r--r-- | common/PubnubBase.py | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/common/PubnubBase.py b/common/PubnubBase.py index 5856921..43eee2a 100644 --- a/common/PubnubBase.py +++ b/common/PubnubBase.py @@ -15,7 +15,8 @@ class PubnubBase(object): cipher_key = False, ssl_on = False, origin = 'pubsub.pubnub.com', - UUID = None + UUID = None, + auth_key = None ) : """ #** @@ -43,6 +44,7 @@ class PubnubBase(object): self.cipher_key = cipher_key self.ssl = ssl_on self.pc = PubnubCrypto() + self.auth_key = auth_key if self.ssl : self.origin = 'https://' + self.origin @@ -82,6 +84,13 @@ class PubnubBase(object): return message + def _return_wrapped_callback(self, callback=None): + def _new_format_callback(response): + if 'payload' in response: + if (callback != None): callback({'message' : response['message'], 'payload' : response['payload']}) + else: + if (callback != None):callback(response) + if (callback != None): return _new_format_callback def publish( self, args ) : """ @@ -131,7 +140,7 @@ class PubnubBase(object): channel, '0', message - ]}, callback) + ], 'urlparams' : {'auth' : self.auth_key}}, self._return_wrapped_callback(callback)) def presence( self, args ) : """ @@ -171,7 +180,7 @@ class PubnubBase(object): callback = args['callback'] subscribe_key = args.get('subscribe_key') or self.subscribe_key - return self.subscribe({'channel': channel+'-pnpres', 'subscribe_key':subscribe_key, 'callback': callback}) + return self.subscribe({'channel': channel+'-pnpres', 'subscribe_key':subscribe_key, 'callback': self._return_wrapped_callback(callback)}) def here_now( self, args ) : @@ -211,7 +220,7 @@ class PubnubBase(object): 'v2','presence', 'sub_key', self.subscribe_key, 'channel', channel - ]}, callback); + ], 'urlparams' : {'auth' : self.auth_key}}, self._return_wrapped_callback(callback)); def history( self, args ) : @@ -255,7 +264,7 @@ class PubnubBase(object): channel, '0', str(limit) - ] }, callback); + ], 'urlparams' : {'auth' : self.auth_key} }, self._return_wrapped_callback(callback)); def detailedHistory(self, args) : """ @@ -286,6 +295,7 @@ class PubnubBase(object): count = int(args['count']) params['count'] = str(count) + params['auth'] = self.auth_key if args.has_key('reverse'): params['reverse'] = str(args['reverse']).lower() @@ -315,7 +325,7 @@ class PubnubBase(object): self.subscribe_key, 'channel', channel, - ],'urlparams' : params }, callback=callback); + ],'urlparams' : params }, callback=self._return_wrapped_callback(callback)); def time(self, args = None) : """ @@ -345,20 +355,30 @@ class PubnubBase(object): return time[0] + def _encode( self, request ) : - return [ + return "".join([ "".join([ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and hex(ord(ch)).replace( '0x', '%' ).upper() or ch for ch in list(bit) - ]) for bit in request] + ]) for bit in request]) + def _add_param(self, key, value): + if value: + return key + "=" + self._encode(value) + else: + return "" + def getUrl(self,request): ## Build URL + args_string = None url = self.origin + '/' + "/".join([ "".join([ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and hex(ord(ch)).replace( '0x', '%' ).upper() or ch for ch in list(bit) ]) for bit in request["urlcomponents"]]) if (request.has_key("urlparams")): - url = url + '?' + "&".join([ x + "=" + y for x,y in request["urlparams"].iteritems()]) + args_string = "&".join([ self._add_param(x,y) for x,y in request["urlparams"].iteritems()]) + if (args_string != None and len(args_string) > 0): + url = url + '?' + args_string return url |
