diff --git a/app/main.py b/app/main.py index b9aff949..2bb5b87a 100644 --- a/app/main.py +++ b/app/main.py @@ -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 @@ -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()], ) # ##################### diff --git a/app/utils/httputils.py b/app/utils/httputils.py index a0793170..d29975dc 100644 --- a/app/utils/httputils.py +++ b/app/utils/httputils.py @@ -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() diff --git a/tests/test_httputils.py b/tests/test_httputils.py index 547f3725..ad79db80 100644 --- a/tests/test_httputils.py +++ b/tests/test_httputils.py @@ -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