Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from .config import get_settings
from .data import data_source
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session
from .utils.httputils import Session

# ############
# Creating Session
# ############
clientSession = Session()

# ############
# FastAPI App
Expand All @@ -37,8 +42,8 @@
version="2.0.4",
docs_url="/",
redoc_url="/docs",
on_startup=[setup_client_session],
on_shutdown=[teardown_client_session],
on_startup=[clientSession.setup_client_session()],
on_shutdown=[clientSession.teardown_client_session()],
)

# #####################
Expand Down
47 changes: 32 additions & 15 deletions app/utils/httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,41 @@
CLIENT_SESSION: ClientSession


LOGGER = logging.getLogger(__name__)
# LOGGER = logging.getLogger(__name__)


async def setup_client_session():
"""Set up the application-global aiohttp.ClientSession instance.
class Session:
def _init_(self):
self.__CLIENT_SESSION = CLIENT_SESSION
self.__LOGGER = logging.getLogger(__name__)

aiohttp recommends that only one ClientSession exist for the lifetime of an application.
See: https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request
def getClientSession(self):
return self.__CLIENT_SESSION

"""
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Setting up global aiohttp.ClientSession.")
CLIENT_SESSION = ClientSession()
def reassignClientSession(self):
self.__CLIENT_SESSION = CLIENT_SESSION # reassign to updated CLIENT_SESSION

def reassignLogger(self):
self.__LOGGER = logging.getLogger(__name__)

async def teardown_client_session():
"""Close the application-global aiohttp.ClientSession.
"""
global CLIENT_SESSION # pylint: disable=global-statement
LOGGER.info("Closing global aiohttp.ClientSession.")
await CLIENT_SESSION.close()
async def setup_client_session(self):
"""Set up the application-global aiohttp.ClientSession instance.

aiohttp recommends that only one ClientSession exist for the lifetime of an application.
See: https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request

"""
global CLIENT_SESSION # pylint: disable=global-statement
self.reassignLogger()
self.__LOGGER.info("Setting up global aiohttp.ClientSession.")
CLIENT_SESSION = ClientSession()
self.reassignClientSession()

async def teardown_client_session(self):
"""Close the application-global aiohttp.ClientSession.
"""
global CLIENT_SESSION # pylint: disable=global-statement
self.reassignLogger()
self.__LOGGER.info("Closing global aiohttp.ClientSession.")
await CLIENT_SESSION.close()
self.reassignClientSession()
6 changes: 4 additions & 2 deletions tests/test_httputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ async def test_setup_teardown_client_session():
# Ensure client_session is undefined prior to setup
httputils.CLIENT_SESSION

await httputils.setup_client_session()
testSesssion = httputils.Session()

await testSesssion.setup_client_session()

assert httputils.CLIENT_SESSION

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

del httputils.CLIENT_SESSION