Skip to main content

An extension of the Tiferet Framework for the Fast API.

Project description

Tiferet Fast - A FastAPI Extension for the Tiferet Framework

Introduction

Tiferet Fast elevates the Tiferet Python framework by enabling developers to build high-performance, asynchronous APIs using FastAPI, grounded in Domain-Driven Design (DDD) principles. Starting with v0.3, Tiferet Fast uses tiferet-openapi as the shared domain backbone for route configuration, Swagger metadata, and error-to-status-code mappings — leaving only FastAPI-specific concerns in this package.

In v0.4, the class-based FastApiBuilder is replaced by a set of composable blueprint functions (build_fast_app, build_router, get_routers, resolve_model), aligning with the Tiferet core blueprint pattern for a simpler, more functional API.

For a deeper understanding of Tiferet's core concepts, refer to the Tiferet documentation.

Getting Started

Requirements

Installation

pip install tiferet-fast

Architecture (v0.4.0)

Tiferet Fast v0.4 is a thin adapter layer. Domain objects, service interfaces, domain events, mappers, and repositories all live in tiferet-openapi. This package provides only:

  • Blueprints (tiferet_fast.blueprints) — Composable functions for assembling FastAPI applications from ApiRouter/ApiRoute domain objects:
    • build_fast_app(interface_id, view_func, **parameters) — One-call assembly of a complete FastAPI app with middleware and routers.
    • build_router(router, view_func) — Builds a single APIRouter from an ApiRouter domain object with Swagger metadata.
    • get_routers(service_provider) — Resolves routers via the service provider.
    • resolve_model(model_path) — Dynamically imports a Pydantic model class by dotted path.
    • FastAPI — Alias for build_fast_app.
  • Contexts (tiferet_fast.contexts) — FastApiContext extends OpenApiContext with FastAPI-specific error handling (converts TiferetAPIError to HTTPException). FastRequestContext is an alias for OpenApiRequestContext.

All domain-layer concerns (routes, routers, request/response models, events, repos) are imported directly from tiferet_openapi.

Usage

Configuration

Tiferet v2 beta supports a consolidated config.yml at the project root:

interfaces:
  calc_fast_api:
    name: Calculator FastAPI
    description: Arithmetic operations via FastAPI with Swagger docs
    module_path: tiferet_fast.contexts.fast
    class_name: FastApiContext
    attrs:
      get_routers_evt:
        module_path: tiferet_openapi.events.openapi
        class_name: GetRouters
      get_route_evt:
        module_path: tiferet_openapi.events.openapi
        class_name: GetRoute
      get_status_code_evt:
        module_path: tiferet_openapi.events.openapi
        class_name: GetStatusCode
      openapi_service:
        module_path: tiferet_openapi.repos.openapi
        class_name: OpenApiYamlRepository
        params:
          openapi_yaml_file: config.yml

openapi:
  routers:
    calc:
      prefix: /calc
      routes:
        add:
          path: /add
          methods: [POST]
          status_code: 200
          summary: Add two numbers
          description: Adds two numbers and returns the result.
          request_model: app.domain.request.TwoOperandRequest
          response_model: app.domain.request.CalculatorResponse
  errors:
    DIVISION_BY_ZERO: 400
    INVALID_INPUT: 422

Routes support Swagger metadata fields (summary, description, tags, request_model, response_model). The response_model is dynamically resolved by resolve_model() and passed to FastAPI's add_api_route() for native Swagger schema generation.

Building and Running the API

from fastapi import Request
from tiferet_fast import FastAPI, FastApiContext
from tiferet_openapi.blueprints.main import realize_interface, resolve_interface

# Define the view function.
async def view_func(request: Request):
    data = await request.json() if request.headers.get('content-type') == 'application/json' else {}
    data.update(dict(request.query_params))
    headers = dict(request.headers)

    response, status_code = context.run(
        feature_id=request.scope['route'].name,
        headers=headers,
        data=data,
    )
    return {'result': response}

