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. Inspired by the concept of beauty in harmony, this extension integrates Tiferet's domain-event-driven architecture with FastAPI's modern, declarative routing and automatic OpenAPI documentation. The result is a robust, modular platform for crafting scalable web services that transform complex business logic into elegant, extensible API designs.

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

Getting Started

Requirements

  • Python 3.10 or later
  • Tiferet >= 2.0.0b1

Installation

pip install tiferet-fast

Project Structure

project_root/
├── calc_fast_api.py
├── app/
│   ├── events/
│   │   ├── __init__.py
│   │   ├── calc.py
│   │   └── settings.py
│   └── configs/
│       ├── __init__.py
│       ├── app.yml
│       ├── container.yml
│       ├── error.yml
│       ├── feature.yml
│       ├── fast.yml
│       └── logging.yml

The app/events/ directory holds domain event classes for arithmetic operations. The app/configs/ directory contains YAML configuration files for application interfaces, dependency injection, error handling, feature workflows, FastAPI routing, and logging. The calc_fast_api.py script initializes and serves the FastAPI application using FastApiBuilder.

Architecture (v0.2.0)

Tiferet Fast v0.2.0 aligns with the Tiferet v2.0 layered architecture:

  • Domain (tiferet_fast.domain) — FastRoute, FastRouter domain objects (Pydantic v2).
  • Interfaces (tiferet_fast.interfaces) — FastApiService abstract service contract.
  • Mappers (tiferet_fast.mappers) — Aggregates and YAML TransferObjects for routes/routers.
  • Events (tiferet_fast.events) — GetRouters, GetRoute, GetStatusCode domain events.
  • Repos (tiferet_fast.repos) — FastYamlRepository (YamlLoader-based service implementation).
  • Contexts (tiferet_fast.contexts) — FastApiContext, FastRequestContext.
  • Builders (tiferet_fast.builders) — FastApiBuilder (primary entry point, aliased as FastAPI).

Usage

Configuring Routes in fast.yml

fast:
  routers:
    calc:
      prefix: /calc
      routes:
        add:
          path: /add
          methods: [POST, GET]
          status_code: 200
        subtract:
          path: /subtract
          methods: [POST, GET]
          status_code: 200
  errors:
    INVALID_INPUT: 400
    DIVISION_BY_ZERO: 422

Configuring the App Interface in app.yml

interfaces:
  calc_fast_api:
    name: Basic Calculator API
    description: Perform basic calculator operations via FastAPI
    module_path: tiferet_fast.contexts.fast
    class_name: FastApiContext
    attrs:
      get_route_evt:
        module_path: tiferet_fast.events.fast
        class_name: GetRoute
      get_status_code_evt:
        module_path: tiferet_fast.events.fast
        class_name: GetStatusCode
      fast_api_service:
        module_path: tiferet_fast.repos.fast
        class_name: FastYamlRepository
        params:
          fast_yaml_file: app/configs/fast.yml

Building and Running the API

from tiferet_fast import FastAPI
from starlette.requests import Request
from starlette.responses import JSONResponse

# Create the builder and load the app service.
app = FastAPI()
app.load_app_service()

# Load the interface context.
context = app.load_interface('calc_fast_api')

# Define a view function for handling requests.
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 JSONResponse(response, status_code=status_code)

# Build the FastAPI application.
fast_app = app.build_fast_app('calc_fast_api', view_func=view_func)

# Serve with uvicorn.
if __name__ == '__main__':
    import uvicorn
    uvicorn.run(fast_app, host='127.0.0.1', port=8000)

Testing the API

# Add two numbers
curl -X POST http://127.0.0.1:8000/calc/add -H "Content-Type: application/json" -d '{"a": 1, "b": 2}'
# Output: 3

# Calculate square root
curl -X POST http://127.0.0.1:8000/calc/sqrt -H "Content-Type: application/json" -d '{"a": 16}'
# Output: 4.0

Migration from v0.1.x

v0.2.0 is a breaking release that aligns with Tiferet v2.0:

  • models/domain/ (Pydantic v2 DomainObject replaces schematics ModelObject)
  • contracts/interfaces/ (Service(ABC) replaces typed contracts)
  • data/mappers/ (Aggregate + TransferObject replace DataObject)
  • handlers/events/ (DomainEvent subclasses replace handler classes)
  • proxies/repos/ (YamlLoader composition replaces YamlConfigurationProxy inheritance)
  • FastApiContext.build_fast_app()FastApiBuilder.build_fast_app() (builder pattern)
  • Top-level export: from tiferet_fast import FastAPI (alias for FastApiBuilder)

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.2.1.tar.gz (13.2 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.2.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tiferet_fast-0.2.1.tar.gz
  • Upload date:
  • Size: 13.2 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.2.1.tar.gz
Algorithm Hash digest
SHA256 c6cfb7a0e9383c46078acc8cb158a0b0d0290a4f85137e031454e5201c93ae6c
MD5 1b22b028954a46752b94ad3d7da592fc
BLAKE2b-256 d29955bc5eed03920f7d8a64b4271c2cd2823b5dd976b3460f73753a87e674f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_fast-0.2.1.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.2.1-py3-none-any.whl.

File metadata

  • Download URL: tiferet_fast-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 715e7b16b6a570b6b52e349945136b6b33a92b8a8671bf226aeeba95410af3ca
MD5 3daec8d06bf4a0f2c4893675bd45eb03
BLAKE2b-256 24e0e5356da3771cea3197d95b865664ba2667045e2fedfa7b3472833067a3b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_fast-0.2.1-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