<s> from tapioca . spec import APISpecification , Resource , Path , Method , Param <EOL> class Metadata ( object ) : <EOL> def __init__ ( self , version = None , base_url = None ) : <EOL> self . spec = APISpecification ( version = version , base_url = base_url ) <EOL> def add ( self , path , handler ) : <EOL> resource = Resource ( path ) <EOL> basic_methods = list ( self . get_basic_methods ( handler ) ) <EOL> if basic_methods : <EOL> resource . add_path ( <EOL> Path ( '<STR_LIT>' . format ( path ) , methods = basic_methods ) ) <EOL> resource . add_path ( <EOL> Path ( '<STR_LIT>' . format ( path ) , <EOL> params = [ Param ( '<STR_LIT:type>' , style = '<STR_LIT:url>' ) ] , <EOL> methods = basic_methods ) ) <EOL> instance_methods = list ( self . get_instance_methods ( handler ) ) <EOL> if instance_methods : <EOL> resource . add_path ( <EOL> Path ( '<STR_LIT>' . format ( path ) , <EOL> params = [ Param ( '<STR_LIT:key>' , style = '<STR_LIT:url>' ) ] , <EOL> methods = instance_methods ) ) <EOL> resource . add_path ( <EOL> Path ( '<STR_LIT>' . format ( path ) , <EOL> params = [ <EOL> Param ( '<STR_LIT:key>' , style = '<STR_LIT:url>' ) , <EOL> Param ( '<STR_LIT:type>' , style = '<STR_LIT:url>' ) <EOL> ] , <EOL> methods = instance_methods ) ) <EOL> self . spec . add_resource ( resource ) <EOL> def get_basic_methods ( self , handler ) : <EOL> return self . introspect_methods ( <EOL> GET = handler . get_collection , <EOL> POST = handler . create_model ) <EOL> def is_overridden ( self , method ) : <EOL> return not hasattr ( method , '<STR_LIT>' ) <EOL> def get_instance_methods ( self , handler ) : <EOL> return self . introspect_methods ( <EOL> GET = handler . get_model , <EOL> PUT = handler . update_model , <EOL> DELETE = handler . delete_model ) <EOL> def introspect_methods ( self , ** mapping ) : <EOL> for method_type , implementation in mapping . items ( ) : <EOL> if self . is_overridden ( implementation ) : <EOL> params = self . introspect_params ( implementation ) <EOL> yield Method ( method_type , <EOL> params = params , description = implementation . __doc__ ) <EOL> def introspect_params ( self , method ) : <EOL> params = [ ] <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> request_schema = method . request_schema <EOL> if hasattr ( request_schema , '<STR_LIT>' ) : <EOL> for param in request_schema . querystring_params ( ) : <EOL> params . append ( <EOL> Param ( <EOL> param . name , <EOL> required = not param . is_optional , <EOL> style = '<STR_LIT>' , <EOL> description = param . description <EOL> ) <EOL> ) <EOL> return params </s>
