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 elevates the Tiferet Python framework by empowering developers to craft sophisticated Flask-based APIs rooted in Domain-Driven Design (DDD) principles. Drawing from the Kabbalistic ideal of beauty in harmony, Tiferet Flask seamlessly blends Tiferet’s command-driven architecture with Flask’s intuitive routing and request handling. The result is a powerful, modular platform for building scalable web services that distill complex business logic into elegant, extensible API designs.

This tutorial walks you through creating a streamlined calculator API, leveraging Tiferet’s robust commands and configurations while introducing Flask-specific interfaces and endpoints. For a deeper understanding of Tiferet’s foundational concepts, consult the Tiferet documentation.

Getting Started with Tiferet Flask

Embark on your Tiferet Flask journey by preparing your development environment. This guide assumes familiarity with Tiferet’s core setup.

Installing Python

Tiferet Flask requires Python 3.10 or later. Follow the detailed Python installation instructions in the Tiferet README for your platform (Windows, macOS, or Linux). Verify your installation with:

python3.10 --version

Setting Up a Virtual Environment

Create a dedicated virtual environment named tiferet_flask_app to keep dependencies organized:

# Create the Environment
# Windows
python -m venv tiferet_flask_app

# macOS/Linux
python3.10 -m venv tiferet_flask_app

# Activate the Environment
# Windows (Command Prompt)
tiferet_flask_app\Scripts\activate

# Windows (PowerShell)
.\tiferet_flask_app\Scripts\Activate.ps1

# macOS/Linux
source tiferet_flask_app/bin/activate

Exit the environment with deactivate when finished.

Your First Calculator API

With your environment ready, install dependencies and configure the project structure to build a dynamic calculator API using Tiferet Flask.

Installing Tiferet Flask

In your activated virtual environment, install Tiferet, Flask, and the Tiferet Flask extension using pip:

# Windows
pip install tiferet

# macOS/Linux
pip3 install tiferet

Note: If developing locally, replace with the appropriate local installation command.

Project Structure

Adapt Tiferet’s project structure to incorporate Flask, adding a dedicated API script:

project_root/
├── basic_calc.py
├── calc_cli.py
├── calc_flask_api.py
├── app/
    ├── commands/
    │   ├── __init__.py
    │   ├── calc.py
    │   └── settings.py
    └── configs/
        ├── __init__.py
        ├── app.yml
        ├── container.yml
        ├── error.yml
        ├── feature.yml
        ├── flask.yml
        └── logging.yml

The app/commands/ and app/configs/ directories align with Tiferet’s structure (see Tiferet README). The calc_flask_api.py script initializes and runs the Flask API, while flask.yml defines blueprint and routing configurations.

Crafting the Calculator API

Extend Tiferet’s calculator application into a powerful API by reusing its commands and configurations, enhanced with Flask-specific functionality.

Defining Base and Arithmetic Command Classes

Leverage Tiferet’s BasicCalcCommand (app/commands/settings.py) for input validation and arithmetic commands (AddNumber, SubtractNumber, MultiplyNumber, DivideNumber, ExponentiateNumber in app/commands/calc.py) for core operations. These remain unchanged from the original calculator app; refer to the Tiferet README for details.

Configuring the Calculator API

Reuse Tiferet’s container.yml (here), error.yml (here), and feature.yml (here) for command mappings, error handling, and feature workflows. Introduce a Flask-specific interface in app.yml and routing configurations in flask.yml.

Configuring the App Interface in configs/app.yml

Enhance app/configs/app.yml with the calc_flask_api interface:

interfaces:
  calc_flask_api:
    name: Basic Calculator API
    description: Perform basic calculator operations via Flask API
    module_path: tiferet_flask.contexts.flask
    class_name: FlaskApiContext
    attrs:
      flask_api_handler:
        module_path: tiferet_flask.handlers.flask
        class_name: FlaskApiHandler
      flask_repo:
        module_path: tiferet_flask.proxies.yaml.flask
        class_name: FlaskYamlProxy
        params:
          flask_config_file: app/configs/flask.yml

Configuring Blueprints, Routes, and Error Status Codes in configs/flask.yml

Define Flask API blueprints, routes, and error mappings in app/configs/flask.yml:

