API reference

safir Package

Safir is the SQuaRE framework for Roundtable bots.

Variables

__version__

The version string of Safir (PEP 440 / SemVer compatible).

version_info

The decomposed version, split across ".."

safir.database Module

Utility functions for database management.

Functions

create_async_session(engine[, logger, statement])

Create a new async database session.

create_database_engine(url, password, *[, ...])

Create a new async database engine.

create_sync_session(url, password[, logger, ...])

Create a new sync database session.

datetime_from_db()

Add the UTC time zone to a naive datetime from the database.

datetime_to_db()

Strip time zone for storing a datetime in the database.

initialize_database(url, password, logger, ...)

Create and initialize a new database.

Classes

DatabaseInitializationError

Database initialization failed.

Class Inheritance Diagram

Inheritance diagram of safir.database.DatabaseInitializationError

safir.dependencies.db_session Module

Manage an async database session.

Classes

DatabaseSessionDependency()

Manages an async per-request SQLAlchemy session.

Variables

db_session_dependency

The dependency that will return the async session proxy.

Class Inheritance Diagram

Inheritance diagram of safir.dependencies.db_session.DatabaseSessionDependency

safir.dependencies.gafaelfawr Module

Gafaelfawr authentication dependencies.

Functions

auth_dependency([x_auth_request_user])

Retrieve authentication information from HTTP headers.

auth_logger_dependency([user, logger])

Logger bound to the authenticated user.

safir.dependencies.http_client Module

HTTP client dependency for FastAPI.

Classes

HTTPClientDependency()

Provides an httpx.AsyncClient as a dependency.

Variables

DEFAULT_HTTP_TIMEOUT

Default timeout (in seconds) for outbound HTTP requests.

http_client_dependency

The dependency that will return the HTTP client.

Class Inheritance Diagram

Inheritance diagram of safir.dependencies.http_client.HTTPClientDependency

safir.dependencies.logger Module

Logger dependency for FastAPI.

Provides a structlog logger as a FastAPI dependency. The logger will incorporate information from the request in its bound context.

Classes

LoggerDependency()

Provides a structlog logger configured with request information.

Variables

logger_dependency

The dependency that will return the logger for the current request.

Class Inheritance Diagram

Inheritance diagram of safir.dependencies.logger.LoggerDependency

safir.kubernetes Module

Utilities for configuring a Kubernetes client.

Functions

initialize_kubernetes()

Load the Kubernetes configuration.

Variables

annotations

safir.logging Module

Utilities for configuring structlog-based logging.

Functions

add_log_severity(logger, method_name, event_dict)

Add the log level to the event dict as severity.

configure_logging(*, name[, profile, ...])

Configure logging and structlog.

Variables

logger_name

Name of the configured global logger.

safir.metadata Module

Standardized metadata for Roundtable HTTP services.

Functions

get_metadata(*, package_name, application_name)

Retrieve metadata for the application.

get_project_url(meta, label)

Get a specific URL from a package's project_urls metadata.

Classes

Metadata(*, name, version[, description, ...])

Metadata about a package.

Class Inheritance Diagram

Inheritance diagram of safir.metadata.Metadata

safir.models Module

Standard models for FastAPI applications.

Examples

To reference the ErrorModel model when returning an error message, use code similar to this:

@router.get(
    "/route/{foo}",
    ...,
    responses={404: {"description": "Not found", "model": ErrorModel}},
)
async def route(foo: str) -> None:
    ...
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail=[
            {"loc": ["path", "foo"], "msg": msg, "type": "invalid_foo"},
        ],
    )

Notes

FastAPI does not appear to export its error response model in a usable form, so define a copy of it so that we can reference it in API definitions to generate good documentation.

Classes

ErrorDetail(*[, loc])

The detail of the error message.

ErrorModel(*, detail)

A structured API error message.

Class Inheritance Diagram

Inheritance diagram of safir.models.ErrorDetail, safir.models.ErrorModel

safir.middleware.x_forwarded Module

Update the request based on X-Forwarded-For headers.

Classes

XForwardedMiddleware(app, *[, proxies])

Middleware to update the request based on X-Forwarded-For.

Class Inheritance Diagram

Inheritance diagram of safir.middleware.x_forwarded.XForwardedMiddleware

safir.testing.kubernetes Module

Mock Kubernetes API for testing.

Functions

patch_kubernetes()

Replace the Kubernetes API with a mock class.

Classes

MockKubernetesApi()

Mock Kubernetes API for testing.

Class Inheritance Diagram

Inheritance diagram of safir.testing.kubernetes.MockKubernetesApi