File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- from functools import wraps
2- from fastapi import Request
3- from typing import Callable , Any , Union
4-
5-
6- def limited (default_limit : int = 100 ):
7- def decorator (func : Callable ):
8- @wraps (func )
9- async def wrapper (* args , ** kwargs ) -> Any :
10- # Extract request from kwargs (FastAPI automatically passes it)
11- request : Request = kwargs .get ("request" )
12- limit : Union [str , int ] = request .query_params .get ("limit" , default_limit )
13-
14- try :
15- limit = int (limit ) # Ensure limit is an integer
16- except ValueError :
17- limit = default_limit # Fallback if conversion fails
18-
19- # Call the original function
20- result = await func (* args , ** kwargs )
21-
22- # Apply limit if the result is a list
23- if isinstance (result , list ):
24- return result [:limit ]
25- return result # If not a list, return as-is
26-
27- return wrapper
28-
29- return decorator
30-
311from functools import wraps
322from fastapi import Request
333from typing import Callable , Any , Union , Tuple
You can’t perform that action at this time.
0 commit comments