-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
50 lines (33 loc) · 1.41 KB
/
api.py
File metadata and controls
50 lines (33 loc) · 1.41 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
import random
from time import sleep
from util import coerce_products
from flask import (jsonify,
make_response)
def register_apis(app, db_connection):
@app.route("/api/get_top_sellers/")
def get_top_sellers():
# sleep(2)
with db_connection.cursor() as cursor:
query_string = "SELECT * FROM products"
cursor.execute(query_string)
all_products = cursor.fetchall()
all_products_length = len(all_products)
random_sample = random.sample(range(0, all_products_length), min(3, all_products_length))
top_sellers = []
for i, product in enumerate(all_products):
if i in random_sample:
coerce_products(product)
product["price"] = float(product["price"])
top_sellers.append(product)
return jsonify(top_sellers)
@app.route("/api/get_product_by_id/<int:id_>")
def get_product_by_id(id_):
with db_connection.cursor() as cursor:
query_string = "SELECT * FROM products WHERE products.id = {}".format(id_)
cursor.execute(query_string)
product = cursor.fetchone()
if product is not None:
coerce_products(product)
return jsonify(product)
else:
return make_response("Product with id {} not found".format(id_), 404)