Skip to content

Commit 7b950c6

Browse files
committed
Moved aggregates to locations endpoint, side effect is the ability to filter locations by attributes other than just country_code
1 parent d0fef4f commit 7b950c6

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ GET /v2/locations
4545
```
4646
```json
4747
{
48+
"aggregate": {
49+
"confirmed": 272166,
50+
"deaths": 11299,
51+
"recovered": 87256
52+
},
4853
"locations": [
4954
{
5055
"id": 0,

app/routes/v2/latest.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@
33

44
@api.route('/latest')
55
def latest():
6-
# Query parameters.
7-
args = request.args
8-
96
# Get the serialized version of all the locations.
107
locations = request.source.get_all()
11-
#print([i.country_code for i in locations])
12-
13-
# Filter based on args.
14-
if len(args) > 0:
15-
locations = [i for i in locations for j in args if hasattr(i, j) and getattr(i, j) == args[j]]
168

179
# All the latest information.
1810
# latest = list(map(lambda location: location['latest'], locations))

app/routes/v2/locations.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
@api.route('/locations')
66
def locations():
77
# Query parameters.
8-
timelines = strtobool(request.args.get('timelines', default='0'))
9-
country_code = request.args.get('country_code', type=str)
8+
args = request.args
9+
timelines = strtobool(args.get('timelines', default='0'))
1010

1111
# Retrieve all the locations.
1212
locations = request.source.get_all()
1313

14-
# Filtering my country code if provided.
15-
if not country_code is None:
16-
locations = list(filter(lambda location: location.country_code == country_code.upper(), locations))
14+
# Filtering by args if provided.
15+
for i in args:
16+
if i != 'timelines':
17+
try:
18+
locations = [j for j in locations if getattr(j, i) == args.get(i, type=str)]
19+
except AttributeError:
20+
print('TimelinedLocation object does not have attribute {}.'.format(i))
1721

1822
# Serialize each location and return.
1923
return jsonify({
24+
'aggregate': {
25+
'confirmed': sum(map(lambda location: location.confirmed, locations)),
26+
'deaths' : sum(map(lambda location: location.deaths, locations)),
27+
'recovered': sum(map(lambda location: location.recovered, locations)),
28+
},
2029
'locations': [
2130
location.serialize(timelines) for location in locations
2231
]

0 commit comments

Comments
 (0)