def add_app_context( logger: logging.Logger, method_name: str, event_dict: MutableMapping[str, Any], ) -> MutableMapping[str, Any]: if context.exists(): event_dict.update(context.data) return event_dict
async def db_session() -> AsyncGenerator[AsyncSession, None]: # if a session is created for the request, use it directly if context.exists() and "db_session" in context: try: yield context["db_session"] finally: pass # otherwise, create a new session else: session = AsyncSession(get_db_engine()) try: yield session finally: await session.close()
def test_context_outside_of_request_response_cycle(): assert context.exists() is False resp = client.get("/") assert context.exists() is False
async def index(request: Request) -> Response: return JSONResponse({"exists": context.exists()})
def request_user_id(self) -> Optional[str]: return context.data.get("user_id", None) if context.exists() else None
def get_trace_id(cls) -> Optional[str]: return context.data['X-Request-ID'] if context.exists() else None