No project description provided
Project description
Flask-Tjfu-Body 1.0.0
Effortlessly Handle Request Bodies with Class Definitions in Flask
Primary Dependencies
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.
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
File details
Details for the file flask-tjfu-body-1.0.0.tar.gz
.
File metadata
- Download URL: flask-tjfu-body-1.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f572acba8e8be80b848615d5fcc760e96c802cde4fc00cb94891dcf998c984f |
|
MD5 | d1d2e94a3c40d6cfd8c4ce7704584537 |
|
BLAKE2b-256 | 665a42c5984194c1f8cf7090dd34d29422ee13331a4411b017e9715a81742fdc |
File details
Details for the file flask_tjfu_body-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: flask_tjfu_body-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17737c7b604efbc5b3eca081615f7ed7c3abc392178e8f2433d6f8e7a9d68d94 |
|
MD5 | a7131f783e39fd481666f980099667cd |
|
BLAKE2b-256 | 781ea97a63bd7a8b02e7166ac6a14e7bd131c3bddfb00bf55e55019b42be00d1 |