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
137 lines (121 loc) · 4.21 KB
/
app.py
File metadata and controls
137 lines (121 loc) · 4.21 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
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from flasgger import Swagger
import book_review
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)
class UppercaseText(Resource):
def get(self):
"""
This method responds to the GET request for this endpoint and returns the data in uppercase.
---
tags:
- Text Processing
parameters:
- name: text
in: query
type: string
required: true
description: The text to be converted to uppercase
responses:
200:
description: A successful GET request
content:
application/json:
schema:
type: object
properties:
text:
type: string
description: The text in uppercase
"""
text = request.args.get('text')
return jsonify({"text": text.upper()})
class Records(Resource):
def get(self):
"""
This method responds to the GET request for returning a number of books.
---
tags:
- Records
parameters:
- name: count
in: query
type: integer
required: false
description: The number of books to return
- name: sort
in: query
type: string
enum: ['ASC', 'DESC']
required: false
description: Sort order for the books
responses:
200:
description: A successful GET request
schema:
type: object
properties:
books:
type: array
items:
type: object
properties:
title:
type: string
description: The title of the book
author:
type: string
description: The author of the book
"""
count = request.args.get('count') # Default to returning 10 books if count is not provided
sort = request.args.get('sort')
# Get all the books
books = book_review.get_all_records(count=count, sort=sort)
return {"books": books}, 200
class AddRecord(Resource):
def post(self):
"""
This method responds to the POST request for adding a new record to the DB table.
---
tags:
- Records
parameters:
- in: body
name: body
required: true
schema:
id: BookReview
required:
- Book
- Rating
properties:
Book:
type: string
description: the name of the book
Rating:
type: integer
description: the rating of the book (1-10)
responses:
200:
description: A successful POST request
400:
description: Bad request, missing 'Book' or 'Rating' in the request body
"""
data = request.json
print(data)
# Check if 'Book' and 'Rating' are present in the request body
if 'Book' not in data or 'Rating' not in data:
return {"message": "Bad request, missing 'Book' or 'Rating' in the request body"}, 400
# Call the add_record function to add the record to the DB table
success = book_review.add_record(data)
if success:
return {"message": "Record added successfully"}, 200
else:
return {"message": "Failed to add record"}, 500
api.add_resource(AddRecord, "/add-record")
api.add_resource(Records, "/records")
api.add_resource(UppercaseText, "/uppercase")
if __name__ == "__main__":
app.run(debug=True)