Debian Codegraph @

docs/examples/dependency_injection/dependency_yield_simple.py

raw
1 from typing import Dict, Generator
2
3 from litestar import Litestar, get
4 from litestar.di import Provide
5
6 CONNECTION = {"open": False}
7
8
9 def generator_function() -> Generator[Dict[str, bool], None, None]:
10 """Set connection to open and close it after the handler returns."""
11 CONNECTION["open"] = True
12 yield CONNECTION
13 CONNECTION["open"] = False
14
15
16 @get("/", dependencies={"conn": Provide(generator_function)})
17 def index(conn: Dict[str, bool]) -> Dict[str, bool]:
18 """Return the current connection state."""
19 return conn
20
21
22 app = Litestar(route_handlers=[index])