Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Fix linter warnings
  • Loading branch information
james-gray committed Apr 2, 2020
commit 77100b0d9039b3baeaabac5394ca755f34bdaa81
4 changes: 2 additions & 2 deletions app/router/v1/confirmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@V1.get("/confirmed")
async def confirmed():
"""Confirmed cases."""
confirmed = await get_category("confirmed")
confirmed_data = await get_category("confirmed")

return confirmed
return confirmed_data
4 changes: 2 additions & 2 deletions app/router/v1/deaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@V1.get("/deaths")
async def deaths():
"""Total deaths."""
deaths = await get_category("deaths")
deaths_data = await get_category("deaths")

return deaths
return deaths_data
4 changes: 2 additions & 2 deletions app/router/v1/recovered.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@V1.get("/recovered")
async def recovered():
"""Recovered cases."""
recovered = await get_category("recovered")
recovered_data = await get_category("recovered")

return recovered
return recovered_data
2 changes: 1 addition & 1 deletion app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def get_locations():
:returns: The locations.
:rtype: dict
"""
async with httputils.client_session.get(BASE_URL) as response:
async with httputils.CLIENT_SESSION.get(BASE_URL) as response:
text = await response.text()

data = list(csv.DictReader(text.splitlines()))
Expand Down
2 changes: 1 addition & 1 deletion app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def get_category(category):
url = BASE_URL + "time_series_covid19_%s_global.csv" % category

# Request the data
async with httputils.client_session.get(url) as response:
async with httputils.CLIENT_SESSION.get(url) as response:
text = await response.text()

# Parse the CSV.
Expand Down
11 changes: 6 additions & 5 deletions app/utils/httputils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""app.utils.httputils.py"""
import logging

from aiohttp import ClientSession


# Singleton aiohttp.ClientSession instance.
client_session: ClientSession
CLIENT_SESSION: ClientSession


LOGGER = logging.getLogger(__name__)
Expand All @@ -17,14 +18,14 @@ async def setup_client_session():
See: https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request

"""
global client_session
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Setting up global aiohttp.ClientSession.")
client_session = ClientSession()
CLIENT_SESSION = ClientSession()


async def teardown_client_session():
"""Close the application-global aiohttp.ClientSession.
"""
global client_session
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Closing global aiohttp.ClientSession.")
await client_session.close()
await CLIENT_SESSION.close()
14 changes: 7 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def mock_client_session_class(request):
See: https://docs.pytest.org/en/5.4.1/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks
"""

httputils.client_session = request.cls.mock_client_session = mock.AsyncMock()
httputils.client_session.get = mocked_session_get
httputils.CLIENT_SESSION = request.cls.mock_client_session = mock.AsyncMock()
httputils.CLIENT_SESSION.get = mocked_session_get
try:
yield
finally:
del httputils.client_session
del httputils.CLIENT_SESSION


@pytest.fixture
Expand All @@ -94,12 +94,12 @@ async def mock_client_session():
instance.
"""

httputils.client_session = mock.AsyncMock()
httputils.client_session.get = mocked_session_get
httputils.CLIENT_SESSION = mock.AsyncMock()
httputils.CLIENT_SESSION.get = mocked_session_get
try:
yield httputils.client_session
yield httputils.CLIENT_SESSION
finally:
del httputils.client_session
del httputils.CLIENT_SESSION


@asynccontextmanager
Expand Down
8 changes: 4 additions & 4 deletions tests/test_httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
async def test_setup_teardown_client_session():
with pytest.raises(AttributeError):
# Ensure client_session is undefined prior to setup
httputils.client_session
httputils.CLIENT_SESSION

await httputils.setup_client_session()

assert httputils.client_session
assert httputils.CLIENT_SESSION

await httputils.teardown_client_session()
assert httputils.client_session.closed
assert httputils.CLIENT_SESSION.closed

del httputils.client_session
del httputils.CLIENT_SESSION