forked from KeithGalli/python-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
161 lines (145 loc) · 5.02 KB
/
app.py
File metadata and controls
161 lines (145 loc) · 5.02 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from flasgger import Swagger
import book
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)
br = book.BookReview()
class AllReviews(Resource):
def get(self):
"""
This method responds to the GET request for this endpoint and returns a list of book reviews.
---
tags:
- Book Reviews
parameters:
- name: sort
in: query
type: string
required: false
enum: ['ASC','DESC']
description: The parameter to sort the reviews by (e.g., 'rating', 'book')
- name: max_records
in: query
type: integer
required: false
description: The maximum number of records to return
responses:
200:
description: A successful GET request
content:
application/json:
schema:
type: array
items:
type: object
properties:
Books:
type: string
ISBN:
type: string
Rating:
type: number
Notes:
type: string
"""
sort = request.args.get("sort", default=None)
max_records = request.args.get("max_records", default=10, type=int)
if sort == "ASC":
book_reviews = br.get_book_ratings(sort=sort, max_records=max_records)
elif sort == "DESC":
book_reviews = br.get_book_ratings(sort=sort, max_records=max_records)
else:
book_reviews = br.get_book_ratings(max_records=max_records)
return book_reviews, 200
class PostReview(Resource):
def post(self):
"""
This method responds to the POST request for this endpoint and adds a new book review.
---
tags:
- Book Reviews
consumes:
- application/json
parameters:
- in: body
name: body
description: Book review data
required: true
schema:
id: BookReview
required:
- Book
- Rating
- ISBN
properties:
Book:
type: string
description: The name of the book
Rating:
type: number
description: The review text
ISBN:
type: string
description: Book Serial Number
Notes:
type: string
description: Additional notes (optional)
responses:
201:
description: Review added successfully
"""
data = request.get_json()
book = data.get("Book")
isbn = data.get("ISBN")
rating = data.get("Rating")
notes = data.get("Notes", None)
br.add_book_rating(book_title=book, rating=rating, isbn=isbn, notes=notes)
# Here you would add logic to insert the review data into your database
return {"message": "Review added successfully"}, 201
class UpdateReview(Resource):
def put(self):
"""
This method responds to the PUT request for this endpoint and updates a book review.
---
tags:
- Book Reviews
consumes:
- application/json
parameters:
- in: body
name: body
description: Data to update the book review
required: true
schema:
id: UpdateBookReview
required:
- ISBN
- Rating
properties:
ISBN:
type: string
description: Book Serial Number
Rating:
type: number
description: Updated rating for the book
Notes:
type: string
description: Optional updated notes for the book
responses:
200:
description: Review updated successfully
"""
data = request.get_json()
isbn = data.get("ISBN")
rating = data.get("Rating")
notes = data.get("Notes", None) # 'notes' is optional
# Here you would add logic to update the review data in your database
br.update_book_rating(isbn, rating, notes)
return {"message": "Review updated successfully"}, 200
api.add_resource(UpdateReview, "/update_review")
api.add_resource(PostReview, "/post_review")
api.add_resource(AllReviews, "/all_reviews")
if __name__ == "__main__":
app.run(debug=True)