Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (78 loc) · 3.15 KB
/
Copy pathapp.py
File metadata and controls
93 lines (78 loc) · 3.15 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
import falcon
import json
import pandas as pd
class City(object):
def on_get(self, req, resp, city_id):
data_file = open('data/cities.json')
data = json.loads(data_file.read())
resp.body = None
for city in data:
if city['id'] == city_id:
resp.body = json.dumps(city)
return
if resp.body is None:
raise falcon.HTTPError(
"400 Bad Request",
title = "City ID Error",
description = "No city with id `{}` found".format(city_id)
)
class AllCities(object):
def on_get(self, req, resp):
data_file = open('data/cities.json')
resp.body = data_file.read()
class Ranking(object):
def on_post(self, req, resp):
json_body = json.loads(req.bounded_stream.read())
try:
weights = json_body['weights']
except Exception as e:
print(e)
raise falcon.HTTPError(
"400 Bad Request",
title = "Weights Error",
description = "Malformed request. Check out the documentation for how to make a request for this data.",
href = "https://github.com/brokenfoot/api_developer_technical_project/blob/master/doc/endpoints.md"
)
limit = json_body['limit'] if 'limit' in json_body else "all"
try:
if limit != "all":
limit = int(limit)
except ValueError:
raise falcon.HTTPError(
"400 Bad Request",
title = "Limit Error",
description = "`limit` must be a number, not a string."
)
data_file = open('data/cities.json')
data = json.loads(data_file.read())
city_weightings = []
for city in data:
city['overall_score'] = 0
for weight_name, weight_value in weights.items():
try:
city['overall_score'] += city['scores'][weight_name] * float(weight_value)
except KeyError:
raise falcon.HTTPError(
"400 Bad Request",
title = "Weights Error",
description = "Invalid weight parameter `{}` passed".format(weight_name)
)
except ValueError:
raise falcon.HTTPError(
"400 Bad Request",
title = "Weights Error",
description = "`{}` value must be a number, not a string.".format(weight_name)
)
city_weightings.append(city)
df = pd.DataFrame(city_weightings)
df.sort_values(by = 'overall_score', inplace = True, ascending = False)
if limit != "all":
df = df.head(limit)
resp.body = json.dumps({
"data": df.to_dict(orient = 'records'),
"totalRows": len(df)
})
app = falcon.API()
app.add_route('/city/{city_id:int}', City())
app.add_route('/city/all', AllCities())
app.add_route('/rank', Ranking())