<s> import logging <EOL> import posixpath <EOL> from desktop . lib . i18n import smart_unicode <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Resource ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , relpath = "<STR_LIT>" , urlencode = True ) : <EOL> """<STR_LIT>""" <EOL> self . _client = client <EOL> self . _path = relpath . strip ( '<STR_LIT:/>' ) <EOL> self . _urlencode = urlencode <EOL> @ property <EOL> def base_url ( self ) : <EOL> return self . _client . base_url <EOL> def _join_uri ( self , relpath ) : <EOL> if relpath is None : <EOL> return self . _path <EOL> return self . _path + posixpath . normpath ( '<STR_LIT:/>' + relpath ) <EOL> def _format_response ( self , resp ) : <EOL> """<STR_LIT>""" <EOL> if resp . headers . get ( '<STR_LIT:location>' ) and resp . headers . get ( '<STR_LIT:location>' ) . startswith ( '<STR_LIT>' ) : <EOL> return resp . headers . get ( '<STR_LIT:location>' ) . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> if len ( resp . content ) != <NUM_LIT:0> and resp . headers . get ( '<STR_LIT>' ) and '<STR_LIT:application/json>' in resp . headers . get ( '<STR_LIT>' ) : <EOL> try : <EOL> return resp . json ( ) <EOL> except Exception , ex : <EOL> self . _client . logger . exception ( '<STR_LIT>' % resp . content ) <EOL> raise ex <EOL> else : <EOL> return resp . content <EOL> def invoke ( self , method , relpath = None , params = None , data = None , headers = None , files = None , allow_redirects = False ) : <EOL> """<STR_LIT>""" <EOL> path = self . _join_uri ( relpath ) <EOL> resp = self . _client . execute ( method , <EOL> path , <EOL> params = params , <EOL> data = data , <EOL> headers = headers , <EOL> files = files , <EOL> allow_redirects = allow_redirects , <EOL> urlencode = self . _urlencode ) <EOL> if self . _client . logger . isEnabledFor ( logging . DEBUG ) : <EOL> self . _client . logger . debug ( <EOL> "<STR_LIT>" % <EOL> ( method , <EOL> smart_unicode ( resp . content [ : <NUM_LIT:32> ] , errors = '<STR_LIT:replace>' ) , <EOL> len ( resp . content ) > <NUM_LIT:32> and "<STR_LIT>" or "<STR_LIT>" ) ) <EOL> return self . _format_response ( resp ) <EOL> def get ( self , relpath = None , params = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . invoke ( "<STR_LIT:GET>" , relpath , params , headers = headers , allow_redirects = True ) <EOL> def delete ( self , relpath = None , params = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> return self . invoke ( "<STR_LIT>" , relpath , params , headers = headers ) <EOL> def post ( self , relpath = None , params = None , data = None , contenttype = None , headers = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> return self . invoke ( "<STR_LIT:POST>" , relpath , params , data , self . _make_headers ( contenttype , headers ) , files ) <EOL> def put ( self , relpath = None , params = None , data = None , contenttype = None ) : <EOL> """<STR_LIT>""" <EOL> return self . invoke ( "<STR_LIT>" , relpath , params , data , self . _make_headers ( contenttype ) ) <EOL> def _make_headers ( self , contenttype = None , headers = None ) : <EOL> if headers is None : <EOL> headers = { } <EOL> if contenttype : <EOL> headers . update ( { '<STR_LIT:Content-Type>' : contenttype } ) <EOL> return headers </s>
