-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathtodo_blueprint.py
96 lines (75 loc) · 2.49 KB
/
todo_blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from flask import Flask, Blueprint
from flask_restx import Api, Resource, fields
api_v1 = Blueprint("api", __name__, url_prefix="/api/1")
api = Api(
api_v1,
version="1.0",
title="Todo API",
description="A simple TODO API",
)
ns = api.namespace("todos", description="TODO operations")
TODOS = {
"todo1": {"task": "build an API"},
"todo2": {"task": "?????"},
"todo3": {"task": "profit!"},
}
todo = api.model(
"Todo", {"task": fields.String(required=True, description="The task details")}
)
listed_todo = api.model(
"ListedTodo",
{
"id": fields.String(required=True, description="The todo ID"),
"todo": fields.Nested(todo, description="The Todo"),
},
)
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
api.abort(404, "Todo {} doesn't exist".format(todo_id))
parser = api.parser()
parser.add_argument(
"task", type=str, required=True, help="The task details", location="form"
)
@ns.route("/<string:todo_id>")
@api.doc(responses={404: "Todo not found"}, params={"todo_id": "The Todo ID"})
class Todo(Resource):
"""Show a single todo item and lets you delete them"""
@api.doc(description="todo_id should be in {0}".format(", ".join(TODOS.keys())))
@api.marshal_with(todo)
def get(self, todo_id):
"""Fetch a given resource"""
abort_if_todo_doesnt_exist(todo_id)
return TODOS[todo_id]
@api.doc(responses={204: "Todo deleted"})
def delete(self, todo_id):
"""Delete a given resource"""
abort_if_todo_doesnt_exist(todo_id)
del TODOS[todo_id]
return "", 204
@api.doc(parser=parser)
@api.marshal_with(todo)
def put(self, todo_id):
"""Update a given resource"""
args = parser.parse_args()
task = {"task": args["task"]}
TODOS[todo_id] = task
return task
@ns.route("/")
class TodoList(Resource):
"""Shows a list of all todos, and lets you POST to add new tasks"""
@api.marshal_list_with(listed_todo)
def get(self):
"""List all todos"""
return [{"id": id, "todo": todo} for id, todo in TODOS.items()]
@api.doc(parser=parser)
@api.marshal_with(todo, code=201)
def post(self):
"""Create a todo"""
args = parser.parse_args()
todo_id = "todo%d" % (len(TODOS) + 1)
TODOS[todo_id] = {"task": args["task"]}
return TODOS[todo_id], 201
if __name__ == "__main__":
app = Flask(__name__)
app.register_blueprint(api_v1)
app.run(debug=True)