Skip to content

Commit e52874e

Browse files
authored
Merge pull request #122 from SeanCena/master
Add ability to filter aggregate data
2 parents a15d421 + acb27f0 commit e52874e

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ GET /v2/locations
4747
```
4848
```json
4949
{
50+
"latest": {
51+
"confirmed": 272166,
52+
"deaths": 11299,
53+
"recovered": 87256
54+
},
5055
"locations": [
5156
{
5257
"id": 0,
@@ -84,7 +89,7 @@ GET /v2/locations
8489
}
8590
```
8691

87-
Additionally, you can also filter by country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
92+
Additionally, you can also filter by any attribute, including province and country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
8893
```http
8994
GET /v2/locations?country_code=US
9095
```

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' and i[:2] != '__':
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+
'latest': {
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)