Skip to content

Utils

get_api_routes

get_api_routes(
    router: SupportsRoutes,
) -> Generator[APIRoute, None, None]

Generator that yields APIRoute objects from an APIRouter

Parameters:

Name Type Description Default
router SupportsRoutes

router object, can be APIRouter or FastAPI

required

Yields:

Type Description
APIRoute

Generator[APIRoute, None, None]: the APIRoute object

Source code in fastlimits/utils.py
def get_api_routes(router: SupportsRoutes) -> Generator[APIRoute, None, None]:
    """Generator that yields `APIRoute` objects from an `APIRouter`

    Args:
        router (SupportsRoutes): router object, can be `APIRouter` or `FastAPI`

    Yields:
        Generator[APIRoute, None, None]: the `APIRoute` object
    """
    yield from (r for r in router.routes if isinstance(r, APIRoute))

find_api_route

find_api_route(
    router: SupportsRoutes, func: Callable[..., Any]
) -> Optional[APIRoute]

Find the APIRoute object from the APIRouter.routes or FastAPI.routes

Source code in fastlimits/utils.py
def find_api_route(
    router: SupportsRoutes, func: Callable[..., Any]
) -> Optional[APIRoute]:
    """Find the APIRoute object from the APIRouter.routes or FastAPI.routes"""
    for r in get_api_routes(router):
        if getattr(r, "endpoint", None) == func:
            return r

create_response_model

create_response_model(
    model: Type[ModelT],
    parsed_limit: RateLimitItem,
    show_limit_in_response_model: bool = False,
) -> Type[ModelT]

Returns a copy of the model with the default value updated and placeholders filled.G

Source code in fastlimits/utils.py
def create_response_model(
    model: Type[ModelT],
    parsed_limit: RateLimitItem,
    show_limit_in_response_model: bool = False,
) -> Type[ModelT]:
    """
    Returns a copy of the model with the default value updated and placeholders filled.G
    """
    _model = model
    if show_limit_in_response_model:
        if (detail := model.model_fields.get("detail", None)) is not None:
            _detail = detail.default.format(
                x=parsed_limit.amount,
                y=parsed_limit.multiples,
                granularity=parsed_limit.GRANULARITY.name,
            )
            _model = create_model(
                model.__name__,
                __base__=model,
                detail=(str, _detail),
            )
    return _model

fncopy

fncopy(
    func: Callable[..., R], sig: Tuple[Parameter, ...]
) -> Callable[..., R]

creates a deepcopy of a function with the same code, globals, defaults, closures but slighlty different signature

Parameters:

Name Type Description Default
func Callable[P, R]

the function to create a copy of

required
sig tuple[Parameter]

new parameters

required

Returns:

Type Description
Callable[..., R]

Callable[P, R]: the copied new function

Source code in fastlimits/utils.py
def fncopy(
    func: Callable[..., R], sig: Tuple[inspect.Parameter, ...]
) -> Callable[..., R]:
    """creates a deepcopy of a function with the same code, globals, defaults, closures but slighlty different signature

    Args:
        func (Callable[P, R]): the function to create a copy of
        sig (tuple[inspect.Parameter]): new parameters

    Returns:
        Callable[P, R]: the copied new function
    """
    fn = types.FunctionType(
        func.__code__,
        func.__globals__,
        func.__name__,
        func.__defaults__,
        func.__closure__,
    )
    fn.__dict__.update(func.__dict__)
    fn.__signature__ = inspect.Signature(parameters=sig)  # type: ignore
    return fn

ensure_list

ensure_list(
    value: Optional[Union[P, List[P]]]
) -> List[P]

ensure the value is a list

returns an empty list if value is None

Source code in fastlimits/utils.py
def ensure_list(value: Optional[Union[P, List[P]]]) -> List[P]:
    """ensure the value is a list

    returns an empty list if value is None
    """
    if not value:
        return []
    if isinstance(value, list):
        return value
    return [value]