Skip to content

Commit

Permalink
Add region info to jh endpoint
Browse files Browse the repository at this point in the history
This makes better region information available, for example ISO country codes,
and allows the user to filter based on them.

This addresses #43

Example:

    http://127.0.0.1:8000/v1/jh/daily-reports/?country_code_iso2=GB

      {
        "id": 100477,
        "country_region": "United Kingdom",
        "province_state": "Channel Islands",
        "fips": null,
        "admin2": null,
        "last_update": "2020-04-04T23:34:00",
        "confirmed": 262,
        "deaths": 5,
        "recovered": 13,
        "region_info": {
          "uid": 8261,
          "scope": "Province_State",
          "country_code_iso2": "GB",
          "country_code_iso3": "GBR",
          "country_region": "United Kingdom",
          "province_state": "Channel Islands",
          "fips": null,
          "admin2": null
        }
      }

This example includes province/state level reports as well as country/region level. The scope parameter
can be used to filter these out:

    http://127.0.0.1:8000/v1/jh/daily-reports/?country_code_iso2=GB&scope=Country_Region

      {
        "id": 100709,
        "country_region": "United Kingdom",
        "province_state": null,
        "fips": null,
        "admin2": null,
        "last_update": "2020-04-04T23:34:00",
        "confirmed": 41903,
        "deaths": 4313,
        "recovered": 135,
        "region_info": {
          "uid": 826,
          "scope": "Country_Region",
          "country_code_iso2": "GB",
          "country_code_iso3": "GBR",
          "country_region": "United Kingdom",
          "province_state": null,
          "fips": null,
          "admin2": null
        }
      },

The country_region, province_state, fips, admin2 fields duplicate the top level one, but I think we should
include them for backwards compatability. Then if we create a V2 of the API we can drop the top level ones.

The ones in region info are the cleaned versions - they ensure that the same region will always be presented
the same way. The top level ones come from the reports themselves and are inconsistent from day to day.
  • Loading branch information
MatMoore committed Apr 18, 2020
1 parent 9006e24 commit 0468036
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,51 @@ The next step will be to add more data sources.
```
In [1]: import requests
In [2]: response = requests.get('https://api.covid19data.cloud/v1/jh/daily-reports?last_update_from=2020-04-01&last_update_to=2020-04-03&country=Italy')
In [2]: response = requests.get('https://api.covid19data.cloud/v1/jh/daily-reports?last_update_from=2020-04-01&last_update_to=2020-04-03&country_code_iso2=IT&scope=Country_Region')
In [3]: response.json()
Out[3]:
[{'id': 35343,
[{'id': 92789,
'country_region': 'Italy',
'province_state': None,
'fips': None,
'admin2': None,
'last_update': '2020-04-01T21:58:34',
'confirmed': 110574,
'deaths': 13155,
'recovered': 16847},
{'id': 37895,
'recovered': 16847,
'region_info': {
'uid': 380,
'scope': 'Country_Region',
'country_code_iso2': 'IT',
'country_code_iso3': 'ITA',
'country_region': 'Italy',
'province_state': None,
'fips': None,
'admin2': None
}
},
{
'id': 95346,
'country_region': 'Italy',
'province_state': None,
'fips': None,
'admin2': None,
'last_update': '2020-04-02T23:25:14',
'last_update': '2020-04-02T23:25:00',
'confirmed': 115242,
'deaths': 13915,
'recovered': 18278}]
'recovered': 18278,
'region_info': {
'uid': 380,
'scope': 'Country_Region',
'country_code_iso2': 'IT',
'country_code_iso3': 'ITA',
'country_region': 'Italy',
'province_state': None,
'fips': None,
'admin2': None
}
}]
```

Further API documentation is available at https://api.covid19data.cloud/docs
Expand Down
14 changes: 14 additions & 0 deletions covidapi/api/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pkg_resources
from fastapi import Depends
from fastapi import APIRouter
from fastapi import Query
from starlette.responses import HTMLResponse
from typing import List
from datetime import date

from ..services.crud import JHCRUD
from ..services.session import get_db, Session
from ..schemas.schemas import JHDailyReport
from ..schemas.enums import Scope

router = APIRouter()

Expand All @@ -29,13 +31,25 @@ def get_daily_reports(
last_update_to: date = None,
country: str = None,
province: str = None,
country_code_iso2: str = Query(None, min_length=2, max_length=2),
country_code_iso3: str = Query(None, min_length=2, max_length=3),
scope: Scope = None
):
if country_code_iso2:
country_code_iso2 = country_code_iso2.upper()

if country_code_iso3:
country_code_iso3 = country_code_iso3.upper()

daily_reports = JHCRUD().get_daily_reports(
db, skip=skip, limit=limit,
last_update_from=last_update_from,
last_update_to=last_update_to,
country=country,
province=province,
country_code_iso2=country_code_iso2,
country_code_iso3=country_code_iso3,
scope=scope
)

return daily_reports
6 changes: 3 additions & 3 deletions covidapi/schemas/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum

class Scope(Enum):
COUNTRY_REGION = 1
PROVINCE_STATE = 2
ADMIN2 = 3
COUNTRY_REGION = 'Country_Region'
PROVINCE_STATE = 'Province_State'
ADMIN2 = 'Admin2'
18 changes: 17 additions & 1 deletion covidapi/schemas/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
from pydantic import BaseModel
from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional
from .enums import Scope

class JHRegionInfo(BaseModel):
jh_id: int = Field(None, alias='uid')
scope: Scope
country_code_iso2: str
country_code_iso3: str
country_region: str
province_state: Optional[str]
fips: Optional[str]
admin2: Optional[str]

class Config:
orm_mode = True
allow_population_by_field_name = True


class JHDailyReport(BaseModel):
Expand All @@ -13,6 +28,7 @@ class JHDailyReport(BaseModel):
confirmed: int
deaths: int
recovered: int
region_info: JHRegionInfo

class Config:
orm_mode = True
16 changes: 14 additions & 2 deletions covidapi/services/crud.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, joinedload
from datetime import date

from ..db import models
from ..schemas import schemas
from ..schemas.enums import Scope


class JHCRUD:
Expand All @@ -19,8 +20,13 @@ def get_daily_reports(
last_update_to: date = None,
country: str = None,
province: str = None,
country_code_iso2: str = None,
country_code_iso3: str = None,
scope: Scope = None
):
query = db.query(models.JHDailyReport)
query = db.query(models.JHDailyReport).join(
models.JHRegionInfo
)

if last_update_from:
query = query.filter(models.JHDailyReport.last_update >= last_update_from)
Expand All @@ -30,6 +36,12 @@ def get_daily_reports(
query = query.filter(models.JHDailyReport.country_region == country)
if province:
query = query.filter(models.JHDailyReport.province_state == province)
if country_code_iso2:
query = query.filter(models.JHRegionInfo.country_code_iso2 == country_code_iso2)
if country_code_iso3:
query = query.filter(models.JHRegionInfo.country_code_iso2 == country_code_iso3)
if scope:
query = query.filter(models.JHRegionInfo.scope == scope)

query = query.offset(skip).limit(limit)
return query.all()
Expand Down

0 comments on commit 0468036

Please sign in to comment.