-
Notifications
You must be signed in to change notification settings - Fork 327
/
cat.py
38 lines (31 loc) · 876 Bytes
/
cat.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
from flask_restx import Namespace, Resource, fields
api = Namespace("cats", description="Cats related operations")
cat = api.model(
"Cat",
{
"id": fields.String(required=True, description="The cat identifier"),
"name": fields.String(required=True, description="The cat name"),
},
)
CATS = [
{"id": "felix", "name": "Felix"},
]
@api.route("/")
class CatList(Resource):
@api.doc("list_cats")
@api.marshal_list_with(cat)
def get(self):
"""List all cats"""
return CATS
@api.route("/<id>")
@api.param("id", "The cat identifier")
@api.response(404, "Cat not found")
class Cat(Resource):
@api.doc("get_cat")
@api.marshal_with(cat)
def get(self, id):
"""Fetch a cat given its identifier"""
for cat in CATS:
if cat["id"] == id:
return cat
api.abort(404)