Skip to content

Commit 9b94573

Browse files
committed
Add async/await keywords as necessary up the call chain
1 parent 63f6884 commit 9b94573

File tree

9 files changed

+52
-36
lines changed

9 files changed

+52
-36
lines changed

app/router/v1/all.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55

66
@V1.get("/all")
7-
def all(): # pylint: disable=redefined-builtin
7+
async def all(): # pylint: disable=redefined-builtin
88
"""Get all the categories."""
9-
confirmed = get_category("confirmed")
10-
deaths = get_category("deaths")
11-
recovered = get_category("recovered")
9+
confirmed = await get_category("confirmed")
10+
deaths = await get_category("deaths")
11+
recovered = await get_category("recovered")
1212

1313
return {
1414
# Data.

app/router/v1/confirmed.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
@V1.get("/confirmed")
7-
def confirmed():
7+
async def confirmed():
88
"""Confirmed cases."""
9-
return get_category("confirmed")
9+
confirmed = await get_category("confirmed")
10+
11+
return confirmed

app/router/v1/deaths.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
@V1.get("/deaths")
7-
def deaths():
7+
async def deaths():
88
"""Total deaths."""
9-
return get_category("deaths")
9+
deaths = await get_category("deaths")
10+
11+
return deaths

app/router/v1/recovered.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
@V1.get("/recovered")
7-
def recovered():
7+
async def recovered():
88
"""Recovered cases."""
9-
return get_category("recovered")
9+
recovered = await get_category("recovered")
10+
11+
return recovered

app/router/v2/latest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88

99
@V2.get("/latest", response_model=Latest)
10-
def get_latest(request: Request, source: Sources = "jhu"): # pylint: disable=unused-argument
10+
async def get_latest(request: Request, source: Sources = "jhu"): # pylint: disable=unused-argument
1111
"""
1212
Getting latest amount of total confirmed cases, deaths, and recoveries.
1313
"""
14-
locations = request.state.source.get_all()
14+
locations = await request.state.source.get_all()
1515
return {
1616
"latest": {
1717
"confirmed": sum(map(lambda location: location.confirmed, locations)),

app/router/v2/locations.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# pylint: disable=unused-argument,too-many-arguments,redefined-builtin
1111
@V2.get("/locations", response_model=Locations, response_model_exclude_unset=True)
12-
def get_locations(
12+
async def get_locations(
1313
request: Request,
1414
source: Sources = "jhu",
1515
country_code: str = None,
@@ -28,7 +28,7 @@ def get_locations(
2828
params.pop("timelines", None)
2929

3030
# Retrieve all the locations.
31-
locations = request.state.source.get_all()
31+
locations = await request.state.source.get_all()
3232

3333
# Attempt to filter out locations with properties matching the provided query params.
3434
for key, value in params.items():
@@ -57,8 +57,9 @@ def get_locations(
5757

5858
# pylint: disable=invalid-name
5959
@V2.get("/locations/{id}", response_model=Location)
60-
def get_location_by_id(request: Request, id: int, source: Sources = "jhu", timelines: bool = True):
60+
async def get_location_by_id(request: Request, id: int, source: Sources = "jhu", timelines: bool = True):
6161
"""
6262
Getting specific location by id.
6363
"""
64-
return {"location": request.state.source.get(id).serialize(timelines)}
64+
location = await request.state.source.get(id)
65+
return {"location": location.serialize(timelines)}

app/services/location/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LocationService(ABC):
88
"""
99

1010
@abstractmethod
11-
def get_all(self):
11+
async def get_all(self):
1212
"""
1313
Gets and returns all of the locations.
1414
@@ -18,7 +18,7 @@ def get_all(self):
1818
raise NotImplementedError
1919

2020
@abstractmethod
21-
def get(self, id): # pylint: disable=redefined-builtin,invalid-name
21+
async def get(self, id): # pylint: disable=redefined-builtin,invalid-name
2222
"""
2323
Gets and returns location with the provided id.
2424

app/services/location/csbs.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212

1313
class CSBSLocationService(LocationService):
1414
"""
15-
Servive for retrieving locations from csbs
15+
Service for retrieving locations from csbs
1616
"""
1717

18-
def get_all(self):
19-
# Get the locations
20-
return get_locations()
18+
async def get_all(self):
19+
# Get the locations.
20+
locations = await get_locations()
21+
return locations
2122

22-
def get(self, loc_id): # pylint: disable=arguments-differ
23-
return self.get_all()[loc_id]
23+
async def get(self, loc_id): # pylint: disable=arguments-differ
24+
# Get location at the index equal to the provided id.
25+
locations = await self.get_all()
26+
return locations[loc_id]
2427

2528

2629
# Base URL for fetching data

app/services/location/jhu.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ class JhuLocationService(LocationService):
1818
Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).
1919
"""
2020

21-
def get_all(self):
21+
async def get_all(self):
2222
# Get the locations.
23-
return get_locations()
23+
locations = await get_locations()
24+
return locations
2425

25-
def get(self, loc_id): # pylint: disable=arguments-differ
26+
async def get(self, loc_id): # pylint: disable=arguments-differ
2627
# Get location at the index equal to provided id.
27-
return self.get_all()[loc_id]
28+
locations = await self.get_all()
29+
return locations[loc_id]
2830

2931

3032
# ---------------------------------------------------------------
@@ -103,28 +105,32 @@ async def get_category(category):
103105

104106

105107
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
106-
def get_locations():
108+
async def get_locations():
107109
"""
108110
Retrieves the locations from the categories. The locations are cached for 1 hour.
109111
110112
:returns: The locations.
111113
:rtype: List[Location]
112114
"""
113115
# Get all of the data categories locations.
114-
confirmed = get_category("confirmed")["locations"]
115-
deaths = get_category("deaths")["locations"]
116-
# recovered = get_category('recovered')['locations']
116+
confirmed = await get_category("confirmed")
117+
deaths = await get_category("deaths")
118+
# recovered = await get_category("recovered")
119+
120+
locations_confirmed = confirmed["locations"]
121+
locations_deaths = deaths["locations"]
122+
# locations_recovered = recovered["locations"]
117123

118124
# Final locations to return.
119125
locations = []
120126

121127
# Go through locations.
122-
for index, location in enumerate(confirmed):
128+
for index, location in enumerate(locations_confirmed):
123129
# Get the timelines.
124130
timelines = {
125-
"confirmed": confirmed[index]["history"],
126-
"deaths": deaths[index]["history"],
127-
# 'recovered' : recovered[index]['history'],
131+
"confirmed": locations_confirmed[index]["history"],
132+
"deaths": locations_deaths[index]["history"],
133+
# 'recovered' : locations_recovered[index]['history'],
128134
}
129135

130136
# Grab coordinates.

0 commit comments

Comments
 (0)