Skip to content

Commit 19876d4

Browse files
committed
Added base files for book review API
1 parent 634522b commit 19876d4

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

app.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
from flask import Flask, jsonify, request
2+
from flask_restful import Api, Resource
3+
from flasgger import Swagger
4+
5+
import book_review
6+
7+
app = Flask(__name__)
8+
api = Api(app)
9+
swagger = Swagger(app)
10+
11+
class UppercaseText(Resource):
12+
13+
def get(self):
14+
"""
15+
This method responds to the GET request for this endpoint and returns the data in uppercase.
16+
---
17+
tags:
18+
- Text Processing
19+
parameters:
20+
- name: text
21+
in: query
22+
type: string
23+
required: true
24+
description: The text to be converted to uppercase
25+
responses:
26+
200:
27+
description: A successful GET request
28+
content:
29+
application/json:
30+
schema:
31+
type: object
32+
properties:
33+
text:
34+
type: string
35+
description: The text in uppercase
36+
"""
37+
text = request.args.get('text')
38+
39+
return jsonify({"text": text.upper()})
40+
41+
class Records(Resource):
42+
43+
def get(self):
44+
"""
45+
This method responds to the GET request for returning a number of books.
46+
---
47+
tags:
48+
- Records
49+
parameters:
50+
- name: count
51+
in: query
52+
type: integer
53+
required: false
54+
description: The number of books to return
55+
- name: sort
56+
in: query
57+
type: string
58+
enum: ['ASC', 'DESC']
59+
required: false
60+
description: Sort order for the books
61+
responses:
62+
200:
63+
description: A successful GET request
64+
content:
65+
application/json:
66+
schema:
67+
type: object
68+
properties:
69+
books:
70+
type: array
71+
items:
72+
type: object
73+
properties:
74+
title:
75+
type: string
76+
description: The title of the book
77+
author:
78+
type: string
79+
description: The author of the book
80+
"""
81+
82+
count = request.args.get('count') # Default to returning 10 books if count is not provided
83+
sort = request.args.get('sort')
84+
85+
# Get all the books
86+
books = book_review.get_all_records(count=count, sort=sort)
87+
88+
return jsonify({"books": books})
89+
90+
class AddRecord(Resource):
91+
92+
def post(self):
93+
"""
94+
This method responds to the POST request for adding a new record to the DB table.
95+
---
96+
tags:
97+
- Records
98+
parameters:
99+
- in: body
100+
name: body
101+
required: true
102+
schema:
103+
id: BookReview
104+
required:
105+
- Book
106+
- Rating
107+
properties:
108+
Book:
109+
type: string
110+
description: the name of the book
111+
Rating:
112+
type: integer
113+
description: the rating of the book (1-10)
114+
responses:
115+
200:
116+
description: A successful POST request
117+
content:
118+
application/json:
119+
schema:
120+
type: object
121+
properties:
122+
message:
123+
type: string
124+
"""
125+
126+
data = request.json
127+
print(data)
128+
129+
# Check if 'Book' and 'Rating' are present in the request body
130+
if 'Book' not in data or 'Rating' not in data:
131+
return jsonify({"message": "Bad request, missing 'Book' or 'Rating' in the request body"})
132+
# Call the add_record function to add the record to the DB table
133+
success = book_review.add_record(data)
134+
135+
if success:
136+
return jsonify({"message": "Record added successfully"})
137+
else:
138+
return jsonify({"message": "Failed to add record"})
139+
140+
141+
142+
api.add_resource(AddRecord, "/add-record")
143+
api.add_resource(Records, "/records")
144+
api.add_resource(UppercaseText, "/uppercase")
145+
146+
if __name__ == "__main__":
147+
app.run(debug=True)

book_review.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
from pyairtable import Api
3+
4+
API_TOKEN = os.environ.get('AIRTABLE_TOKEN')
5+
6+
BASE_ID = 'appi1uzlLKn1TEKSw'
7+
TABLE_ID = 'tblvMMAVHo901m2Ra'
8+
9+
api = Api(API_TOKEN)
10+
11+
table = api.table(BASE_ID, TABLE_ID)
12+
13+
def get_all_records(count=None, sort=None):
14+
sort_param = []
15+
if sort and sort.upper()=='DESC':
16+
sort_param = ['-Rating']
17+
elif sort and sort.upper()=='ASC':
18+
sort_param = ['Rating']
19+
20+
return table.all(max_records=count, sort=sort_param)
21+
22+
def get_record_id(name):
23+
return table.first(formula=f"Book='{name}'")['id']
24+
25+
def update_record(record_id, data):
26+
table.update(record_id, data)
27+
28+
return True
29+
30+
def add_record(data):
31+
# require data contains a "Book" key and a "Rating" key (data is a dict)
32+
if 'Book' not in data or 'Rating' not in data:
33+
return False
34+
35+
table.create(data)
36+
return True
37+
38+
if __name__ == '__main__':
39+
## Show getting certain records
40+
print("Show getting certain records")
41+
print(table.all(formula="Rating < 5", sort=['-Rating']))
42+
43+
## Show getting a single record
44+
print("Show getting a single record")
45+
46+
# Replace a record
47+
print("Replace a record")
48+
name = "Test Message"
49+
record_id = table.first(formula=f"Book='{name}'")['id']
50+
table.update(record_id, {"Rating": 5})
51+
52+
## Show all records
53+
print("All records!")
54+
print(table.all())

requirements.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)