Skip to main content

Flask-Tjfu-Body 1.0.0

Effortlessly Handle Request Bodies with Class Definitions in Flask

Primary Dependencies

  1. Flask

Installation

pip install flask-tjfu-body

Getting Started

from dataclasses import dataclass

from flask import Flask

from flask_tjfu_body import TJFUBody, String, FormText, FormFile

app = Flask(__name__)
tjfu_body = TJFUBody(app)


@dataclass
class UserLogin:
    username: String
    password: String


@dataclass
class UploadVideo:
    description: FormText
    video: FormFile


@app.route('/login', methods=['POST'])
@tjfu_body.from_json(UserLogin)
def login(cls: UserLogin):
    print(cls.username)
    print(cls.password)
    return "Success"


@app.route('/upload', methods=['POST'])
@tjfu_body.from_form_data(UploadVideo)
def upload_video(cls: UploadVideo):
    desc = cls.description.value
    video = cls.video.value
    video.save(video.filename)
    return "Success"

JSON Data Types

Flask-Tjfu-Body provides 7 JSON data types: Object, Array, String, Integer, Float, Boolean, and Dict, corresponding to class, list, str, int, float, bool, and dict.

from dataclasses import dataclass
from flask_tjfu_body import Object, String, Integer, Array, Boolean, Float, Dict

@dataclass
class User(Object):
    name: String
    age: Integer

@dataclass
class MyJsonBody:
    user: User
    favorite_foods: Array
    male: Boolean
    bmi: Float
    other: Dict

Corresponding JSON:

{
  "user": {
    "name": "John Doe",
    "age": 18
  },
  "favorite_foods": [
    "Apple",
    "Pine Apple"
  ],
  "male": true,
  "bmi": 18.5,
  "other": {}
}

Form Data Types

Flask-Tjfu-Body provides 2 Form Data types: FormText and FormFile, corresponding to str and werkzeug.datastructures.file_storage.FileStorage.

from dataclasses import dataclass
from flask_tjfu_body import FormText, FormFile

@dataclass
class UploadVideo:
    description: FormText
    video: FormFile

Requesting Body from Client

Use from_json for JSON and from_form_data for Form Data, passing the defined class as an argument.

from dataclasses import dataclass

from flask import Flask

from flask_tjfu_body import TJFUBody, String, FormText, FormFile

app = Flask(__name__)
tjfu_body = TJFUBody(app)


@dataclass
class UserLogin:
    username: String
    password: String


@dataclass
class UploadVideo:
    description: FormText
    video: FormFile


@app.route('/login', methods=['POST'])
@tjfu_body.from_json(UserLogin)
def login(cls: UserLogin):
    ...


@app.route('/upload', methods=['POST'])
@tjfu_body.from_form_data(UploadVideo)
def upload_video(cls: UploadVideo):
    ...

Handling Variable Part

Body arguments are placed after Variable Part arguments.

from dataclasses import dataclass
from flask import Flask
from flask_tjfu_body import TJFUBody, String

app = Flask(__name__)
tjfu_body = TJFUBody(app)

@dataclass
class A:
    a: String

@app.route('/user/<user_id>', methods=['POST'])
@tjfu_body.from_json(A)
def user(user_id, a: A):
    ...

Customization

Customize responses for invalid requests with on_from_json_missing_attribute, on_from_json_invalid_attribute_type, and on_from_form_data_invalid_attribute_type.

from flask import Flask

from flask_tjfu_body import TJFUBody

app = Flask(__name__)
tjfu_body = TJFUBody(app)


def on_from_json_missing_attribute(attr, attr_type, on_class, status_code):
    return f"Error: The attribute '{attr}' of type '{attr_type}' is missing in the class '{on_class}'. Status code: {status_code}."


def on_from_json_invalid_attribute_type(attr, attr_type, invalid_type, status_code):
    return f"Error: The attribute '{attr}' is expected to be of type '{attr_type}', but found type '{invalid_type}'. Status code: {status_code}."


def on_from_form_data_invalid_attribute_type(attr, attr_type, invalid_type, status_code):
    return f"Error: The attribute '{attr}' is expected to be of type '{attr_type}', but found type '{invalid_type}'. Status code: {status_code}."


tjfu_body.on_from_json_missing_attribute(on_from_json_missing_attribute)
tjfu_body.on_from_json_invalid_attribute_type(on_from_json_invalid_attribute_type)
tjfu_body.on_from_form_data_invalid_attribute_type(on_from_form_data_invalid_attribute_type)

Changelog

Version 1.0.0 - Initial Release - June 23, 2024

  • Initial release with core functionalities:
    • Define classes and process data from Body into explicitly defined classes.
    • Provide customization functions.

Each section in this changelog provides a summary of what was added, changed, fixed, or removed in each release, helping users and developers understand the evolution of the project and highlighting important updates or improvements.

Metadata

Release files for flask-tjfu-body 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-tjfu-body 1.0.0
File Size Uploaded Provenance Actions
flask-tjfu-body-1.0.0.tar.gz 4.1 kB Without provenance Details for flask-tjfu-body-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-tjfu-body 1.0.0
File Interpreter ABI Platform Actions
flask_tjfu_body-1.0.0-py3-none-any.whl Python 3 none any Details for flask_tjfu_body-1.0.0-py3-none-any.whl

Total release size: 8.5 kB

Release files / flask-tjfu-body-1.0.0.tar.gz

Download URL flask-tjfu-body-1.0.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0f572acba8e8be80b848615d5fcc760e96c802cde4fc00cb94891dcf998c984f
BLAKE2b-256 checksum
How to use checksums
665a42c5984194c1f8cf7090dd34d29422ee13331a4411b017e9715a81742fdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / flask_tjfu_body-1.0.0-py3-none-any.whl

Download URL flask_tjfu_body-1.0.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17737c7b604efbc5b3eca081615f7ed7c3abc392178e8f2433d6f8e7a9d68d94
BLAKE2b-256 checksum
How to use checksums
781ea97a63bd7a8b02e7166ac6a14e7bd131c3bddfb00bf55e55019b42be00d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Security for flask-tjfu-body 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page