Skip to content

ApiSettings

🛈 DocStrings

Settings of the API server.

Source code in src/docler_api/settings.py
@dataclass(frozen=True)
class ApiSettings:
    """Settings of the API server."""

    token: str | None = None
    """Bearer token clients must send (`Authorization: Bearer <token>`)."""

    cors_origins: tuple[str, ...] = field(default_factory=tuple)
    """Origins allowed to call the API from a browser; none (CORS off) by default."""

    max_upload_bytes: int = DEFAULT_MAX_UPLOAD_MB * 1024 * 1024
    """Largest request body accepted; bigger ones get 413."""

    @classmethod
    def from_env(cls) -> ApiSettings:
        """Read the settings from `DOCLER_API_*` environment variables."""
        origins = os.environ.get(CORS_ORIGINS_ENV, "")
        max_mb = float(os.environ.get(MAX_UPLOAD_MB_ENV) or DEFAULT_MAX_UPLOAD_MB)
        return cls(
            token=os.environ.get(TOKEN_ENV) or None,
            cors_origins=tuple(o.strip() for o in origins.split(",") if o.strip()),
            max_upload_bytes=int(max_mb * 1024 * 1024),
        )

cors_origins class-attribute instance-attribute

cors_origins: tuple[str, ...] = field(default_factory=tuple)

Origins allowed to call the API from a browser; none (CORS off) by default.

max_upload_bytes class-attribute instance-attribute

max_upload_bytes: int = DEFAULT_MAX_UPLOAD_MB * 1024 * 1024

Largest request body accepted; bigger ones get 413.

token class-attribute instance-attribute

token: str | None = None

Bearer token clients must send (Authorization: Bearer <token>).

from_env classmethod

from_env() -> ApiSettings

Read the settings from DOCLER_API_* environment variables.

Source code in src/docler_api/settings.py
@classmethod
def from_env(cls) -> ApiSettings:
    """Read the settings from `DOCLER_API_*` environment variables."""
    origins = os.environ.get(CORS_ORIGINS_ENV, "")
    max_mb = float(os.environ.get(MAX_UPLOAD_MB_ENV) or DEFAULT_MAX_UPLOAD_MB)
    return cls(
        token=os.environ.get(TOKEN_ENV) or None,
        cors_origins=tuple(o.strip() for o in origins.split(",") if o.strip()),
        max_upload_bytes=int(max_mb * 1024 * 1024),
    )

:fa-brands-github: Show source on GitHub