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
199 lines (180 loc) · 6.92 KB
/
app.py
File metadata and controls
199 lines (180 loc) · 6.92 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
class StringGenerator(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: message
in: query
type: string
required: true
description: The text to be converted to uppercase dude!
- name: duplication_factor
in: query
type: integer
required: false
description: number of times to duplicates the text (default is 1)
- name: capitalization
in: query
type: string
required: false
enum: [UPPER, LOWER]
description: Capitalization style UPPER, LOWER, or None (default)
responses:
200:
description: A successful GET request
content:
application/json:
schema:
type: object
properties:
generated_text:
type: string
description: The generated text
"""
args = request.args
message = args["message"]
duplication_factor = int(args.get("duplication_factor",1))
capitalization = args.get("capitalization", None)
if capitalization == "UPPER":
message = message.upper()
elif capitalization == "LOWER":
message = message.lower()
generated_text = (message + " ") * duplication_factor
return {"generated_text": generated_text}, 200
api.add_resource(AddRecord, "/add-record")
api.add_resource(Records, "/records")
api.add_resource(UppercaseText, "/uppercase")
api.add_resource(StringGenerator,"/generator")
if __name__ == "__main__":
app.run(debug=True)
#
#In this code, we have a Flask application with a RESTful API. The application has four endpoints: `/generate`, `/uppercase`, `/records`, and `/add-record`. Each endpoint corresponds to a different resource in the `resources` module.
#
#The `StringGenerator` resource handles the `/generate` endpoint. It takes a message and optional parameters for duplication factor and capitalization style. The message is then processed according to the provided parameters and returned as a generated text.
#
#The `UppercaseText` resource handles the `/uppercase` endpoint. It takes a text and converts it to uppercase.
#
#The `Records` resource handles the `/records` endpoint. It retrieves a specified number of books from the database and returns them as a list of dictionaries.
#
#The `AddRecord` resource handles the `/add-record` endpoint. It takes a JSON object containing a book and a rating, and adds a new record to the database.
#
#The `flasgger` module is used