Skip to content

Middleware

plateforme.core.api.middleware

This module provides utilities for managing middleware within the Plateforme framework's API using FastAPI and Starlette features.

BaseHTTPMiddleware

BaseHTTPMiddleware(
    app: ASGIApp,
    dispatch: Optional[DispatchFunction] = None,
)
Source code in .venv/lib/python3.12/site-packages/starlette/middleware/base.py
def __init__(
    self, app: ASGIApp, dispatch: typing.Optional[DispatchFunction] = None
) -> None:
    self.app = app
    self.dispatch_func = self.dispatch if dispatch is None else dispatch

Middleware

Middleware(
    cls: type[_MiddlewareClass[P]],
    *args: args,
    **kwargs: kwargs,
)
Source code in .venv/lib/python3.12/site-packages/starlette/middleware/__init__.py
def __init__(
    self,
    cls: type[_MiddlewareClass[P]],
    *args: P.args,
    **kwargs: P.kwargs,
) -> None:
    self.cls = cls
    self.args = args
    self.kwargs = kwargs

plateforme.core.api.background

This module provides utilities for managing background tasks within the Plateforme framework's API using FastAPI and Starlette features.

BackgroundTasks

BackgroundTasks(
    tasks: Sequence[BackgroundTask] | None = None,
)

Bases: BackgroundTasks

A collection of background tasks that will be called after a response has been sent to the client.

Read more about it in the FastAPI docs for Background Tasks.

Example
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}
Source code in .venv/lib/python3.12/site-packages/starlette/background.py
def __init__(self, tasks: typing.Sequence[BackgroundTask] | None = None):
    self.tasks = list(tasks) if tasks else []

add_task

add_task(
    func: Annotated[
        Callable[P, Any],
        Doc(
            "\n                The function to call after the response is sent.\n\n                It can be a regular `def` function or an `async def` function.\n                "
        ),
    ],
    *args: args,
    **kwargs: kwargs,
) -> None

Add a function to be called in the background after the response is sent.

Read more about it in the FastAPI docs for Background Tasks.

Source code in .venv/lib/python3.12/site-packages/fastapi/background.py
def add_task(
    self,
    func: Annotated[
        Callable[P, Any],
        Doc(
            """
            The function to call after the response is sent.

            It can be a regular `def` function or an `async def` function.
            """
        ),
    ],
    *args: P.args,
    **kwargs: P.kwargs,
) -> None:
    """
    Add a function to be called in the background after the response is sent.

    Read more about it in the
    [FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
    """
    return super().add_task(func, *args, **kwargs)