Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5f1a422
add FastAPI uvicorn
Kilo59 Mar 21, 2020
8166a4d
define app
Kilo59 Mar 21, 2020
d79965e
create provisional models
Kilo59 Mar 22, 2020
cd64e47
define latest, location endpoints
Kilo59 Mar 22, 2020
21f4472
update models
Kilo59 Mar 22, 2020
b362750
change model name to match pre-existing model
Kilo59 Mar 22, 2020
033d924
defaults
Kilo59 Mar 22, 2020
b1a4742
update versions and response models
Kilo59 Mar 22, 2020
bdae7f0
divide sections
Kilo59 Mar 22, 2020
825de31
create middleware to attach the "data_source"
Kilo59 Mar 22, 2020
e31aad6
validation exception handler
Kilo59 Mar 22, 2020
20c49c2
get location by id
Kilo59 Mar 22, 2020
1cfdc2c
WIP get_all_locations
Kilo59 Mar 22, 2020
e8e50e8
add Totals
Kilo59 Mar 22, 2020
94a2f4e
Mount v2 of the application
Kilo59 Mar 22, 2020
091b461
add FIXME for timelines
Kilo59 Mar 22, 2020
ce9374c
end of file newlines
Kilo59 Mar 22, 2020
0d2f5f3
move models to models.py
Kilo59 Mar 22, 2020
04a35d8
exclude unset
Kilo59 Mar 22, 2020
6ecbc26
gunicorn with FastAPI
Kilo59 Mar 23, 2020
43bdeb0
change version name
Kilo59 Mar 23, 2020
86617e6
create Sources enum
Kilo59 Mar 23, 2020
21e952c
expose v1 and v2 apis via mounted WSGI app
Kilo59 Mar 23, 2020
0a2a262
lock dependencies for linux machines
Kilo59 Mar 23, 2020
8a19959
specify python 3.8 runtime for Heroku
Kilo59 Mar 23, 2020
85e493f
Merge pull request #3 from Kilo59/master
ExpDev07 Mar 23, 2020
ab817bd
add sources
Kilo59 Mar 24, 2020
ece30f3
update prefix
Kilo59 Mar 24, 2020
aec80ac
use separate v2 router
Kilo59 Mar 24, 2020
1949bea
add sources and make Timelines a boolean
Kilo59 Mar 23, 2020
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
Prev Previous commit
Next Next commit
move models to models.py
  • Loading branch information
Kilo59 committed Mar 23, 2020
commit 0d2f5f328ce14fcaed450ee218d44aa0eb32fe4a
56 changes: 6 additions & 50 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,9 @@
import uvicorn
from fastapi.middleware.wsgi import WSGIMiddleware

from .data import data_source
from . import models
from .core import create_app

# #################################
# Models
# #################################


class Totals(pydantic.BaseModel):
confirmed: int
deaths: int
recovered: int


class Latest(pydantic.BaseModel):
latest: Totals


class TimelineStats(pydantic.BaseModel):
latest: int
timeline: Dict[str, int]


class TimelinedLocation(pydantic.BaseModel):
confirmed: TimelineStats
deaths: TimelineStats
recovered: TimelineStats


class Country(pydantic.BaseModel):
coordinates: Dict
country: str
country_code: str
id: int
last_updated: dt.datetime
latest: Totals
province: str = ""
timelines: TimelinedLocation = None # FIXME


class AllLocations(pydantic.BaseModel):
latest: Totals
locations: List[Country]


class Location(pydantic.BaseModel):
location: Country

from .data import data_source

# ################
# Dependencies
Expand Down Expand Up @@ -113,7 +68,7 @@ async def handle_validation_error(
# ################


@APP.get("/latest", response_model=Latest)
@APP.get("/latest", response_model=models.Latest)
def get_latest(request: fastapi.Request):
"""Getting latest amount of total confirmed cases, deaths, and recoveries."""
locations = request.state.source.get_all()
Expand All @@ -126,7 +81,7 @@ def get_latest(request: fastapi.Request):
}


@APP.get("/locations", response_model=AllLocations)
@APP.get("/locations", response_model=models.AllLocations)
def get_all_locations(
request: fastapi.Request, country_code: str = None, timelines: int = 0
):
Expand All @@ -152,10 +107,11 @@ def get_all_locations(
}


@APP.get("/locations/{id}", response_model=Location)
@APP.get("/locations/{id}", response_model=models.Location)
def get_location_by_id(request: fastapi.Request, id: int, timelines: int = 1):
return {"location": request.state.source.get(id).serialize(timelines)}


# mount the existing Flask app to /v2
APP.mount("/v2", WSGIMiddleware(create_app()))

Expand Down
50 changes: 50 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
app.models.py
~~~~~~~~~~~~~
Reponse data models.
"""
import datetime as dt
from typing import Dict, List

import pydantic


class Totals(pydantic.BaseModel):
confirmed: int
deaths: int
recovered: int


class Latest(pydantic.BaseModel):
latest: Totals


class TimelineStats(pydantic.BaseModel):
latest: int
timeline: Dict[str, int]


class TimelinedLocation(pydantic.BaseModel):
confirmed: TimelineStats
deaths: TimelineStats
recovered: TimelineStats


class Country(pydantic.BaseModel):
coordinates: Dict
country: str
country_code: str
id: int
last_updated: dt.datetime
latest: Totals
province: str = ""
timelines: TimelinedLocation = None # FIXME


class AllLocations(pydantic.BaseModel):
latest: Totals
locations: List[Country]


class Location(pydantic.BaseModel):
location: Country