forked from KeithGalli/python-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook_Review_class.py
More file actions
141 lines (129 loc) · 4.9 KB
/
Book_Review_class.py
File metadata and controls
141 lines (129 loc) · 4.9 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
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from flasgger import Swagger
import book_review_module4
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)
br = book_review_module4.BookReview()
class AllReview(Resource):
def get(self):
"""
This method responds to the GET request for retrieving all book reviews.
---
tags:
- Book Reviews
parameters:
- name: sort
in: query
type: string
required: false
enum: ["ASC", "DESC"]
description: Sort reviews by rating in ascending or descending order (optional)
- name: max_records
in: query
type: integer
required: false
description: Maximum number of records to retrieve (optional)
responses:
200:
description: A successful GET request
content:
application/json:
schema:
type: array
items:
type: object
properties:
book_title:
type: string
description: The title of the book
book_rating:
type: number
description: The rating of the book
book_notes:
type: string
description: The notes of the book
"""
# Retrieve optional parameters from the query string
sort = request.args.get('sort',default=None)
max_records = int(request.args.get('max_records', default=10))
#Validate the sort parameter
if sort and sort not in ["ASC", "DESC"]:
return{"error": "Invalid value for 'sort' parameter"}, 400
# Sort the reviews if sort parameter is provided
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)
#delete below
#else:
# book_reviews_sorted = book_reviews
#(not needed handled in previous one) Limit the number of records if max_records parameter is provided
#if max_records:
# book_reviews_sorted = book_reviews_sorted[:int(max_records)]
return book_reviews, 200
class PostReview(Resource):
def post(self):
"""
This method responds to the GET request for retrieving all book reviews.
---
tags:
- Post Book Reviews
parameters:
- in: body
name: body
required: true
schema:
id: BookReview
required:
- book
- rating
properties:
book:
type: string
description: The title of the book
rating:
type: integer
description: Insert Book rating here (Required)
notes:
type: string
description: Insert notes here (Required)
# - name: book_title
# in: query
# type: string
# required: true
# description: Insert Book Title here (Required)
# - name: book_rating
# in: query
# type: integer
# required: true
# description: Insert Book Rating here, example 1.0 (Required)
# - name: notes
# in: query
# type: string
# required: false
# description: Notes about the book / summary (optional)
responses:
200:
description: A successful POST request
400:
description: Bad request, missing 'Book' or 'Rating' in the request body
"""
data = request.json()
if not data:
return{"error":"Request body must be in JSON format."}, 400
book = data.get("book")
rating = data.get("rating")
notes = data.get("notes","")
if not book or not rating:
return {"error":"Both 'book' and 'rating' are required fields."}, 400
br.add_book_ratings(book, rating, notes)
return{"message":"Book Review added successfully."},201
# Add the resource to the API
api.add_resource(AllReview, '/all_reviews')
api.add_resource(PostReview, '/post_reviews')
if __name__ == "__main__":
app.run(debug=True)