docs/examples/lifecycle_hooks/after_response.py
raw| 1 | from collections import defaultdict |
| 2 | from typing import Dict |
| 3 | |
| 4 | from litestar import Litestar, Request, get |
| 5 | |
| 6 | COUNTER: Dict[str, int] = defaultdict(int) |
| 7 | |
| 8 | |
| 9 | async def after_response(request: Request) -> None: |
| 10 | COUNTER[request.url.path] += 1 |
| 11 | |
| 12 | |
| 13 | @get("/hello") |
| 14 | async def hello() -> Dict[str, int]: |
| 15 | return COUNTER |
| 16 | |
| 17 | |
| 18 | app = Litestar(route_handlers=[hello], after_response=after_response) |
| 19 | |
| 20 | |
| 21 | # run: /hello |
| 22 | # run: /hello |