|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
import os |
|
3
|
|
|
from random import uniform as random_uniform |
|
4
|
|
|
from time import sleep |
|
5
|
|
|
|
|
6
|
|
|
from flask import Flask, request, jsonify, json, abort |
|
7
|
|
|
|
|
8
|
|
|
app = Flask(__name__) |
|
9
|
|
|
|
|
10
|
|
|
# Ugly global object but injecting it into the Flask app would require extra |
|
11
|
|
|
# work that isn't worth it for the purpose of this tiny piece of the demo. |
|
12
|
|
|
# Besides, dependency injection has already been demonstrated on the main |
|
13
|
|
|
# server. |
|
14
|
|
|
data = {} |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def load_data(): |
|
18
|
|
|
path = os.path.realpath(os.path.dirname(__file__)) |
|
19
|
|
|
path = os.path.join(path, 'data.json') |
|
20
|
|
|
with open(path) as fp: |
|
21
|
|
|
result = json.load(fp) |
|
22
|
|
|
|
|
23
|
|
|
return result |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def get_all_items_by_key(key): |
|
27
|
|
|
""" DRY function to get a full list of items given that list's key. """ |
|
28
|
|
|
skip = int(request.args.get('skip', 0)) |
|
29
|
|
|
limit = int(request.args.get('limit', 10)) |
|
30
|
|
|
return data[key][skip:skip+limit-1] |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def get_items_by_filter(data_key, comparison_key, comparison_data): |
|
34
|
|
|
""" DRY function to filter items from a list. """ |
|
35
|
|
|
return list(filter(lambda x: x[comparison_key] == comparison_data, |
|
36
|
|
|
data[data_key])) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def get_purchases(purchases): |
|
40
|
|
|
""" Generic DRY function to respond to requests for purchases. """ |
|
41
|
|
|
skip = int(request.args.get('skip', 0)) |
|
42
|
|
|
limit = int(request.args.get('limit', 10)) |
|
43
|
|
|
|
|
44
|
|
|
if len(purchases) > 0: |
|
45
|
|
|
return jsonify({'purchases': purchases[skip:skip + limit - 1]}) |
|
46
|
|
|
else: |
|
47
|
|
|
abort(404) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
@app.route('/api/products') |
|
51
|
|
|
def products(): |
|
52
|
|
|
return jsonify({'products': get_all_items_by_key('products')}) |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
@app.route('/api/products/<int:product_id>') |
|
56
|
|
|
def product(product_id): |
|
57
|
|
|
p = get_items_by_filter('products', 'id', product_id) |
|
58
|
|
|
|
|
59
|
|
|
if len(p) > 0: |
|
60
|
|
|
return jsonify({'product': p[0]}) |
|
61
|
|
|
else: |
|
62
|
|
|
abort(404) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
@app.route('/api/users') |
|
66
|
|
|
def users(): |
|
67
|
|
|
return jsonify({'users': get_all_items_by_key('users')}) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
@app.route('/api/users/<username>') |
|
71
|
|
|
def user(username): |
|
72
|
|
|
u = get_items_by_filter('users', 'username', username) |
|
73
|
|
|
if len(u) > 0: |
|
74
|
|
|
return jsonify({'user': u[0]}) |
|
75
|
|
|
else: |
|
76
|
|
|
abort(404) |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
@app.route('/api/purchases/by_user/<username>') |
|
80
|
|
|
def purchases_by_user(username): |
|
81
|
|
|
p = get_items_by_filter('purchases', 'username', username) |
|
82
|
|
|
return get_purchases(p) |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
@app.route('/api/purchases/by_product/<int:product_id>') |
|
86
|
|
|
def purchases_by_product(product_id): |
|
87
|
|
|
p = get_items_by_filter('purchases', 'product_id', product_id) |
|
88
|
|
|
return get_purchases(p) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
if __name__ == "__main__": |
|
92
|
|
|
data = load_data() |
|
93
|
|
|
app.before_request(lambda: sleep(random_uniform(0.5, 1))) |
|
94
|
|
|
app.run(host='0.0.0.0', port=8085) |
|
95
|
|
|
|