<s> import requests <EOL> try : <EOL> from urllib . parse import urlparse , urlsplit , urlunsplit <EOL> except ImportError : <EOL> from urlparse import urlparse , urlsplit , urlunsplit <EOL> from . import exceptions <EOL> from . serialize import Serializer <EOL> from . utils import url_join , iterator , copy_kwargs <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class ResourceAttributesMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , item ) : <EOL> if item . startswith ( "<STR_LIT:_>" ) : <EOL> raise AttributeError ( item ) <EOL> kwargs = copy_kwargs ( self . _store ) <EOL> kwargs . update ( { "<STR_LIT>" : url_join ( self . _store [ "<STR_LIT>" ] , item ) } ) <EOL> return self . _get_resource ( ** kwargs ) <EOL> class Resource ( ResourceAttributesMixin , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _store = kwargs <EOL> def __call__ ( self , id = None , format = None , url_override = None ) : <EOL> """<STR_LIT>""" <EOL> if id is None and format is None and url_override is None : <EOL> return self <EOL> kwargs = copy_kwargs ( self . _store ) <EOL> if id is not None : <EOL> kwargs [ "<STR_LIT>" ] = url_join ( self . _store [ "<STR_LIT>" ] , id ) <EOL> if format is not None : <EOL> kwargs [ "<STR_LIT>" ] = format <EOL> if url_override is not None : <EOL> kwargs [ "<STR_LIT>" ] = url_override <EOL> kwargs [ "<STR_LIT>" ] = self . _store [ "<STR_LIT>" ] <EOL> return self . _get_resource ( ** kwargs ) <EOL> def _request ( self , method , data = None , files = None , params = None ) : <EOL> serializer = self . _store [ "<STR_LIT>" ] <EOL> url = self . url ( ) <EOL> headers = { "<STR_LIT>" : serializer . get_content_type ( ) } <EOL> if not files : <EOL> headers [ "<STR_LIT>" ] = serializer . get_content_type ( ) <EOL> if data is not None : <EOL> data = serializer . dumps ( data ) <EOL> resp = self . _store [ "<STR_LIT>" ] . request ( method , url , data = data , params = params , files = files , headers = headers ) <EOL> if <NUM_LIT> <= resp . status_code <= <NUM_LIT> : <EOL> exception_class = exceptions . HttpNotFoundError if resp . status_code == <NUM_LIT> else exceptions . HttpClientError <EOL> raise exception_class ( "<STR_LIT>" % ( resp . status_code , url ) , response = resp , content = resp . content ) <EOL> elif <NUM_LIT> <= resp . status_code <= <NUM_LIT> : <EOL> raise exceptions . HttpServerError ( "<STR_LIT>" % ( resp . status_code , url ) , response = resp , content = resp . content ) <EOL> self . _ = resp <EOL> return resp <EOL> def _handle_redirect ( self , resp , ** kwargs ) : <EOL> resource_obj = self ( url_override = resp . headers [ "<STR_LIT:location>" ] ) <EOL> return resource_obj . get ( ** kwargs ) <EOL> def _try_to_serialize_response ( self , resp ) : <EOL> s = self . _store [ "<STR_LIT>" ] <EOL> if resp . status_code in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> return <EOL> if resp . headers . get ( "<STR_LIT>" , None ) and resp . content : <EOL> content_type = resp . headers . get ( "<STR_LIT>" ) . split ( "<STR_LIT:;>" ) [ <NUM_LIT:0> ] . strip ( ) <EOL> try : <EOL> stype = s . get_serializer ( content_type = content_type ) <EOL> except exceptions . SerializerNotAvailable : <EOL> return resp . content <EOL> if type ( resp . content ) == bytes : <EOL> try : <EOL> encoding = requests . utils . guess_json_utf ( resp . content ) <EOL> return stype . loads ( resp . content . decode ( encoding ) ) <EOL> except : <EOL> return resp . content <EOL> return stype . loads ( resp . content ) <EOL> else : <EOL> return resp . content <EOL> def _process_response ( self , resp ) : <EOL> if <NUM_LIT:200> <= resp . status_code <= <NUM_LIT> : <EOL> return self . _try_to_serialize_response ( resp ) <EOL> else : <EOL> return <EOL> def url ( self ) : <EOL> url = self . _store [ "<STR_LIT>" ] <EOL> if self . _store [ "<STR_LIT>" ] and not url . endswith ( "<STR_LIT:/>" ) : <EOL> url = url + "<STR_LIT:/>" <EOL> return url <EOL> def get ( self , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT:GET>" , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def options ( self , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT>" , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def head ( self , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT>" , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def post ( self , data = None , files = None , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT:POST>" , data = data , files = files , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def patch ( self , data = None , files = None , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT>" , data = data , files = files , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def put ( self , data = None , files = None , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT>" , data = data , files = files , params = kwargs ) <EOL> return self . _process_response ( resp ) <EOL> def delete ( self , ** kwargs ) : <EOL> resp = self . _request ( "<STR_LIT>" , params = kwargs ) <EOL> if <NUM_LIT:200> <= resp . status_code <= <NUM_LIT> : <EOL> if resp . status_code == <NUM_LIT> : <EOL> return True <EOL> else : <EOL> return True <EOL> else : <EOL> return False <EOL> def _get_resource ( self , ** kwargs ) : <EOL> return self . __class__ ( ** kwargs ) <EOL> class API ( ResourceAttributesMixin , object ) : <EOL> resource_class = Resource <EOL> def __init__ ( self , base_url = None , auth = None , format = None , append_slash = True , session = None , serializer = None ) : <EOL> if serializer is None : <EOL> serializer = Serializer ( default = format ) <EOL> if session is None : <EOL> session = requests . session ( ) <EOL> if auth is not None : <EOL> session . auth = auth <EOL> self . _store = { <EOL> "<STR_LIT>" : base_url , <EOL> "<STR_LIT>" : format if format is not None else "<STR_LIT>" , <EOL> "<STR_LIT>" : append_slash , <EOL> "<STR_LIT>" : session , <EOL> "<STR_LIT>" : serializer , <EOL> } <EOL> if self . _store . get ( "<STR_LIT>" ) is None : <EOL> raise exceptions . ImproperlyConfigured ( "<STR_LIT>" ) <EOL> def _get_resource ( self , ** kwargs ) : <EOL> return self . resource_class ( ** kwargs ) </s>
