<s> """<STR_LIT>""" <EOL> import six <EOL> import time <EOL> from ubersmith . compat import total_ordering , file_type <EOL> import requests <EOL> from ubersmith . exceptions import ( <EOL> RequestError , <EOL> ResponseError , <EOL> UpdatingTokenResponse , <EOL> MaintenanceResponse , <EOL> ) <EOL> from ubersmith . utils import ( <EOL> append_qs , <EOL> to_nested_php_args , <EOL> get_filename , <EOL> ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> _DEFAULT_REQUEST_HANDLER = None <EOL> """<STR_LIT>""" <EOL> METHODS = { <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u"<STR_LIT>" , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> class _ProxyModule ( object ) : <EOL> def __init__ ( self , handler , module ) : <EOL> self . handler = handler <EOL> self . module = module <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> call_func = getattr ( self . module , name ) <EOL> if callable ( call_func ) : <EOL> call_p = call_func . handler ( self . handler ) <EOL> setattr ( self , name , call_p ) <EOL> return call_p <EOL> raise AttributeError ( "<STR_LIT>" . format ( <EOL> type ( self ) . __name__ , name ) ) <EOL> class RequestHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , base_url , username = None , password = None , verify = True , <EOL> session = None ) : <EOL> """<STR_LIT>""" <EOL> self . base_url = base_url <EOL> self . username = username <EOL> self . password = password <EOL> self . verify = verify <EOL> if session is None : <EOL> session = requests . session ( ) <EOL> self . _session = session <EOL> @ property <EOL> def session ( self ) : <EOL> return self . _session <EOL> def process_request ( self , method , data = None ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_request_method ( method ) <EOL> attempts = <NUM_LIT:3> <EOL> for i in range ( attempts ) : <EOL> response = self . _send_request ( method , data ) <EOL> if self . _is_token_response ( response ) : <EOL> if i < attempts - <NUM_LIT:1> : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> continue <EOL> else : <EOL> raise UpdatingTokenResponse <EOL> break <EOL> resp = BaseResponse ( response ) <EOL> if response . headers . get ( '<STR_LIT>' ) == '<STR_LIT:application/json>' : <EOL> if not resp . json . get ( '<STR_LIT:status>' ) : <EOL> if all ( [ <EOL> resp . json . get ( '<STR_LIT>' ) == <NUM_LIT:1> , <EOL> resp . json . get ( '<STR_LIT>' ) == u"<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ] ) : <EOL> raise MaintenanceResponse ( response = resp . json ) <EOL> else : <EOL> raise ResponseError ( response = resp . json ) <EOL> return resp <EOL> @ staticmethod <EOL> def _is_token_response ( response ) : <EOL> return ( '<STR_LIT>' in response . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) and <EOL> '<STR_LIT>' in response . content ) <EOL> def _send_request ( self , method , data ) : <EOL> url = append_qs ( self . base_url , { '<STR_LIT>' : method } ) <EOL> data , files , headers = self . _encode_data ( data ) <EOL> return self . session . post ( url , data = data , files = files , headers = headers , <EOL> auth = ( self . username , self . password ) , <EOL> verify = self . verify ) <EOL> @ staticmethod <EOL> def _validate_request_method ( method ) : <EOL> """<STR_LIT>""" <EOL> if method not in METHODS : <EOL> raise RequestError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def _encode_data ( data ) : <EOL> """<STR_LIT>""" <EOL> data = data if data is not None else { } <EOL> data = to_nested_php_args ( data ) <EOL> files = dict ( [ <EOL> ( key , value ) for key , value in <EOL> data . items ( ) if isinstance ( value , file_type ) ] ) <EOL> for fname in files : <EOL> del data [ fname ] <EOL> return data , files or None , None <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in set ( m . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] for m in METHODS ) : <EOL> module_name = '<STR_LIT>' . format ( name ) <EOL> module = __import__ ( module_name , fromlist = [ '<STR_LIT>' ] ) <EOL> proxy = _ProxyModule ( self , module ) <EOL> setattr ( self , name , proxy ) <EOL> return proxy <EOL> raise AttributeError ( "<STR_LIT>" . format ( <EOL> type ( self ) . __name__ , name ) ) <EOL> class BaseResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response ) : <EOL> self . response = response <EOL> @ classmethod <EOL> def from_cleaned ( cls , response , cleaned ) : <EOL> resp = cls ( response . response ) <EOL> resp . cleaned = cleaned <EOL> return resp <EOL> @ property <EOL> def json ( self ) : <EOL> return self . response . json ( ) <EOL> @ property <EOL> def data ( self ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return self . cleaned <EOL> else : <EOL> return self . json [ '<STR_LIT:data>' ] <EOL> @ property <EOL> def type ( self ) : <EOL> return self . response . headers . get ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return str ( self . data ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . data ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . data ) <EOL> def __json__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data <EOL> @ total_ordering <EOL> class DictResponse ( BaseResponse ) : <EOL> __marker = object ( ) <EOL> def keys ( self ) : <EOL> return self . data . keys ( ) <EOL> def iterkeys ( self ) : <EOL> return six . iterkeys ( self . data ) <EOL> def values ( self ) : <EOL> return self . data . values ( ) <EOL> def itervalues ( self ) : <EOL> return six . itervalues ( self . data ) <EOL> def items ( self ) : <EOL> return self . data . items ( ) <EOL> def iteritems ( self ) : <EOL> return six . iteritems ( self . data ) <EOL> def get ( self , key , default = None ) : <EOL> return self . data . get ( key , default ) <EOL> def update ( self , d ) : <EOL> self . data . update ( d ) <EOL> def setdefault ( self , key , value ) : <EOL> self . data . setdefault ( key , value ) <EOL> def pop ( self , key , default = __marker ) : <EOL> if default is self . __marker : <EOL> return self . data . pop ( key ) <EOL> else : <EOL> return self . data . pop ( key , default ) <EOL> def popitem ( self ) : <EOL> return self . data . popitem ( ) <EOL> def clear ( self ) : <EOL> self . data . clear ( ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . data [ key ] = value <EOL> def __iter__ ( self ) : <EOL> return iter ( self . data ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . data [ key ] <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> def __eq__ ( self , other ) : <EOL> return self . data == other <EOL> def __lt__ ( self , other ) : <EOL> return self . data < other <EOL> def __contains__ ( self , item ) : <EOL> return item in self . data <EOL> @ total_ordering <EOL> class IntResponse ( BaseResponse ) : <EOL> @ property <EOL> def numerator ( self ) : <EOL> return self . data <EOL> @ property <EOL> def denominator ( self ) : <EOL> return <NUM_LIT:1> <EOL> @ property <EOL> def real ( self ) : <EOL> return self . data <EOL> @ property <EOL> def imag ( self ) : <EOL> return <NUM_LIT:0> <EOL> def bit_length ( self ) : <EOL> if hasattr ( self . data , '<STR_LIT>' ) : <EOL> return self . data . bit_length ( ) <EOL> else : <EOL> return len ( bin ( abs ( self . data ) ) ) - <NUM_LIT:2> <EOL> def conjugate ( self ) : <EOL> return self . data <EOL> def __int__ ( self ) : <EOL> return self . data <EOL> __index__ = __long__ = __trunc__ = __int__ <EOL> def __float__ ( self ) : <EOL> return float ( self . data ) <EOL> def __oct__ ( self ) : <EOL> return oct ( self . data ) <EOL> def __hex__ ( self ) : <EOL> return hex ( self . data ) <EOL> def __eq__ ( self , other ) : <EOL> return self . data == other <EOL> def __lt__ ( self , other ) : <EOL> return self . data < other <EOL> def __add__ ( self , other ) : <EOL> return int ( self ) + other <EOL> __radd__ = __add__ <EOL> def __sub__ ( self , other ) : <EOL> return int ( self ) - other <EOL> def __rsub__ ( self , other ) : <EOL> return other - int ( self ) <EOL> def __mul__ ( self , other ) : <EOL> return int ( self ) * other <EOL> __rmul__ = __mul__ <EOL> def __div__ ( self , other ) : <EOL> return int ( self ) / other <EOL> def __rdiv__ ( self , other ) : <EOL> return other / int ( self ) <EOL> def __floordiv__ ( self , other ) : <EOL> return int ( self ) // other <EOL> def __rfloordiv__ ( self , other ) : <EOL> return other // int ( self ) <EOL> def __truediv__ ( self , other ) : <EOL> return float ( self ) / other <EOL> def __rtruediv__ ( self , other ) : <EOL> return other / float ( self ) <EOL> def __mod__ ( self , other ) : <EOL> return int ( self ) % other <EOL> def __rmod__ ( self , other ) : <EOL> return other % int ( self ) <EOL> def __pow__ ( self , other ) : <EOL> return int ( self ) ** other <EOL> def __rpow__ ( self , other ) : <EOL> return other ** int ( self ) <EOL> def __abs__ ( self ) : <EOL> return abs ( self . data ) <EOL> def __neg__ ( self ) : <EOL> return - self . data <EOL> def __pos__ ( self ) : <EOL> return self . data <EOL> def __divmod__ ( self , other ) : <EOL> return self // other , self % other <EOL> def __rdivmod__ ( self , other ) : <EOL> return other // self , other % self <EOL> def __and__ ( self , other ) : <EOL> return self . data & other <EOL> __rand__ = __and__ <EOL> def __or__ ( self , other ) : <EOL> return self . data | other <EOL> __ror__ = __or__ <EOL> def __xor__ ( self , other ) : <EOL> return self . data ^ other <EOL> __rxor__ = __xor__ <EOL> def __lshift__ ( self , other ) : <EOL> return self . data << other <EOL> def __rlshift__ ( self , other ) : <EOL> return other << self . data <EOL> def __rshift__ ( self , other ) : <EOL> return self . data >> other <EOL> def __rrshift__ ( self , other ) : <EOL> return other >> self . data <EOL> def __invert__ ( self ) : <EOL> return ~ self . data <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . data ) <EOL> class FileResponse ( BaseResponse ) : <EOL> @ property <EOL> def json ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def data ( self ) : <EOL> return self . response . content <EOL> @ property <EOL> def filename ( self ) : <EOL> disposition = self . response . headers . get ( '<STR_LIT>' ) <EOL> return get_filename ( disposition ) <EOL> def get_default_request_handler ( ) : <EOL> """<STR_LIT>""" <EOL> if not _DEFAULT_REQUEST_HANDLER : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return _DEFAULT_REQUEST_HANDLER <EOL> def set_default_request_handler ( request_handler ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( request_handler , RequestHandler ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> global _DEFAULT_REQUEST_HANDLER <EOL> _DEFAULT_REQUEST_HANDLER = request_handler </s>
