<s> from flask import Flask , Blueprint <EOL> from flask . ext . restplus import Api , Resource , fields <EOL> api_v1 = Blueprint ( '<STR_LIT>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> api = Api ( api_v1 , version = '<STR_LIT:1.0>' , title = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> ) <EOL> ns = api . namespace ( '<STR_LIT>' , description = '<STR_LIT>' ) <EOL> TODOS = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> } <EOL> todo = api . model ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : fields . String ( required = True , description = '<STR_LIT>' ) <EOL> } ) <EOL> listed_todo = api . model ( '<STR_LIT>' , { <EOL> '<STR_LIT:id>' : fields . String ( required = True , description = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : fields . Nested ( todo , description = '<STR_LIT>' ) <EOL> } ) <EOL> def abort_if_todo_doesnt_exist ( todo_id ) : <EOL> if todo_id not in TODOS : <EOL> api . abort ( <NUM_LIT> , "<STR_LIT>" . format ( todo_id ) ) <EOL> parser = api . parser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' , location = '<STR_LIT>' ) <EOL> @ ns . route ( '<STR_LIT>' ) <EOL> @ api . doc ( responses = { <NUM_LIT> : '<STR_LIT>' } , params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class Todo ( Resource ) : <EOL> '''<STR_LIT>''' <EOL> @ api . doc ( description = '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( TODOS . keys ( ) ) ) ) <EOL> @ api . marshal_with ( todo ) <EOL> def get ( self , todo_id ) : <EOL> '''<STR_LIT>''' <EOL> abort_if_todo_doesnt_exist ( todo_id ) <EOL> return TODOS [ todo_id ] <EOL> @ api . doc ( responses = { <NUM_LIT> : '<STR_LIT>' } ) <EOL> def delete ( self , todo_id ) : <EOL> '''<STR_LIT>''' <EOL> abort_if_todo_doesnt_exist ( todo_id ) <EOL> del TODOS [ todo_id ] <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> @ api . doc ( parser = parser ) <EOL> @ api . marshal_with ( todo ) <EOL> def put ( self , todo_id ) : <EOL> '''<STR_LIT>''' <EOL> args = parser . parse_args ( ) <EOL> task = { '<STR_LIT>' : args [ '<STR_LIT>' ] } <EOL> TODOS [ todo_id ] = task <EOL> return task <EOL> @ ns . route ( '<STR_LIT:/>' ) <EOL> class TodoList ( Resource ) : <EOL> '''<STR_LIT>''' <EOL> @ api . marshal_list_with ( listed_todo ) <EOL> def get ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ { '<STR_LIT:id>' : id , '<STR_LIT>' : todo } for id , todo in TODOS . items ( ) ] <EOL> @ api . doc ( parser = parser ) <EOL> @ api . marshal_with ( todo , code = <NUM_LIT> ) <EOL> def post ( self ) : <EOL> '''<STR_LIT>''' <EOL> args = parser . parse_args ( ) <EOL> todo_id = '<STR_LIT>' % ( len ( TODOS ) + <NUM_LIT:1> ) <EOL> TODOS [ todo_id ] = { '<STR_LIT>' : args [ '<STR_LIT>' ] } <EOL> return TODOS [ todo_id ] , <NUM_LIT> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( api_v1 ) <EOL> app . run ( debug = True ) </s>
