<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> import json <EOL> import time <EOL> import hmac <EOL> import base64 <EOL> import hashlib <EOL> import requests <EOL> from email import utils <EOL> try : import urllib . parse as urllib <EOL> except ImportError : import urllib <EOL> __publicReadEndpoint__ = "<STR_LIT>" <EOL> __readEndpoint__ = "<STR_LIT>" <EOL> __writeEndpoint__ = "<STR_LIT>" <EOL> __publishEndpoint__ = "<STR_LIT>" <EOL> __connectionsEndpoint__ = "<STR_LIT>" <EOL> __channelsEndpoint__ = "<STR_LIT>" <EOL> class BBT : <EOL> akey = None <EOL> skey = None <EOL> token = None <EOL> hostname = None <EOL> port = None <EOL> ssl = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , akey = None , skey = None , token = None , hostname = "<STR_LIT>" , port = "<STR_LIT>" , ssl = False ) : <EOL> if akey and skey and token == None : <EOL> self . akey = akey <EOL> self . skey = skey <EOL> elif akey == None and skey == None and token : <EOL> self . token = token <EOL> else : <EOL> raise InitializationError ( "<STR_LIT>" ) <EOL> self . hostname = hostname <EOL> self . port = port <EOL> self . ssl = ssl <EOL> """<STR_LIT>""" <EOL> def sign ( self , stringToSign ) : <EOL> signature = hmac . new ( self . skey . encode ( ) , stringToSign . encode ( ) , hashlib . sha1 ) <EOL> return "<STR_LIT>" % ( self . akey , bytes . decode ( base64 . b64encode ( signature . digest ( ) ) ) ) <EOL> """<STR_LIT>""" <EOL> def __signRequest__ ( self , verb , uri , date , c_type , c_md5 = "<STR_LIT>" ) : <EOL> stringToSign = "<STR_LIT>" % ( verb , c_md5 , c_type , date , uri ) <EOL> return self . sign ( stringToSign ) <EOL> """<STR_LIT>""" <EOL> def __processResponse__ ( self , response ) : <EOL> code = response [ '<STR_LIT:status>' ] <EOL> data = { } <EOL> if response [ '<STR_LIT:data>' ] : <EOL> data = json . loads ( response [ '<STR_LIT:data>' ] ) <EOL> if code < <NUM_LIT> : <EOL> return data <EOL> else : <EOL> errcode = data [ '<STR_LIT:error>' ] [ '<STR_LIT:code>' ] <EOL> errmsg = data [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] <EOL> if code == <NUM_LIT> : <EOL> if errcode == <NUM_LIT> : <EOL> raise AuthenticationError ( "<STR_LIT>" % errmsg ) <EOL> elif errcode == <NUM_LIT> : <EOL> raise ParameterError ( "<STR_LIT>" % errmsg ) <EOL> elif errcode == <NUM_LIT> : <EOL> raise BadRequestError ( "<STR_LIT>" % errmsg ) <EOL> elif errcode == <NUM_LIT> : <EOL> raise TypeError ( "<STR_LIT>" % errmsg ) <EOL> elif errcode == <NUM_LIT> : <EOL> raise BadTypeError ( "<STR_LIT>" % errmsg ) <EOL> elif errcode == <NUM_LIT> : <EOL> raise PayloadLimitError ( "<STR_LIT>" % errmsg ) <EOL> else : <EOL> raise UnexpectedError ( "<STR_LIT>" % ( code , errcode , errmsg ) ) <EOL> elif code == <NUM_LIT> : <EOL> if errcode == <NUM_LIT> : <EOL> raise NotAllowedError ( "<STR_LIT>" % errmsg ) <EOL> else : <EOL> raise UnexpectedError ( "<STR_LIT>" % ( code , errcode , errmsg ) ) <EOL> elif code == <NUM_LIT> : <EOL> if errcode == <NUM_LIT> : <EOL> raise InternalError ( "<STR_LIT>" % errmsg ) <EOL> else : <EOL> raise InternalError ( "<STR_LIT>" % ( code , errcode , errmsg ) ) <EOL> elif code == <NUM_LIT> : <EOL> if errcode == <NUM_LIT> : <EOL> raise NotFoundError ( "<STR_LIT>" % errmsg ) <EOL> if errcode == <NUM_LIT> : <EOL> raise NotFoundError ( "<STR_LIT>" % errmsg ) <EOL> if errcode == <NUM_LIT> : <EOL> raise NotFoundError ( "<STR_LIT>" % errmsg ) <EOL> if errcode == <NUM_LIT> : <EOL> raise AlreadyExistError ( "<STR_LIT>" % errmsg ) <EOL> if errcode == <NUM_LIT> : <EOL> raise AlreadyExistError ( "<STR_LIT>" % errmsg ) <EOL> if errcode == <NUM_LIT> : <EOL> raise AlreadyExistError ( "<STR_LIT>" % errmsg ) <EOL> else : <EOL> raise UnexpectedError ( "<STR_LIT>" % ( code , errcode , errmsg ) ) <EOL> else : <EOL> raise UnexpectedError ( "<STR_LIT>" % ( code , errcode , errmsg ) ) <EOL> """<STR_LIT>""" <EOL> def __postData__ ( self , uri , data , auth = True ) : <EOL> if self . ssl : <EOL> url = "<STR_LIT>" % ( '<STR_LIT>' , self . hostname , self . port , uri ) <EOL> else : <EOL> url = "<STR_LIT>" % ( '<STR_LIT:http>' , self . hostname , self . port , uri ) <EOL> if self . token : <EOL> if auth : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : self . token } <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> else : <EOL> md5 = bytes . decode ( base64 . b64encode ( hashlib . md5 ( str . encode ( data ) ) . digest ( ) ) ) <EOL> date = utils . formatdate ( ) <EOL> if auth : <EOL> sig = self . __signRequest__ ( '<STR_LIT:POST>' , uri , date , "<STR_LIT:application/json>" , md5 ) <EOL> headers = { '<STR_LIT>' : md5 , '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date , '<STR_LIT>' : sig } <EOL> else : <EOL> headers = { '<STR_LIT>' : md5 , '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date } <EOL> r = requests . post ( url , data = data , headers = headers ) <EOL> return self . __processResponse__ ( { '<STR_LIT:status>' : r . status_code , '<STR_LIT:data>' : r . text } ) <EOL> """<STR_LIT>""" <EOL> def __getData__ ( self , uri , query , auth = True ) : <EOL> if self . ssl : <EOL> url = "<STR_LIT>" % ( '<STR_LIT>' , self . hostname , self . port , uri ) <EOL> else : <EOL> url = "<STR_LIT>" % ( '<STR_LIT:http>' , self . hostname , self . port , uri ) <EOL> full_uri = "<STR_LIT:%s>" % ( uri ) <EOL> if query != None : <EOL> full_uri = "<STR_LIT>" % ( full_uri , urllib . urlencode ( query ) ) <EOL> if self . token : <EOL> if auth : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : self . token } <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> else : <EOL> date = utils . formatdate ( ) <EOL> if auth : <EOL> sig = self . __signRequest__ ( '<STR_LIT:GET>' , full_uri , date , "<STR_LIT:application/json>" ) <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date , '<STR_LIT>' : sig } <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date } <EOL> r = requests . get ( url , params = query , headers = headers ) <EOL> return self . __processResponse__ ( { '<STR_LIT:status>' : r . status_code , '<STR_LIT:data>' : r . text } ) <EOL> """<STR_LIT>""" <EOL> def __deleteData__ ( self , uri , query , auth = True ) : <EOL> if self . ssl : <EOL> url = "<STR_LIT>" % ( '<STR_LIT>' , self . hostname , self . port , uri ) <EOL> else : <EOL> url = "<STR_LIT>" % ( '<STR_LIT:http>' , self . hostname , self . port , uri ) <EOL> full_uri = "<STR_LIT:%s>" % ( uri ) <EOL> if query != None : <EOL> full_uri = "<STR_LIT>" % ( full_uri , urllib . urlencode ( query ) ) <EOL> if self . token : <EOL> if auth : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : self . token } <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> else : <EOL> date = utils . formatdate ( ) <EOL> if auth : <EOL> sig = self . __signRequest__ ( '<STR_LIT>' , full_uri , date , "<STR_LIT:application/json>" ) <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date , '<STR_LIT>' : sig } <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : date } <EOL> r = requests . delete ( url , params = query , headers = headers ) <EOL> return self . __processResponse__ ( { '<STR_LIT:status>' : r . status_code , '<STR_LIT:data>' : r . text } ) <EOL> """<STR_LIT>""" <EOL> def readPublic ( self , owner , channel , resource , limit = <NUM_LIT> , source = "<STR_LIT>" , time_range = None , data_filter = None , sample_rate = None ) : <EOL> query = { '<STR_LIT>' : limit , '<STR_LIT:source>' : source } <EOL> if time_range : <EOL> query [ '<STR_LIT>' ] = time_range <EOL> if data_filter : <EOL> query [ '<STR_LIT>' ] = data_filter <EOL> if sample_rate : <EOL> query [ '<STR_LIT>' ] = sample - rate <EOL> endpoint = "<STR_LIT>" % ( __publicReadEndpoint__ , owner , channel , resource ) <EOL> response = self . __getData__ ( endpoint , query , False ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def read ( self , channel , resource , limit = <NUM_LIT:1> , source = "<STR_LIT>" , time_range = None , data_filter = None , sample_rate = None ) : <EOL> query = { '<STR_LIT>' : limit , '<STR_LIT:source>' : source } <EOL> if time_range : <EOL> query [ '<STR_LIT>' ] = time_range <EOL> if data_filter : <EOL> query [ '<STR_LIT>' ] = data_filter <EOL> if sample_rate : <EOL> query [ '<STR_LIT>' ] = sample - rate <EOL> endpoint = "<STR_LIT>" % ( __readEndpoint__ , channel , resource ) <EOL> response = self . __getData__ ( endpoint , query , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def write ( self , channel , resource , data , ts = None ) : <EOL> body = { '<STR_LIT:data>' : data } <EOL> if ts != None : <EOL> body [ '<STR_LIT>' ] = ts <EOL> else : <EOL> body [ '<STR_LIT>' ] = round ( time . time ( ) * <NUM_LIT:1000> ) <EOL> endpoint = "<STR_LIT>" % ( __writeEndpoint__ , channel , resource ) <EOL> response = self . __postData__ ( endpoint , json . dumps ( body , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def writeBulk ( self , channel , data_array ) : <EOL> body = { '<STR_LIT>' : data_array } <EOL> endpoint = "<STR_LIT>" % ( __writeEndpoint__ , channel ) <EOL> response = self . __postData__ ( endpoint , json . dumps ( body , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def publish ( self , channel , resource , data , ts = None , source = None ) : <EOL> body = { '<STR_LIT:data>' : data } <EOL> if source : <EOL> body [ '<STR_LIT:source>' ] = source <EOL> if ts != None : <EOL> body [ '<STR_LIT>' ] = ts <EOL> else : <EOL> body [ '<STR_LIT>' ] = round ( time . time ( ) * <NUM_LIT:1000> ) <EOL> endpoint = "<STR_LIT>" % ( __publishEndpoint__ , channel , resource ) <EOL> response = self . __postData__ ( endpoint , json . dumps ( body , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def publishBulk ( self , channel , data_array ) : <EOL> body = { '<STR_LIT>' : data_array } <EOL> endpoint = "<STR_LIT>" % ( __publishEndpoint__ , channel ) <EOL> response = self . __postData__ ( endpoint , json . dumps ( body , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def auth_client ( self , sid , channel , resource = '<STR_LIT:*>' , ttl = <NUM_LIT:0> , read = False , write = False ) : <EOL> r = '<STR_LIT:false>' <EOL> w = '<STR_LIT:false>' <EOL> if read : <EOL> r = '<STR_LIT:true>' <EOL> if write : <EOL> w = '<STR_LIT:true>' <EOL> stringToSign = "<STR_LIT>" % ( sid , channel , resource , ttl , r , w ) <EOL> return self . sign ( stringToSign ) <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> def getUserConnections ( self , userid = None , sid = None ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = __connectionsEndpoint__ <EOL> query = None <EOL> if userid != None : <EOL> endpoint = "<STR_LIT>" % ( __connectionsEndpoint__ , userid ) <EOL> elif sid != None : <EOL> query = { '<STR_LIT>' : sid } <EOL> response = self . __getData__ ( endpoint , query , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def dropUserConnection ( self , userid , sid = None ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> query = None <EOL> if sid != None : <EOL> query = { '<STR_LIT>' : sid } <EOL> endpoint = "<STR_LIT>" % ( __connectionsEndpoint__ , userid ) <EOL> response = self . __deleteData__ ( endpoint , query , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> def getResource ( self , channel , resource = None ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = "<STR_LIT>" % ( __channelsEndpoint__ , channel ) <EOL> if resource != None : <EOL> endpoint = "<STR_LIT>" % ( endpoint , resource ) <EOL> response = self . __getData__ ( endpoint , None , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def deleteResource ( self , channel , resource ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = "<STR_LIT>" % ( __channelsEndpoint__ , channel , resource ) <EOL> response = self . __deleteData__ ( endpoint , None , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def getChannel ( self , channel = None ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = __channelsEndpoint__ <EOL> if channel != None : <EOL> endpoint = "<STR_LIT>" % ( endpoint , channel ) <EOL> response = self . __getData__ ( endpoint , None , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def deleteChannel ( self , channel ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = "<STR_LIT>" % ( __channelsEndpoint__ , channel ) <EOL> response = self . __deleteData__ ( endpoint , None , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> def regenerateChannelToken ( self , channel ) : <EOL> if self . skey == None : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> endpoint = "<STR_LIT>" % ( __channelsEndpoint__ , channel ) <EOL> response = self . __getData__ ( endpoint , None , True ) <EOL> return response ; <EOL> """<STR_LIT>""" <EOL> class Resource : <EOL> channel = None <EOL> resource = None <EOL> bbt = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bbt , channel , resource ) : <EOL> self . channel = channel <EOL> self . resource = resource <EOL> self . bbt = bbt <EOL> """<STR_LIT>""" <EOL> def write ( self , data , ts = None ) : <EOL> return self . bbt . write ( self . channel , self . resource , ts = ts , data = data ) <EOL> """<STR_LIT>""" <EOL> def publish ( self , data , ts = None ) : <EOL> return self . bbt . publish ( self . channel , self . resource , ts = ts , data = data ) <EOL> """<STR_LIT>""" <EOL> def read ( self , limit = <NUM_LIT> , owner = None , source = "<STR_LIT>" , time_range = None , data_filter = None , sample_rate = None ) : <EOL> if owner != None : <EOL> return self . bbt . publicRead ( owner , self . channel , self . resource , limit , source , time_range , data_filter , sample_rate ) <EOL> else : <EOL> return self . bbt . read ( channel = self . channel , resource = self . resource , limit = limit , source = source , time_range = time_range ) <EOL> """<STR_LIT>""" <EOL> def recentVal ( self ) : <EOL> return self . bbt . read ( self . channel , self . resource , limit = <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> class DataPoint : <EOL> channel = None <EOL> resource = None <EOL> data = None <EOL> ts = None <EOL> def __init__ ( self , data , ts , channel = None , resource = None ) : <EOL> self . channel = channel <EOL> self . resource = resource <EOL> self . data = data <EOL> self . ts = ts <EOL> @ classmethod <EOL> def fromJSON ( cls , params ) : <EOL> return cls ( data = params [ '<STR_LIT:data>' ] , ts = params [ '<STR_LIT>' ] ) <EOL> def toJSON ( self , owner = None ) : <EOL> return { '<STR_LIT>' : owner , '<STR_LIT>' : self . channel , '<STR_LIT>' : self . resource , '<STR_LIT:data>' : self . data , '<STR_LIT>' : self . ts } <EOL> class InitializationError ( Exception ) : <EOL> pass <EOL> class AuthenticationError ( Exception ) : <EOL> pass <EOL> class InternalError ( Exception ) : <EOL> pass <EOL> class NotFoundError ( Exception ) : <EOL> pass <EOL> class AlreadyExistError ( Exception ) : <EOL> pass <EOL> class NotAllowedError ( Exception ) : <EOL> pass <EOL> class UsageLimitError ( Exception ) : <EOL> pass <EOL> class PayloadLimitError ( Exception ) : <EOL> pass <EOL> class UnexpectedError ( Exception ) : <EOL> pass <EOL> class BadRequestError ( Exception ) : <EOL> pass <EOL> class ParameterError ( Exception ) : <EOL> pass <EOL> class TypeError ( Exception ) : <EOL> pass <EOL> class BadTypeError ( Exception ) : <EOL> pass </s>
