2323
2424
2525class Sources (str , enum .Enum ):
26- jhu = " jhu"
27- csbs = " csbs"
26+ jhu = ' jhu'
27+ csbs = ' csbs'
2828
2929
3030# ############
3131# FastAPI App
3232# ############
33- LOGGER = logging .getLogger (" api" )
33+ LOGGER = logging .getLogger (' api' )
3434
3535APP = fastapi .FastAPI (
36- title = " Coronavirus Tracker" ,
37- description = " API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak." ,
38- version = " 2.0.1" ,
39- docs_url = "/" ,
40- redoc_url = " /docs" ,
36+ title = ' Coronavirus Tracker' ,
37+ description = ' API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak.' ,
38+ version = ' 2.0.1' ,
39+ docs_url = '/' ,
40+ redoc_url = ' /docs' ,
4141)
4242
4343# #####################
4444# Middleware
4545#######################
4646
4747
48- # TODO this could probably just be a FastAPI dependency
49- @APP .middleware (" http" )
48+ # TODO this could probably just be a FastAPI dependency.
49+ @APP .middleware (' http' )
5050async def add_datasource (request : fastapi .Request , call_next ):
5151 """Attach the data source to the request.state."""
5252 # Retrieve the datas ource from query param.
@@ -60,7 +60,7 @@ async def add_datasource(request: fastapi.Request, call_next):
6060 request .state .source = source
6161
6262 # Move on...
63- LOGGER .info (f" source: { source .__class__ .__name__ } " )
63+ LOGGER .info (f' source: { source .__class__ .__name__ } ' )
6464 response = await call_next (request )
6565 return response
6666
@@ -74,7 +74,7 @@ async def add_datasource(request: fastapi.Request, call_next):
7474async def handle_validation_error (
7575 request : fastapi .Request , exc : pydantic .error_wrappers .ValidationError
7676):
77- return fastapi .responses .JSONResponse ({" message" : exc .errors ()}, status_code = 422 )
77+ return fastapi .responses .JSONResponse ({' message' : exc .errors ()}, status_code = 422 )
7878
7979
8080# ################
@@ -84,27 +84,27 @@ async def handle_validation_error(
8484V2 = fastapi .APIRouter ()
8585
8686
87- @V2 .get (" /latest" , response_model = models .Latest )
88- def get_latest (request : fastapi .Request , source : Sources = " jhu" ):
87+ @V2 .get (' /latest' , response_model = models .Latest )
88+ def get_latest (request : fastapi .Request , source : Sources = ' jhu' ):
8989 """Getting latest amount of total confirmed cases, deaths, and recoveries."""
9090 locations = request .state .source .get_all ()
9191 return {
92- " latest" : {
93- " confirmed" : sum (map (lambda location : location .confirmed , locations )),
94- " deaths" : sum (map (lambda location : location .deaths , locations )),
95- " recovered" : sum (map (lambda location : location .recovered , locations )),
92+ ' latest' : {
93+ ' confirmed' : sum (map (lambda location : location .confirmed , locations )),
94+ ' deaths' : sum (map (lambda location : location .deaths , locations )),
95+ ' recovered' : sum (map (lambda location : location .recovered , locations )),
9696 }
9797 }
9898
9999
100100@V2 .get (
101- " /locations" , response_model = models .Locations , response_model_exclude_unset = True
101+ ' /locations' , response_model = models .Locations , response_model_exclude_unset = True
102102)
103103def get_all_locations (
104104 request : fastapi .Request ,
105105 country_code : str = None ,
106106 timelines : bool = False ,
107- source : Sources = " jhu" ,
107+ source : Sources = ' jhu' ,
108108):
109109 # Retrieve all the locations.
110110 locations = request .state .source .get_all ()
@@ -119,38 +119,38 @@ def get_all_locations(
119119 )
120120 # FIXME: timelines are not showing up
121121 return {
122- " latest" : {
123- " confirmed" : sum (map (lambda location : location .confirmed , locations )),
124- " deaths" : sum (map (lambda location : location .deaths , locations )),
125- " recovered" : sum (map (lambda location : location .recovered , locations )),
122+ ' latest' : {
123+ ' confirmed' : sum (map (lambda location : location .confirmed , locations )),
124+ ' deaths' : sum (map (lambda location : location .deaths , locations )),
125+ ' recovered' : sum (map (lambda location : location .recovered , locations )),
126126 },
127- " locations" : [location .serialize (timelines ) for location in locations ],
127+ ' locations' : [location .serialize (timelines ) for location in locations ],
128128 }
129129
130130
131- @V2 .get (" /locations/{id}" , response_model = models .Location )
131+ @V2 .get (' /locations/{id}' , response_model = models .Location )
132132def get_location_by_id (request : fastapi .Request , id : int , timelines : bool = True ):
133- return {" location" : request .state .source .get (id ).serialize (timelines )}
133+ return {' location' : request .state .source .get (id ).serialize (timelines )}
134134
135135
136- @V2 .get (" /sources" )
136+ @V2 .get (' /sources' )
137137async def sources ():
138138 """
139139 Retrieves a list of data-sources that are availble to use.
140140 """
141- return {" sources" : list (data_sources .keys ())}
141+ return {' sources' : list (data_sources .keys ())}
142142
143143# Include routers.
144- APP .include_router (V2 , prefix = " /v2" , tags = ["v2" ])
144+ APP .include_router (V2 , prefix = ' /v2' , tags = ['v2' ])
145145
146146# mount the existing Flask app
147147# v1 @ /
148- APP .mount ("/" , WSGIMiddleware (create_app ()))
148+ APP .mount ('/' , WSGIMiddleware (create_app ()))
149149
150- if __name__ == " __main__" :
150+ if __name__ == ' __main__' :
151151 uvicorn .run (
152- " app.main:APP" ,
153- host = " 127.0.0.1" ,
154- port = int (os .getenv (" PORT" , 5000 )),
155- log_level = " info" ,
152+ ' app.main:APP' ,
153+ host = ' 127.0.0.1' ,
154+ port = int (os .getenv (' PORT' , 5000 )),
155+ log_level = ' info' ,
156156 )
0 commit comments