Spaces:
Sleeping
Sleeping
Download python_backend/src/Agents/utils/route_middleware.py from VashuTheGreat2/ML-Learner: direct link, hf CLI and curl.
- Browser
- Download file 643 Bytes
-
https://huggingface.co/spaces/VashuTheGreat2/ML-Learner/resolve/main/python_backend/src/Agents/utils/route_middleware.py
- Command line
-
hf download hf://spaces/VashuTheGreat2/ML-Learner/python_backend/src/Agents/utils/route_middleware.py
-
curl -L -o route_middleware.py https://huggingface.co/spaces/VashuTheGreat2/ML-Learner/resolve/main/python_backend/src/Agents/utils/route_middleware.py
643 Bytes
| from functools import wraps | |
| from typing import Callable | |
| def route_middleware(*middlewares: Callable): | |
| def decorator(route_func: Callable): | |
| async def wrapper(*args, **kwargs): | |
| req = kwargs.get("req") | |
| if req is None: | |
| raise ValueError("Route must have 'request: Request' parameter") | |
| for middleware in middlewares: | |
| result = middleware(req) | |
| if callable(getattr(result, "__await__", None)): | |
| await result | |
| return await route_func(*args, **kwargs) | |
| return wrapper | |
| return decorator | |