<s> import re <EOL> from . import interface , exc , meta , request <EOL> def validate ( obj ) : <EOL> """<STR_LIT>""" <EOL> members = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> metas = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:path>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> interface . validate ( IResource , obj , members ) <EOL> class IResource ( interface . Interface ) : <EOL> """<STR_LIT>""" <EOL> class ResourceHandler ( meta . MetaMixin ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> pass <EOL> def __init__ ( self , api_obj , name , path , ** kw ) : <EOL> super ( ResourceHandler , self ) . __init__ ( ** kw ) <EOL> self . api = api_obj <EOL> self . path = path <EOL> self . name = name <EOL> def filter ( self , params ) : <EOL> """<STR_LIT>""" <EOL> return params <EOL> class RESTResourceHandler ( ResourceHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api_obj , name , path , ** kw ) : <EOL> super ( RESTResourceHandler , self ) . __init__ ( api_obj , name , path , ** kw ) <EOL> def get ( self , resource_id = None , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> if resource_id : <EOL> path = '<STR_LIT>' % ( self . path , resource_id ) <EOL> else : <EOL> path = '<STR_LIT>' % self . path <EOL> try : <EOL> response = self . api . make_request ( '<STR_LIT:GET>' , path , <EOL> params = self . filter ( params ) ) <EOL> except exc . dRestRequestError as e : <EOL> msg = "<STR_LIT>" % ( e . msg , self . name , <EOL> resource_id ) <EOL> raise exc . dRestRequestError ( msg , e . response ) <EOL> return response <EOL> def create ( self , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> return self . post ( params ) <EOL> def post ( self , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> params = self . filter ( params ) <EOL> path = '<STR_LIT>' % self . path <EOL> try : <EOL> response = self . api . make_request ( '<STR_LIT:POST>' , path , self . filter ( params ) ) <EOL> except exc . dRestRequestError as e : <EOL> msg = "<STR_LIT>" % ( e . msg , self . name ) <EOL> raise exc . dRestRequestError ( msg , e . response ) <EOL> return response <EOL> def update ( self , resource_id , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> return self . put ( resource_id , params ) <EOL> def put ( self , resource_id , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> params = self . filter ( params ) <EOL> path = '<STR_LIT>' % ( self . path , resource_id ) <EOL> try : <EOL> response = self . api . make_request ( '<STR_LIT>' , path , params ) <EOL> except exc . dRestRequestError as e : <EOL> msg = "<STR_LIT>" % ( e . msg , self . name , <EOL> resource_id ) <EOL> raise exc . dRestRequestError ( msg , e . response ) <EOL> return response <EOL> def patch ( self , resource_id , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> params = self . filter ( params ) <EOL> path = '<STR_LIT>' % ( self . path , resource_id ) <EOL> try : <EOL> response = self . api . make_request ( '<STR_LIT>' , path , params ) <EOL> except exc . dRestRequestError as e : <EOL> msg = "<STR_LIT>" % ( e . msg , self . name , <EOL> resource_id ) <EOL> raise exc . dRestRequestError ( msg , e . response ) <EOL> return response <EOL> def delete ( self , resource_id , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> path = '<STR_LIT>' % ( self . path , resource_id ) <EOL> try : <EOL> response = self . api . make_request ( '<STR_LIT>' , path , params ) <EOL> except exc . dRestRequestError as e : <EOL> msg = "<STR_LIT>" % ( e . msg , self . name , <EOL> resource_id ) <EOL> raise exc . dRestRequestError ( msg , e . response ) <EOL> return response <EOL> class TastyPieResourceHandler ( RESTResourceHandler ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> request = request . TastyPieRequestHandler <EOL> """<STR_LIT>""" <EOL> collection_name = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api_obj , name , path , ** kw ) : <EOL> super ( TastyPieResourceHandler , self ) . __init__ ( api_obj , name , path , ** kw ) <EOL> self . _schema = None <EOL> def get_by_uri ( self , resource_uri , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> resource_uri = resource_uri . rstrip ( '<STR_LIT:/>' ) <EOL> pk = resource_uri . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> return self . get ( pk , params ) <EOL> def patch_list ( self , create_objects = [ ] , delete_objects = [ ] ) : <EOL> """<STR_LIT>""" <EOL> create_objects = [ self . filter ( o ) for o in create_objects ] <EOL> delete_objects = [ self . filter ( o ) for o in delete_objects ] <EOL> delete_collection_name = "<STR_LIT>" % self . _meta . collection_name <EOL> data = { <EOL> self . _meta . collection_name : create_objects , <EOL> delete_collection_name : delete_objects , <EOL> } <EOL> return self . api . make_request ( '<STR_LIT>' , self . path , data ) <EOL> @ property <EOL> def schema ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _schema : <EOL> response = self . api . make_request ( '<STR_LIT:GET>' , '<STR_LIT>' % self . path ) <EOL> self . _schema = response . data <EOL> return self . _schema <EOL> class NestedResource ( object ) : <EOL> pass </s>