# Build the FastAPI application in one call.
fast_app = FastAPI('calc_fast_api', view_func, app_yaml_file='config.yml')

# Realize the context for the view function closure.
app_interface, _ = resolve_interface('calc_fast_api', app_yaml_file='config.yml')
context = realize_interface(app_interface, 'calc_fast_api')

Serve with uvicorn:

uvicorn calc_fast_api:fast_app --reload

Swagger UI is available at http://127.0.0.1:8000/docs.

Example

See the example/ directory for a complete calculator application demonstrating all features.

Migration from v0.3.x

v0.4.0 replaces the class-based FastApiBuilder with composable blueprint functions:

  • FastApiBuilder class — Removed. Use build_fast_app() (or its alias FastAPI) from tiferet_fast.blueprints.
  • FastApiBuilder().load_app_service() — No longer needed. Pass app_yaml_file as a keyword argument to build_fast_app().
  • FastApiBuilder().run(interface_id, view_func) — Replace with build_fast_app(interface_id, view_func, app_yaml_file='config.yml').
  • FastApiBuilder().load_interface(interface_id) — Use resolve_interface() and realize_interface() from tiferet_openapi.blueprints.main.
  • Direct tiferet dependency — Removed from pyproject.toml. Tiferet is now pulled in transitively via tiferet-openapi>=0.1.3.

Before (v0.3.x)

from tiferet_fast import FastApiBuilder

builder = FastApiBuilder()
builder.load_app_service(app_yaml_file='config.yml')
fast_app = builder.run('calc_fast_api', view_func)
context = builder.load_interface('calc_fast_api')

After (v0.4.0)

from tiferet_fast import FastAPI

fast_app = FastAPI('calc_fast_api', view_func, app_yaml_file='config.yml')

Migration from v0.2.x

v0.3.0 removed all domain/interface/event/mapper/repo layers from this package in favor of tiferet-openapi:

  • tiferet_fast.domain — Removed. Use tiferet_openapi.domain (ApiRoute, ApiRouter, ApiRequestModel, ApiResponseModel).
  • tiferet_fast.interfaces — Removed. Use tiferet_openapi.interfaces (OpenApiService).
  • tiferet_fast.events — Removed. Use tiferet_openapi.events (GetRouters, GetRoute, GetStatusCode).
  • tiferet_fast.mappers — Removed. Use tiferet_openapi.mappers.
  • tiferet_fast.repos — Removed. Use tiferet_openapi.repos (OpenApiYamlRepository).
  • fast.yml config with fast: root key — Replaced by openapi.yml or consolidated config.yml with openapi: root key.
  • FastApiContext — Now extends OpenApiContext (from tiferet_openapi) with handle_error() converting TiferetAPIError to HTTPException.
  • FastRequestContext — Now an alias for OpenApiRequestContext.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tiferet_fast-0.4.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiferet_fast-0.4.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file tiferet_fast-0.4.0.tar.gz.

File metadata

  • Download URL: tiferet_fast-0.4.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tiferet_fast-0.4.0.tar.gz
Algorithm Hash digest
SHA256 afc585adb4276455ab4f6f4b3e1d7e07c266d4f649d91a918409e7a4779dcd8c
MD5 c7d76cfd7618a0116522c81b798b34e7
BLAKE2b-256 1a0bc4fd9407a131f6933dda38e87b3dbb333d3916ba2a09caabeb028b8ccf62

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_fast-0.4.0.tar.gz:

Publisher: python-publish.yml on greatstrength/tiferet-fast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tiferet_fast-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: tiferet_fast-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tiferet_fast-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d495a6a1998ca3e93d987c9380193e96800079c6177691ab8087ba478b9eca8f
MD5 4f5b6a51a8b7c5ba7edc533fa8a1b1fa
BLAKE2b-256 82927de942f884ed51448107331fa327463690bb4f445a4ffe801e742c1c8188

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_fast-0.4.0-py3-none-any.whl:

Publisher: python-publish.yml on greatstrength/tiferet-fast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page