|
6 | 6 | import logging |
7 | 7 | import os |
8 | 8 | import reprlib |
9 | | -from typing import Dict, List |
10 | 9 |
|
11 | 10 | import fastapi |
12 | 11 | import pydantic |
13 | 12 | import uvicorn |
14 | 13 |
|
| 14 | +from typing import Dict, List |
| 15 | + |
15 | 16 | from fastapi.middleware.wsgi import WSGIMiddleware |
16 | 17 | from fastapi.middleware.cors import CORSMiddleware |
17 | 18 |
|
18 | 19 | from .core import create_app |
19 | | -from .data import data_source, data_sources |
| 20 | +from .data import data_source |
20 | 21 |
|
21 | 22 | from .models.location import LocationResponse as Location, LocationsResponse as Locations |
22 | 23 | from .models.latest import LatestResponse as Latest |
23 | 24 |
|
24 | | -# ################ |
25 | | -# Dependencies |
26 | | -# ################ |
27 | | - |
28 | | - |
29 | | -class Sources(str, enum.Enum): |
30 | | - """ |
31 | | - A source available for retrieving data. |
32 | | - """ |
33 | | - jhu = 'jhu' |
34 | | - csbs = 'csbs' |
35 | | - |
36 | | - |
37 | 25 | # ############ |
38 | 26 | # FastAPI App |
39 | 27 | # ############ |
@@ -98,94 +86,13 @@ async def handle_validation_error( |
98 | 86 |
|
99 | 87 |
|
100 | 88 | # ################ |
101 | | -# Routes |
| 89 | +# Routing |
102 | 90 | # ################ |
103 | 91 |
|
104 | | -V2 = fastapi.APIRouter() |
105 | | - |
106 | | -@V2.get('/latest', response_model=Latest) |
107 | | -def get_latest(request: fastapi.Request, source: Sources = 'jhu'): |
108 | | - """ |
109 | | - Getting latest amount of total confirmed cases, deaths, and recoveries. |
110 | | - """ |
111 | | - locations = request.state.source.get_all() |
112 | | - return { |
113 | | - 'latest': { |
114 | | - 'confirmed': sum(map(lambda location: location.confirmed, locations)), |
115 | | - 'deaths' : sum(map(lambda location: location.deaths, locations)), |
116 | | - 'recovered': sum(map(lambda location: location.recovered, locations)), |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - |
121 | | -@V2.get( |
122 | | - '/locations', response_model=Locations, response_model_exclude_unset=True |
123 | | -) |
124 | | -def get_locations( |
125 | | - request: fastapi.Request, |
126 | | - source: Sources = 'jhu', |
127 | | - country_code: str = None, |
128 | | - province: str = None, |
129 | | - county: str = None, |
130 | | - timelines: bool = False, |
131 | | -): |
132 | | - """ |
133 | | - Getting the locations. |
134 | | - """ |
135 | | - # All query paramameters. |
136 | | - params = dict(request.query_params) |
137 | | - |
138 | | - # Remove reserved params. |
139 | | - params.pop('source', None) |
140 | | - params.pop('timelines', None) |
141 | | - |
142 | | - # Retrieve all the locations. |
143 | | - locations = request.state.source.get_all() |
144 | | - |
145 | | - # Attempt to filter out locations with properties matching the provided query params. |
146 | | - for key, value in params.items(): |
147 | | - # Clean keys for security purposes. |
148 | | - key = key.lower() |
149 | | - value = value.lower().strip('__') |
150 | | - |
151 | | - # Do filtering. |
152 | | - try: |
153 | | - locations = [location for location in locations if str(getattr(location, key)).lower() == str(value)] |
154 | | - except AttributeError: |
155 | | - pass |
156 | | - |
157 | | - # Return final serialized data. |
158 | | - return { |
159 | | - 'latest': { |
160 | | - 'confirmed': sum(map(lambda location: location.confirmed, locations)), |
161 | | - 'deaths' : sum(map(lambda location: location.deaths, locations)), |
162 | | - 'recovered': sum(map(lambda location: location.recovered, locations)), |
163 | | - }, |
164 | | - 'locations': [location.serialize(timelines) for location in locations], |
165 | | - } |
166 | | - |
167 | | - |
168 | | -@V2.get('/locations/{id}', response_model=Location) |
169 | | -def get_location_by_id(request: fastapi.Request, id: int, source: Sources = 'jhu', timelines: bool = True): |
170 | | - """ |
171 | | - Getting specific location by id. |
172 | | - """ |
173 | | - return { |
174 | | - 'location': request.state.source.get(id).serialize(timelines) |
175 | | - } |
176 | | - |
177 | | - |
178 | | -@V2.get('/sources') |
179 | | -async def sources(): |
180 | | - """ |
181 | | - Retrieves a list of data-sources that are availble to use. |
182 | | - """ |
183 | | - return { |
184 | | - 'sources': list(data_sources.keys()) |
185 | | - } |
| 92 | +from .router import router |
186 | 93 |
|
187 | 94 | # Include routers. |
188 | | -APP.include_router(V2, prefix='/v2', tags=['v2']) |
| 95 | +APP.include_router(router, prefix='/v2', tags=['v2']) |
189 | 96 |
|
190 | 97 | # mount the existing Flask app |
191 | 98 | # v1 @ / |
|
0 commit comments