Skip to main content

An extension of the Tiferet Framework for the Flask API.

Project description

Tiferet Flask - A Flask Extension for the Tiferet Framework

Introduction

Tiferet Flask extends the Tiferet Python framework to build Flask-based APIs using Domain-Driven Design (DDD). It uses tiferet-openapi as its domain backbone, providing a thin Flask integration layer on top of the shared OpenAPI abstraction.

Key components:

  • FlaskAppBuilder — assembles a Flask app from ApiRouter/ApiRoute domain objects.
  • FlaskApiContext — extends OpenApiContext with Swagger UI support.
  • FlaskRequestContext — alias for OpenApiRequestContext with Pydantic serialization.

Getting Started

Prerequisites

  • Python 3.10+
  • A virtual environment (recommended)

Installation

pip install tiferet-flask

Project Structure

project_root/
├── calc_flask_api.py
└── app/
    ├── events/
    │   ├── __init__.py
    │   └── calc.py
    └── configs/
        ├── app.yml
        ├── openapi.yml
        ├── container.yml
        ├── error.yml
        └── feature.yml

Configuration

App Interface (app/configs/app.yml)

Define the Flask API interface using tiferet-openapi types:

interfaces:
  calc_flask_api:
    name: Calculator Flask API
    description: Arithmetic operations via Flask API
    module_path: tiferet_flask.contexts.flask
    class_name: FlaskApiContext
    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: app/configs/openapi.yml

OpenAPI Configuration (app/configs/openapi.yml)

Define routers, routes, and error status codes using the openapi: root key:

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

Usage

Entry Point (calc_flask_api.py)

from tiferet_flask import FlaskAppBuilder

# Create the builder.
builder = FlaskAppBuilder()

# Define the view function.
def view_func(**kwargs):
    from flask import request, jsonify

    data = dict(request.json) if request.is_json else {}
    data.update(dict(request.args))
    data.update(kwargs)
    headers = dict(request.headers)

    response, status_code = context.run(
        feature_id=request.endpoint,
        headers=headers,
        data=data,
    )
    return jsonify(response), status_code

# Build the Flask app with Swagger UI enabled.
flask_app = builder.run('calc_flask_api', view_func, swagger=True)

# Access the context for the view function closure.
context = builder.load_interface('calc_flask_api')

if __name__ == '__main__':
    flask_app.run(host='127.0.0.1', port=5000, debug=True)

Endpoints

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

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

# Division by zero
curl -X POST http://127.0.0.1:5000/calc/divide \
  -H "Content-Type: application/json" -d '{"a": 5, "b": 0}'
# Output: {"error_code": "DIVISION_BY_ZERO", ...}

Swagger UI

When swagger=True is passed to builder.run(), Swagger UI is available at:

  • Swagger UI: http://127.0.0.1:5000/docs
  • OpenAPI spec: http://127.0.0.1:5000/docs/openapi.json

Architecture

Tiferet Flask v0.4.0 delegates all domain, interface, event, mapper, and repository concerns to tiferet-openapi. Only two packages remain under tiferet_flask/:

  • builders/FlaskAppBuilder consumes ApiRouter/ApiRoute from tiferet-openapi, maps them to Flask Blueprints, and optionally registers a Swagger UI blueprint.
  • contexts/FlaskApiContext is a thin subclass of OpenApiContext that adds create_swagger_blueprint(). FlaskRequestContext is an alias for OpenApiRequestContext.

For domain-level documentation (domain objects, events, mappers, repositories), see tiferet-openapi.

Example

See the example/ directory for a complete calculator Flask API demonstrating the v0.4.0 architecture with Swagger support.

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_flask-0.4.0.tar.gz (8.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_flask-0.4.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tiferet_flask-0.4.0.tar.gz
Algorithm Hash digest
SHA256 da51f9774a40fa2c327e43d4011e8ec99455db5194fa202e47ada149e57dae8d
MD5 9ab3d52e7d99a37cbdbc8a441c0fdb7d
BLAKE2b-256 04ed969f44902e96c8f23bb4d9853ee44a8cd2eae342ebbe23276505aab2a6c2

See more details on using hashes here.

Provenance

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

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

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_flask-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: tiferet_flask-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.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_flask-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c4c15056c577e8c617890834f8f4ed61eefeb7ae4ea46ee3efb562d3ba3c1d1
MD5 5dafd89f6e995e8cbf199e8937215ea5
BLAKE2b-256 8a22ba09a8f3d3167528f06bdc6b57473f4f739e91cd3b4d43099394feac8e58

See more details on using hashes here.

Provenance

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

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

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