Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ coverage.xml
docs/_build/

# PyBuilder
target/
target/

# OSX stuff
.DS_Store
2 changes: 1 addition & 1 deletion app/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
api_v2 = Blueprint('api_v2', __name__, url_prefix='/v2')

# API version 2.
from .v2 import locations, latest
from .v2 import locations, latest, country

# API version 1.
from .v1 import confirmed, deaths, recovered, all
Expand Down
38 changes: 38 additions & 0 deletions app/routes/v2/country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from flask import jsonify, request
from ...routes import api_v2 as api
from ...services import jhu

@api.route('/country')
def country():

# Retrieve all the locations.
locations = jhu.get_all()

# accepts either country or country_code as a parameter, but only one is required
country_code = request.args.get('country_code', type=str)
country = request.args.get('country', type=str)

# sort by given country or country_code. Country_code will always override country
if country_code:
country_locations = list(filter(lambda location: location.country_code == country_code.upper(), locations))
elif country:
country_locations = list(filter(lambda location: location.country.lower() == country.lower(), locations))

# serialize location objects
country_locations = [location.serialize() for location in country_locations]

# check for errors in query
if not country_locations:
return jsonify({'error': 'parameter country_code or country required'})

# return as a singe dictionary
return jsonify({
'country_code': country_locations[0]['country_code'],
'country' : country_locations[0]['country'],

'latest': {
'confirmed': sum(location['latest']['confirmed'] for location in country_locations),
'deaths' : sum(location['latest']['deaths'] for location in country_locations),
'recovered': sum(location['latest']['recovered'] for location in country_locations),
}
})
37 changes: 37 additions & 0 deletions coronavirus-tracker-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,43 @@ paths:
$ref: '#/definitions/Location'
'404':
description: Location not found
/country:
get:
summary: Latest Corona stats by country
operationId: latest
produces:
- application/json
parameters:
- name: country_code
in: query
description: "ISO alpha 2 country code. https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2"
required: true
type: string
x-example: US
- name: country
in: query
description: "Country to search by"
required: true
type: string
x-example: "China"
responses:
'200':
description: successful operation
schema:
type: object
properties:
latest:
type: object
properties:
confirmed:
type: number
example: 214910
deaths:
type: number
example: 8733
recovered:
type: number
example: 83207
definitions:
Location:
type: object
Expand Down