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:
build_flask_app/FlaskApp— assembles a Flask app fromApiRouter/ApiRoutedomain objects using blueprint functions.FlaskApiContext— extendsOpenApiContextwith Swagger UI support.FlaskRequestContext— alias forOpenApiRequestContextwith Pydantic serialization.
Getting Started
Prerequisites
- Python 3.10+
- A virtual environment (recommended)
Installation
pip install tiferet-flask
This installs tiferet-openapi (which transitively provides tiferet) along with Flask and Flask-CORS.
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 FlaskApp
# 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 = FlaskApp('calc_flask_api', view_func, swagger=True)
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 FlaskApp(), 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.5.0 delegates all domain, interface, event, mapper, and repository concerns to tiferet-openapi. Only two packages remain under tiferet_flask/:
blueprints/— Stateless blueprint functions (build_flask_app,build_blueprint,get_routers,run) that consumeApiRouter/ApiRoutefrom tiferet-openapi, map them to Flask Blueprints, and optionally register a Swagger UI blueprint. Exported asFlaskAppalias.contexts/—FlaskApiContextis a thin subclass ofOpenApiContextthat addscreate_swagger_blueprint().FlaskRequestContextis an alias forOpenApiRequestContext.
For domain-level documentation (domain objects, events, mappers, repositories), see tiferet-openapi.
Migration from v0.4.x
- Builders → Blueprints: The
FlaskAppBuilderclass has been replaced by stateless blueprint functions.FlaskAppBuilder()→build_flask_app()/FlaskApp(). No more class instantiation — call the function directly. - Imports:
from tiferet_flask import FlaskAppBuilder→from tiferet_flask import FlaskApp(orbuild_flask_app). - Usage:
builder = FlaskAppBuilder(); builder.run(...)→flask_app = FlaskApp(interface_id, view_func, swagger=True). - Dependencies: The direct
tiferet>=2.0.0b1dependency has been removed. Tiferet is now provided transitively throughtiferet-openapi>=0.1.3.
Example
See the example/ directory for a complete calculator Flask API demonstrating the v0.5.0 architecture with Swagger support.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tiferet_flask-0.5.0.tar.gz.
File metadata
- Download URL: tiferet_flask-0.5.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20d7458448b6a3e0cf86fa58d6f890649b31e5d8f95670e87c6e301ce9d11e37
|
|
| MD5 |
1b050b7a31843a00fe9ac02598193478
|
|
| BLAKE2b-256 |
23ecff1191affa59491914993d586e180efdb8f05cfe718ce3b9cc926900abfc
|
Provenance
The following attestation bundles were made for tiferet_flask-0.5.0.tar.gz:
Publisher:
python-publish.yml on greatstrength/tiferet-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiferet_flask-0.5.0.tar.gz -
Subject digest:
20d7458448b6a3e0cf86fa58d6f890649b31e5d8f95670e87c6e301ce9d11e37 - Sigstore transparency entry: 1519934579
- Sigstore integration time:
-
Permalink:
greatstrength/tiferet-flask@d47f89f0f820bfe77555479f316fc17fb95b10f2 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/greatstrength
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d47f89f0f820bfe77555479f316fc17fb95b10f2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file tiferet_flask-0.5.0-py3-none-any.whl.
File metadata
- Download URL: tiferet_flask-0.5.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a46b05957baaa29b30a5be6c8757a00521c12f9f47e7ac616cec9cc860fe912
|
|
| MD5 |
a71432206a47ca37ece8bd8b452ac7f4
|
|
| BLAKE2b-256 |
3a501c8e9e218a3fd842ab4cf072be2e43a9eca52e105cc228e1ee9c82b328e2
|
Provenance
The following attestation bundles were made for tiferet_flask-0.5.0-py3-none-any.whl:
Publisher:
python-publish.yml on greatstrength/tiferet-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tiferet_flask-0.5.0-py3-none-any.whl -
Subject digest:
0a46b05957baaa29b30a5be6c8757a00521c12f9f47e7ac616cec9cc860fe912 - Sigstore transparency entry: 1519934625
- Sigstore integration time:
-
Permalink:
greatstrength/tiferet-flask@d47f89f0f820bfe77555479f316fc17fb95b10f2 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/greatstrength
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d47f89f0f820bfe77555479f316fc17fb95b10f2 -
Trigger Event:
release
-
Statement type: