I am trying to understand what is the right way to use aiohttp with Sanic.
From aiohttp documentation, I find the following:
Don’t cr
That is essentially what I am doing.
I created a module (interactions.py
) that has, for example a function like this:
async def get(url, headers=None, **kwargs):
async with aiohttp.ClientSession() as session:
log.debug(f'Fetching {url}')
async with session.get(url, headers=headers, ssl=ssl) as response:
try:
return await response.json()
except Exception as e:
log.error(f'Unable to complete interaction: {e}')
return await response.text()
Then I just await
on that:
results = await interactions.get(url)
I am not sure why that is not the "right way". The session (at least for my needs) can be closed as soon as my request is done.