from flask import Flask
from flask_restful import Resource, Api
app =Flask(__name__)
api =Api(app)
classHelloWorld(Resource):
defget(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ =='__main__':
app.run(debug=True)
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app =Flask(__name__)
api =Api(app)
TODOS= {
'todo1': {'task': 'build an API'},
'todo2': {'task': '?????'},
'todo3': {'task': 'profit!'},
}
defabort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
abort(404, message="Todo {} doesn't exist".format(todo_id))
parser = reqparse.RequestParser()
parser.add_argument('task')
# Todo# shows a single todo item and lets you delete a todo itemclassTodo(Resource):
defget(self, todo_id):
abort_if_todo_doesnt_exist(todo_id)
returnTODOS[todo_id]
defdelete(self, todo_id):
abort_if_todo_doesnt_exist(todo_id)
del TODOS[todo_id]
return'', 204defput(self, todo_id):
args = parser.parse_args()
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201# TodoList# shows a list of all todos, and lets you POST to add new tasksclassTodoList(Resource):
defget(self):
returnTODOSdefpost(self):
args = parser.parse_args()
todo_id =int(max(TODOS.keys()).lstrip('todo')) +1
todo_id ='todo%i' % todo_id
TODOS[todo_id] = {'task': args['task']}
returnTODOS[todo_id], 201#### Actually setup the Api resource routing here##
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<todo_id>')
if __name__ =='__main__':
app.run(debug=True)
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email