msflib.api¶
Part of the msflib core package.
msflib.api
¶
DependencyNamespace(**entries: Any)
¶
Lightweight attribute namespace with explicit dictionary helpers.
EnhancedAPIRouter(router: APIRouter)
¶
Thin overlay over :class:~fastapi.APIRouter that adds
validation_types support to HTTP-method decorators.
Instantiate via :func:create_enhanced_router rather than directly.
All standard HTTP-method properties (get, post, put, patch,
delete, options, head) forward to the underlying router after
patching the endpoint function's __signature__. Any other attribute
access (e.g. include_router, add_api_route, routes) is
transparently delegated to the underlying :class:~fastapi.APIRouter.
create_enhanced_router(router: APIRouter) -> EnhancedAPIRouter
¶
Wrap router with :class:EnhancedAPIRouter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
APIRouter
|
The :class: |
required |
Returns:
| Type | Description |
|---|---|
EnhancedAPIRouter
|
A thin overlay that exposes the same HTTP-method decorators with an
extra |
Example
::
from fastapi import APIRouter, Depends
from typing import Any
from sqlmodel import Session
from msflib.api import create_enhanced_router
def my_router(get_session, create_type=MyCreate):
api_router = APIRouter(prefix="/things")
enh_router = create_enhanced_router(api_router)
@enh_router.post(
"/",
response_model=MyRead,
status_code=201,
validation_types={"data": create_type},
)
def create_thing(
*,
session: Session = Depends(get_session),
data: Any,
) -> Any:
return action.create(session=session, data=data)
return api_router # return the original router for include_router
deps
¶
DependencyNamespace(**entries: Any)
¶
Lightweight attribute namespace with explicit dictionary helpers.
get_session_factory(engine) -> Callable[[], Generator]
cached
¶
Return a dependency that yields DB sessions from an engine.
get_keystore_factory(redis_host: str | None = None, redis_password: str | None = None, redis_port: int = 6379) -> Callable[[], StoreInterface]
¶
Return a dependency that provides a keystore (Redis or in-memory).
typed_router
¶
typed_router — inject runtime Pydantic/SQLModel validation types into FastAPI route handlers while keeping decorator-style ergonomics.
Why this exists¶
FastAPI resolves request-body schemas by inspecting a function's
__signature__, not its source-level __annotations__. That means we
can replace a parameter's annotation with a concrete model class before
APIRouter.add_api_route is called, and FastAPI will validate the incoming
request against that class — while the function body still receives a plain
argument under the same name.
This module wraps :class:~fastapi.APIRouter in a thin :class:EnhancedAPIRouter
overlay that adds a validation_types keyword argument to every HTTP-method
decorator. Only parameters listed in validation_types are affected; every
other parameter annotation (including FastAPI Depends markers) is left
untouched.
Usage¶
::
from fastapi import APIRouter, Depends
from typing import Any
from sqlmodel import Session
from msflib.api import create_enhanced_router
api_router = APIRouter(prefix="/items")
enh_router = create_enhanced_router(api_router)
@enh_router.post(
"/",
response_model=ItemRead,
status_code=201,
validation_types={"data": ItemCreate},
)
def create_item(
*,
session: Session = Depends(get_session),
data: Any, # annotation is replaced at registration time
) -> Any:
return item_action.create(session=session, data=data)
The underlying api_router is completely unmodified and should still be
passed to app.include_router. EnhancedAPIRouter is a registration-time
overlay only.
EnhancedAPIRouter(router: APIRouter)
¶
Thin overlay over :class:~fastapi.APIRouter that adds
validation_types support to HTTP-method decorators.
Instantiate via :func:create_enhanced_router rather than directly.
All standard HTTP-method properties (get, post, put, patch,
delete, options, head) forward to the underlying router after
patching the endpoint function's __signature__. Any other attribute
access (e.g. include_router, add_api_route, routes) is
transparently delegated to the underlying :class:~fastapi.APIRouter.
create_enhanced_router(router: APIRouter) -> EnhancedAPIRouter
¶
Wrap router with :class:EnhancedAPIRouter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
APIRouter
|
The :class: |
required |
Returns:
| Type | Description |
|---|---|
EnhancedAPIRouter
|
A thin overlay that exposes the same HTTP-method decorators with an
extra |
Example
::
from fastapi import APIRouter, Depends
from typing import Any
from sqlmodel import Session
from msflib.api import create_enhanced_router
def my_router(get_session, create_type=MyCreate):
api_router = APIRouter(prefix="/things")
enh_router = create_enhanced_router(api_router)
@enh_router.post(
"/",
response_model=MyRead,
status_code=201,
validation_types={"data": create_type},
)
def create_thing(
*,
session: Session = Depends(get_session),
data: Any,
) -> Any:
return action.create(session=session, data=data)
return api_router # return the original router for include_router