flask:
  blueprints:
    calc:
      name: calc
      url_prefix: /calc
      routes:
        add:
          rule: /add
          methods: [POST, GET]
          status_code: 200
        subtract:
          rule: /subtract
          methods: [POST, GET]
        multiply:
          rule: /multiply
          methods: [POST, GET]
        divide:
          rule: /divide
          methods: [POST, GET]
        sqrt:
          rule: /sqrt
          methods: [POST, GET]
  errors:
    DIVIDE_BY_ZERO: 400 

The url_prefix ensures all routes are prefixed with /calc. Each route’s endpoint (e.g., calc.add) aligns with the corresponding feature ID in feature.yml. Routes specify a rule, supported HTTP methods, and a default status_code of 200. The errors section maps error codes from error.yml to HTTP status codes, ensuring proper error handling.

This configuration enables FlaskApiContext to orchestrate Flask API operations seamlessly.

Initializing and Demonstrating the API in calc_flask_api.py

Create calc_flask_api.py to initialize the API and define endpoints:

# *** imports

from tiferet import App
from tiferet_flask import FlaskApiContext

# *** functions

# * functions: view_func
def view_func(context: FlaskApiContext, **kwargs):
    '''
    Call the view function whenever a route endpoint is invoked.

    :param context: The Flask API context.
    :type context: FlaskApiContext
    :param kwargs: Additional keyword arguments.
    :type kwargs: dict
    :return: The result of the view function.
    :rtype: Any
    '''

    # Get the Flask request context.
    from flask import request, jsonify

    # Format the request data from the json payload (if applicable) and the query parameters.
    data = dict(request.json) if request.is_json else {}
    data.update(dict(request.args))
    data.update(dict(request.view_args))

    # Format header data from the request headers.
    headers = dict(request.headers)

    # Execute the feature from the request endpoint.
    response, status_code = context.run(
        feature_id=request.endpoint, 
        headers=headers, 
        data=data,
        **kwargs
    )

    # Return the response as JSON.
    return jsonify(response), status_code

# *** exec

# Create the Flask API context.
context: FlaskApiContext = App().load_interface('calc_flask_api')

# Build the Flask app.
context.build_flask_app(view_func=view_func)

# Define the flask_app for external use (e.g., for Flask CLI or WSGI servers).
def flask_app():
    '''
    Create and return the Flask app for testing.

    :return: The Flask app.
    :rtype: Flask
    '''

    return context.flask_app

# Run the Flask app if this script is executed directly.
if __name__ == '__main__':
    context.flask_app.run(host='127.0.0.1', port=5000, debug=True)

This script initializes the Tiferet application, loads the calc_flask_api interface, and dynamically handles RESTful endpoints for arithmetic operations.

Demonstrating the Calculator API

Launch the API:

python3 calc_flask_api.py

Test endpoints using curl or tools like Postman:

# 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

# Calculate 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": "DIVIDE_BY_ZERO", "text": "Cannot divide by zero"}

Conclusion

Tiferet Flask empowers developers to craft elegant, modular Flask APIs within Tiferet’s DDD framework, as showcased in this calculator tutorial. By seamlessly reusing Tiferet’s commands and configurations and introducing a Flask interface, you’ve built a scalable, intuitive API with minimal effort. Expand its capabilities by integrating authentication, advanced features like trigonometric operations, or combining with Tiferet’s CLI or TUI contexts. Dive into the Tiferet documentation for advanced DDD techniques, and experiment with app/configs/ to customize your API, transforming complex web applications—whether monolithic or networked—into clear, purposeful solutions.

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.1.0.tar.gz (14.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_flask-0.1.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tiferet_flask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9a177d885cab7fb3bbe83b5ed1abbca668128760704e5d7c1d703cf68a27a163
MD5 17b4f090d3e88b937201ce3a3cccfda5
BLAKE2b-256 843b7ccb18bb28d2907a0df3a70714d930d6758acb070f90445e690345143efe

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_flask-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: tiferet_flask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tiferet_flask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc26e9e6039369ed3fbc6c57361849585956552904a82529994b5616b3d2a18c
MD5 b4a1581256bd89cb25617e4126f8225f
BLAKE2b-256 c6bd25f39c8b0c3c5ba0ec9b9dc67df0af2231f86f461513ca9ad0ececdca896

See more details on using hashes here.

Provenance

The following attestation bundles were made for tiferet_flask-0.1.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