<s> class Mapping ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mapping , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> filter_args = { k : mapping [ k ] for k in mapping if k not in dir ( self ) } <EOL> self . __dict__ = self <EOL> dict . __init__ ( self , filter_args , * args , ** kwargs ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( { k : self . __dict__ [ k ] for k in self . __dict__ if not k . startswith ( "<STR_LIT:_>" ) } ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , hex ( id ( self ) ) , str ( self ) ) <EOL> class ApiResource ( Mapping ) : <EOL> resource_name = "<STR_LIT>" <EOL> @ classmethod <EOL> def _create_object ( cls , response , connection = None ) : <EOL> if isinstance ( response , list ) : <EOL> return [ cls . _create_object ( obj , connection ) for obj in response ] <EOL> else : <EOL> return cls ( response , _connection = connection ) <EOL> @ classmethod <EOL> def _make_request ( cls , method , url , connection , data = None , params = { } , headers = { } ) : <EOL> return connection . make_request ( method , url , data , params , headers ) <EOL> @ classmethod <EOL> def _get_path ( cls , id ) : <EOL> return "<STR_LIT>" % ( cls . resource_name , id ) <EOL> @ classmethod <EOL> def get ( cls , id , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:GET>' , cls . _get_path ( id ) , connection , params = params ) <EOL> return cls . _create_object ( response , connection = connection ) <EOL> class ApiSubResource ( ApiResource ) : <EOL> parent_resource = "<STR_LIT>" <EOL> parent_key = "<STR_LIT>" <EOL> @ classmethod <EOL> def _get_path ( cls , id , parentid ) : <EOL> return "<STR_LIT>" % ( cls . parent_resource , parentid , cls . resource_name , id ) <EOL> @ classmethod <EOL> def get ( cls , parentid , id , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:GET>' , cls . _get_path ( id , parentid ) , connection , params = params ) <EOL> return cls . _create_object ( response , connection = connection ) <EOL> def parent_id ( self ) : <EOL> return self [ self . parent_key ] <EOL> class CreateableApiResource ( ApiResource ) : <EOL> @ classmethod <EOL> def _create_path ( cls ) : <EOL> return cls . resource_name <EOL> @ classmethod <EOL> def create ( cls , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:POST>' , cls . _create_path ( ) , connection , data = params ) <EOL> return cls . _create_object ( response , connection = connection ) <EOL> class CreateableApiSubResource ( ApiSubResource ) : <EOL> @ classmethod <EOL> def _create_path ( cls , parentid ) : <EOL> return "<STR_LIT>" % ( cls . parent_resource , parentid , cls . resource_name ) <EOL> @ classmethod <EOL> def create ( cls , parentid , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:POST>' , cls . _create_path ( parentid ) , connection , data = params ) <EOL> return cls . _create_object ( response , connection = connection ) <EOL> class ListableApiResource ( ApiResource ) : <EOL> @ classmethod <EOL> def _get_all_path ( cls ) : <EOL> return cls . resource_name <EOL> @ classmethod <EOL> def all ( cls , connection = None , ** params ) : <EOL> request = cls . _make_request ( '<STR_LIT:GET>' , cls . _get_all_path ( ) , connection , params = params ) <EOL> return cls . _create_object ( request , connection = connection ) <EOL> class ListableApiSubResource ( ApiSubResource ) : <EOL> @ classmethod <EOL> def _get_all_path ( cls , parentid ) : <EOL> return "<STR_LIT>" % ( cls . parent_resource , parentid , cls . resource_name ) <EOL> @ classmethod <EOL> def all ( cls , parentid , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:GET>' , cls . _get_all_path ( parentid ) , connection , params = params ) <EOL> return cls . _create_object ( response , connection = connection ) <EOL> class UpdateableApiResource ( ApiResource ) : <EOL> def _update_path ( self ) : <EOL> return "<STR_LIT>" % ( self . resource_name , self . id ) <EOL> def update ( self , ** updates ) : <EOL> response = self . _make_request ( '<STR_LIT>' , self . _update_path ( ) , self . _connection , data = updates ) <EOL> return self . _create_object ( response , connection = self . _connection ) <EOL> class UpdateableApiSubResource ( ApiSubResource ) : <EOL> def _update_path ( self ) : <EOL> return "<STR_LIT>" % ( self . parent_resource , self . parent_id ( ) , self . resource_name , self . id ) <EOL> def update ( self , ** updates ) : <EOL> response = self . _make_request ( '<STR_LIT>' , self . _update_path ( ) , self . _connection , data = updates ) <EOL> return self . _create_object ( response , connection = self . _connection ) <EOL> class DeleteableApiResource ( ApiResource ) : <EOL> def _delete_path ( self ) : <EOL> return "<STR_LIT>" % ( self . resource_name , self . id ) <EOL> def delete ( self ) : <EOL> return self . _make_request ( '<STR_LIT>' , self . _delete_path ( ) , self . _connection ) <EOL> class DeleteableApiSubResource ( ApiSubResource ) : <EOL> def _delete_path ( self ) : <EOL> return "<STR_LIT>" % ( self . parent_resource , self . parent_id ( ) , self . resource_name , self . id ) <EOL> def delete ( self ) : <EOL> return self . _make_request ( '<STR_LIT>' , self . _delete_path ( ) , self . _connection ) <EOL> class CollectionDeleteableApiResource ( ApiResource ) : <EOL> @ classmethod <EOL> def _delete_all_path ( cls ) : <EOL> return cls . resource_name <EOL> @ classmethod <EOL> def delete_all ( cls , connection = None ) : <EOL> return cls . _make_request ( '<STR_LIT>' , cls . _delete_all_path ( ) , connection ) <EOL> class CollectionDeleteableApiSubResource ( ApiSubResource ) : <EOL> @ classmethod <EOL> def _delete_all_path ( cls , parentid ) : <EOL> return "<STR_LIT>" % ( cls . parent_resource , parentid , cls . resource_name ) <EOL> @ classmethod <EOL> def delete_all ( cls , parentid , connection = None ) : <EOL> return cls . _make_request ( '<STR_LIT>' , cls . _delete_all_path ( parentid ) , connection ) <EOL> class CountableApiResource ( ApiResource ) : <EOL> @ classmethod <EOL> def _count_path ( cls ) : <EOL> return "<STR_LIT>" % ( cls . resource_name ) <EOL> @ classmethod <EOL> def count ( cls , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:GET>' , cls . _count_path ( ) , connection , params = params ) <EOL> return response [ '<STR_LIT:count>' ] <EOL> class CountableApiSubResource ( ApiSubResource ) : <EOL> count_resource = None <EOL> @ classmethod <EOL> def _count_path ( cls , parentid = None ) : <EOL> if parentid is not None : <EOL> return "<STR_LIT>" % ( cls . parent_resource , parentid , cls . resource_name ) <EOL> elif cls . count_resource is not None : <EOL> return "<STR_LIT>" % ( cls . count_resource ) <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def count ( cls , parentid = None , connection = None , ** params ) : <EOL> response = cls . _make_request ( '<STR_LIT:GET>' , cls . _count_path ( parentid ) , connection , params = params ) <EOL> return response [ '<STR_LIT:count>' ] </s>
