init_http_session

safir.http.init_http_session(app: Application) → AsyncGenerator

Create an aiohttp.ClientSession and make it available as a "safir/http_session" key on the application.

Parameters:app (aiohttp.web.Application) – The aiohttp.web-based application.

Notes

Use this function as a cleanup context:

app.cleanup_ctx.append(init_http_session)

The session is automatically closed on shut down.

To access the session:

http_session = app["safir/http_session"]

From a request handler:

@routes.get("/")
async def get_index(request: web.Request) -> web.Response:
    http_session = request.config_dict["safir/http_session"]
    ...