forked from SwarnimWalavalkar/rest-api-microservice-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
119 lines (93 loc) · 3.45 KB
/
api.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask
from flask_restful import Resource, Api, reqparse, abort, marshal, fields
# Initialize Flask
app = Flask(__name__)
api = Api(app)
# A List of Dicts to store all of the books
books = [{
"id": 1,
"title": "Zero to One",
"author": "Peter Thiel",
"length": 195,
"rating": 4.17
},
{
"id": 2,
"title": "Atomic Habits ",
"author": "James Clear",
"length": 319,
"rating": 4.35
}
]
# Schema For the Book Request JSON
bookFields = {
"id": fields.Integer,
"title": fields.String,
"author": fields.String,
"length": fields.Integer,
"rating": fields.Float
}
# Resource: Individual Book Routes
class Book(Resource):
def __init__(self):
# Initialize The Flsak Request Parser and add arguments as in an expected request
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument("title", type=str, location="json")
self.reqparse.add_argument("author", type=str, location="json")
self.reqparse.add_argument("length", type=int, location="json")
self.reqparse.add_argument("rating", type=float, location="json")
super(Book, self).__init__()
# GET - Returns a single book object given a matching id
def get(self, id):
book = [book for book in books if book['id'] == id]
if(len(book) == 0):
abort(404)
return{"book": marshal(book[0], bookFields)}
# PUT - Given an id
def put(self, id):
book = [book for book in books if book['id'] == id]
if len(book) == 0:
abort(404)
book = book[0]
# Loop Through all the passed agruments
args = self.reqparse.parse_args()
for k, v in args.items():
# Check if the passed value is not null
if v is not None:
# if not, set the element in the books dict with the 'k' object to the value provided in the request.
book[k] = v
return{"book": marshal(book, bookFields)}
def delete(self, id):
book = [book for book in books if book['id'] == id]
if(len(book) == 0):
abort(404)
books.remove(book[0])
return 201
class BookList(Resource):
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument(
"title", type=str, required=True, help="The title of the book must be provided", location="json")
self.reqparse.add_argument(
"author", type=str, required=True, help="The author of the book must be provided", location="json")
self.reqparse.add_argument("length", type=int, required=True,
help="The length of the book (in pages)", location="json")
self.reqparse.add_argument(
"rating", type=float, required=True, help="The rating must be provided", location="json")
def get(self):
return{"books": [marshal(book, bookFields) for book in books]}
def post(self):
args = self.reqparse.parse_args()
book = {
"id": books[-1]['id'] + 1 if len(books) > 0 else 1,
"title": args["title"],
"author": args["author"],
"length": args["length"],
"rating": args["rating"]
}
books.append(book)
return{"book": marshal(book, bookFields)}, 201
api.add_resource(BookList, "/books")
api.add_resource(Book, "/books/<int:id>")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)