auth0-python
auth0-python copied to clipboard
AsyncIO - Allow providing an initial aiohttp session to the auth0 client
Describe the problem you'd like to have solved
We've refactored our implementation of auth0 python in order to leverage the new asyncio support. During the development, we've found difficult to provide an aiohttp HTTP client session to the auth0 SDK, making difficult to leverage a best practice like https://docs.aiohttp.org/en/stable/faq.html#how-do-i-manage-a-clientsession-within-a-web-server
Describe the ideal solution
Provide a way to inject a custom aiohttp client session that can be reused in a web server.
Alternatives and current work-arounds
for name in modules.keys():
module = getattr(auth0_client, name)
module._async_client.client.set_session(SingletonAuth0AiohttpClientSession.get_aiohttp_client_session())
where the SingletonAuth0AiohttpClientSession
is a class that is managed by FastAPI application events that automatically closes the session on the application shutdown.
Additional information, if any
Hi @ca-simone-chiorazzo - thanks for raising this
You can share a session between individual services as follows:
Users = asyncify(Users)
Connections = asyncify(Connections)
session = aiohttp.ClientSession()
users = Users(domain, mgmt_api_token)
connections = Connections(domain, mgmt_api_token)
users.set_session(session)
connections.set_session(session)
u = await auth0.users.all_async()
c = await auth0.connections.all_async()
session.close()
Is this what you were after?
@adamjmcgrath Nope, I was referring to passing the client session directly to the Auth0
client and not on individual services
@adamjmcgrath It would be great to have something like:
session = aiohttp.ClientSession()
auth0_client = Auth0(domain='a-domain', token='a-token', http_session=session)
in order to be able to continue leverage the Auth0
object.