Skip to content

Commit 2b2fc19

Browse files
authored
Merge pull request #256 from james-gray/async-endpoints
#232: Make all endpoints async
2 parents 27d30fb + c188058 commit 2b2fc19

File tree

20 files changed

+509
-175
lines changed

20 files changed

+509
-175
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ docs/_build/
6666
target/
6767

6868
# OSX Stuff
69-
.DS_Store
69+
.DS_Store
70+
71+
# IntelliJ/Pycharm
72+
.idea/

Pipfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@ url = "https://pypi.org/simple"
44
verify_ssl = true
55

66
[dev-packages]
7+
async-asgi-testclient = "*"
8+
async_generator = "*"
9+
asyncmock = "*"
710
bandit = "*"
811
black = "==19.10b0"
912
coveralls = "*"
13+
importlib-metadata = {version="*", markers="python_version<'3.8'"}
1014
invoke = "*"
1115
isort = "*"
1216
pylint = "*"
1317
pytest = "*"
18+
pytest-asyncio = "*"
1419
pytest-cov = "*"
1520

1621
[packages]
22+
aiohttp = "*"
23+
asyncache = "*"
1724
cachetools = "*"
25+
dataclasses = {version="*", markers="python_version<'3.7'"}
1826
fastapi = "*"
1927
gunicorn = "*"
28+
idna_ssl = {version="*", markers="python_version<'3.7'"}
2029
python-dateutil = "*"
2130
python-dotenv = "*"
2231
requests = "*"

Pipfile.lock

Lines changed: 180 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .data import data_source
1414
from .router.v1 import V1
1515
from .router.v2 import V2
16+
from .utils.httputils import setup_client_session, teardown_client_session
1617

1718
# ############
1819
# FastAPI App
@@ -28,6 +29,8 @@
2829
version="2.0.1",
2930
docs_url="/",
3031
redoc_url="/docs",
32+
on_startup=[setup_client_session],
33+
on_shutdown=[teardown_client_session],
3134
)
3235

3336
# #####################

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_data = await get_category("confirmed")
10+
11+
return confirmed_data

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_data = await get_category("deaths")
10+
11+
return deaths_data

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_data = await get_category("recovered")
10+
11+
return recovered_data

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)),

0 commit comments

Comments
 (0)