-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
42 lines (33 loc) · 1.27 KB
/
Copy pathapi.py
File metadata and controls
42 lines (33 loc) · 1.27 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
import sqlite3
from flask import jsonify, Blueprint
# I created the route for the API in a separate file for clarity
api_blueprint = Blueprint('api', __name__)
@api_blueprint.route('/bookings_API')
def bookings_api():
con = sqlite3.connect('bookings.db')
cur = con.cursor()
cur.execute("SELECT * FROM bookings")
bookings = cur.fetchall()
con.close()
# extracted from https://stackoverflow.com/questions/66367271/displaying-a-list-of-data-from-in-flask-rest-api-python
# and from the serendipity chapter in the projectBook.pdf
# empty list to collect the list of dictionaries
data_list = []
# a loop to extract each field from each entry
for booking in bookings:
data = {
"_id" : booking[0],
"name" : booking[1],
"email" : booking[2],
"date" : booking[3],
"time" : booking[4],
"table" : booking[5]
}
# add the results to the empty list
data_list.append(data)
# convert the result into a json response with josonify
return jsonify(data_list), 200
# to check the API is working:
# http://127.0.0.1:5000/bookings_API on http
# https://127.0.0.1:5000/bookings_API on https
# making sure the port also matches