flask-tjfu-body


Nameflask-tjfu-body JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/duynguyen02/flask-tjfu-body
SummaryNone
upload_time2024-06-22 20:44:08
maintainerNone
docs_urlNone
authorduynguyen02
requires_pythonNone
licenseNone
keywords python flask
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask-Tjfu-Body 1.0.0

Effortlessly Handle Request Bodies with Class Definitions in Flask

### Primary Dependencies

1. [Flask](https://pypi.org/project/Flask/)

### Installation

```
pip install flask-tjfu-body
```

### Getting Started

```python
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`.

```python
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:

```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`.

```python
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.

```python
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.

```python
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`.

```python

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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/duynguyen02/flask-tjfu-body",
    "name": "flask-tjfu-body",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "Python, Flask",
    "author": "duynguyen02",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/66/5a/42c5984194c1f8cf7090dd34d29422ee13331a4411b017e9715a81742fdc/flask-tjfu-body-1.0.0.tar.gz",
    "platform": null,
    "description": "# Flask-Tjfu-Body 1.0.0\n\nEffortlessly Handle Request Bodies with Class Definitions in Flask\n\n### Primary Dependencies\n\n1. [Flask](https://pypi.org/project/Flask/)\n\n### Installation\n\n```\npip install flask-tjfu-body\n```\n\n### Getting Started\n\n```python\nfrom dataclasses import dataclass\n\nfrom flask import Flask\n\nfrom flask_tjfu_body import TJFUBody, String, FormText, FormFile\n\napp = Flask(__name__)\ntjfu_body = TJFUBody(app)\n\n\n@dataclass\nclass UserLogin:\n    username: String\n    password: String\n\n\n@dataclass\nclass UploadVideo:\n    description: FormText\n    video: FormFile\n\n\n@app.route('/login', methods=['POST'])\n@tjfu_body.from_json(UserLogin)\ndef login(cls: UserLogin):\n    print(cls.username)\n    print(cls.password)\n    return \"Success\"\n\n\n@app.route('/upload', methods=['POST'])\n@tjfu_body.from_form_data(UploadVideo)\ndef upload_video(cls: UploadVideo):\n    desc = cls.description.value\n    video = cls.video.value\n    video.save(video.filename)\n    return \"Success\"\n```\n\n### JSON Data Types\n\nFlask-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`.\n\n```python\nfrom dataclasses import dataclass\nfrom flask_tjfu_body import Object, String, Integer, Array, Boolean, Float, Dict\n\n@dataclass\nclass User(Object):\n    name: String\n    age: Integer\n\n@dataclass\nclass MyJsonBody:\n    user: User\n    favorite_foods: Array\n    male: Boolean\n    bmi: Float\n    other: Dict\n```\n\nCorresponding JSON:\n\n```json\n{\n  \"user\": {\n    \"name\": \"John Doe\",\n    \"age\": 18\n  },\n  \"favorite_foods\": [\n    \"Apple\",\n    \"Pine Apple\"\n  ],\n  \"male\": true,\n  \"bmi\": 18.5,\n  \"other\": {}\n}\n```\n\n### Form Data Types\n\nFlask-Tjfu-Body provides 2 Form Data types: `FormText` and `FormFile`, corresponding to `str` and `werkzeug.datastructures.file_storage.FileStorage`.\n\n```python\nfrom dataclasses import dataclass\nfrom flask_tjfu_body import FormText, FormFile\n\n@dataclass\nclass UploadVideo:\n    description: FormText\n    video: FormFile\n```\n\n### Requesting Body from Client\n\nUse `from_json` for JSON and `from_form_data` for Form Data, passing the defined class as an argument.\n\n```python\nfrom dataclasses import dataclass\n\nfrom flask import Flask\n\nfrom flask_tjfu_body import TJFUBody, String, FormText, FormFile\n\napp = Flask(__name__)\ntjfu_body = TJFUBody(app)\n\n\n@dataclass\nclass UserLogin:\n    username: String\n    password: String\n\n\n@dataclass\nclass UploadVideo:\n    description: FormText\n    video: FormFile\n\n\n@app.route('/login', methods=['POST'])\n@tjfu_body.from_json(UserLogin)\ndef login(cls: UserLogin):\n    ...\n\n\n@app.route('/upload', methods=['POST'])\n@tjfu_body.from_form_data(UploadVideo)\ndef upload_video(cls: UploadVideo):\n    ...\n```\n\n### Handling Variable Part\n\nBody arguments are placed after Variable Part arguments.\n\n```python\nfrom dataclasses import dataclass\nfrom flask import Flask\nfrom flask_tjfu_body import TJFUBody, String\n\napp = Flask(__name__)\ntjfu_body = TJFUBody(app)\n\n@dataclass\nclass A:\n    a: String\n\n@app.route('/user/<user_id>', methods=['POST'])\n@tjfu_body.from_json(A)\ndef user(user_id, a: A):\n    ...\n```\n\n### Customization\n\nCustomize responses for invalid requests with `on_from_json_missing_attribute`, `on_from_json_invalid_attribute_type`, and `on_from_form_data_invalid_attribute_type`.\n\n```python\n\nfrom flask import Flask\n\nfrom flask_tjfu_body import TJFUBody\n\napp = Flask(__name__)\ntjfu_body = TJFUBody(app)\n\n\ndef on_from_json_missing_attribute(attr, attr_type, on_class, status_code):\n    return f\"Error: The attribute '{attr}' of type '{attr_type}' is missing in the class '{on_class}'. Status code: {status_code}.\"\n\n\ndef on_from_json_invalid_attribute_type(attr, attr_type, invalid_type, status_code):\n    return f\"Error: The attribute '{attr}' is expected to be of type '{attr_type}', but found type '{invalid_type}'. Status code: {status_code}.\"\n\n\ndef on_from_form_data_invalid_attribute_type(attr, attr_type, invalid_type, status_code):\n    return f\"Error: The attribute '{attr}' is expected to be of type '{attr_type}', but found type '{invalid_type}'. Status code: {status_code}.\"\n\n\ntjfu_body.on_from_json_missing_attribute(on_from_json_missing_attribute)\ntjfu_body.on_from_json_invalid_attribute_type(on_from_json_invalid_attribute_type)\ntjfu_body.on_from_form_data_invalid_attribute_type(on_from_form_data_invalid_attribute_type)\n```\n\n## Changelog\n\n### Version 1.0.0 - Initial Release - June 23, 2024\n\n- Initial release with core functionalities:\n    - Define classes and process data from Body into explicitly defined classes.\n    - Provide customization functions.\n\nEach 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.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/duynguyen02/flask-tjfu-body"
    },
    "split_keywords": [
        "python",
        " flask"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "781ea97a63bd7a8b02e7166ac6a14e7bd131c3bddfb00bf55e55019b42be00d1",
                "md5": "a7131f783e39fd481666f980099667cd",
                "sha256": "17737c7b604efbc5b3eca081615f7ed7c3abc392178e8f2433d6f8e7a9d68d94"
            },
            "downloads": -1,
            "filename": "flask_tjfu_body-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a7131f783e39fd481666f980099667cd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4348,
            "upload_time": "2024-06-22T20:44:05",
            "upload_time_iso_8601": "2024-06-22T20:44:05.370729Z",
            "url": "https://files.pythonhosted.org/packages/78/1e/a97a63bd7a8b02e7166ac6a14e7bd131c3bddfb00bf55e55019b42be00d1/flask_tjfu_body-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "665a42c5984194c1f8cf7090dd34d29422ee13331a4411b017e9715a81742fdc",
                "md5": "d1d2e94a3c40d6cfd8c4ce7704584537",
                "sha256": "0f572acba8e8be80b848615d5fcc760e96c802cde4fc00cb94891dcf998c984f"
            },
            "downloads": -1,
            "filename": "flask-tjfu-body-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d1d2e94a3c40d6cfd8c4ce7704584537",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4121,
            "upload_time": "2024-06-22T20:44:08",
            "upload_time_iso_8601": "2024-06-22T20:44:08.110492Z",
            "url": "https://files.pythonhosted.org/packages/66/5a/42c5984194c1f8cf7090dd34d29422ee13331a4411b017e9715a81742fdc/flask-tjfu-body-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-22 20:44:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "duynguyen02",
    "github_project": "flask-tjfu-body",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "flask-tjfu-body"
}
        
Elapsed time: 0.30046s