flask-msgspec


Nameflask-msgspec JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/floxay/flask-msgspec
Summarymsgspec integration for Flask
upload_time2023-08-26 21:42:01
maintainer
docs_urlNone
authorHuba Tuba
requires_python>=3.8,<4
licenseMIT
keywords api http flask msgspec validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # flask-msgspec
[msgspec](https://github.com/jcrist/msgspec) integration for [Flask](https://github.com/pallets/flask)

This project was inspired by the [flask-pydantic](https://github.com/bauerji/flask-pydantic) package created by [bauerji](https://github.com/bauerji) and the [Litestar](https://github.com/litestar-org/litestar) framework, however while the `validate` decorator appears similar to the one found in `flask-pydantic` there are many differences.

## Installation
```shell
pip install flask-msgspec
```

## Usage
Consider this simple example:
```py
class BodyStructWithConstraints(msgspec.Struct):
    foo: Annotated[int, msgspec.Meta(gt=0, le=100)]
    bar: Annotated[list[str | int], msgspec.Meta(min_length=1)]


@app.post(rule="/test/<uuid:uuid_value>")
@validate()
def test_handler(
    uuid_value: UUID,
    query1: float,
    body: BodyStructWithConstraints,
    optional_query: str = "default_value",
) -> dict:
    return locals()
```
Here we have a UUID path parameter, a required query parameter of `float` type, a body of type `BodyStructWithConstraints`, and an optional query parameter which is a `string`, the endpoint will return a `dictionary` of unknown types.

Currently there is only one reserved keyword; `body`. This tries to convert either `request.data` or `request.form` to the specified type.

Similar to how `Litestar` works, keywords that are neither path parameters or reserved keywords are considered query parameters.

The return type can be set either:
- via the `return_model` keyword in `validate` decorator, or
- by annotating the function return type. (`return_model` keyword takes priority)

Sequences/iterables can also be used for return type, e.g.; `list[ResponseModel]`.

The successful response status code can also be changed in two ways:
- via setting the `status_code` keyword in `validate` decorator, or
- by using the standard Flask syntax of returning a tuple.

Returning a tuple with a status code will override the value set by the `status_code` keyword.

As you might have noticed mixing these together most likely will cause issues or just going to be annoying to annotate.\
***Avoid using the standard Flask tuple return syntax to change the status code if you are also using the function return type to annotate the return model. This will cause issues; first with a static type checker, then with the code handling return values and conversion.***\
In my opinion the response model should be set via annotating the return type of the function and if the status code needs to be changed use the `status_code` keyword. Additionally, if you need to set headers, then set both, the return type and status code using the keywords of the `validate` decorator.

You can use `msgspec.Struct`, `dataclass`, and most built-in types natively supported by `msgspec`.

*More examples and others stuff will be added soon:tm:.*

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/floxay/flask-msgspec",
    "name": "flask-msgspec",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4",
    "maintainer_email": "",
    "keywords": "api,http,flask,msgspec,validation",
    "author": "Huba Tuba",
    "author_email": "hubasolttuba@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e7/c2/d5fc0f5501120f994543f41e32902d3d6777ae00a880c3058733fa5b9eeb/flask_msgspec-0.0.2.tar.gz",
    "platform": null,
    "description": "# flask-msgspec\n[msgspec](https://github.com/jcrist/msgspec) integration for [Flask](https://github.com/pallets/flask)\n\nThis project was inspired by the [flask-pydantic](https://github.com/bauerji/flask-pydantic) package created by [bauerji](https://github.com/bauerji) and the [Litestar](https://github.com/litestar-org/litestar) framework, however while the `validate` decorator appears similar to the one found in `flask-pydantic` there are many differences.\n\n## Installation\n```shell\npip install flask-msgspec\n```\n\n## Usage\nConsider this simple example:\n```py\nclass BodyStructWithConstraints(msgspec.Struct):\n    foo: Annotated[int, msgspec.Meta(gt=0, le=100)]\n    bar: Annotated[list[str | int], msgspec.Meta(min_length=1)]\n\n\n@app.post(rule=\"/test/<uuid:uuid_value>\")\n@validate()\ndef test_handler(\n    uuid_value: UUID,\n    query1: float,\n    body: BodyStructWithConstraints,\n    optional_query: str = \"default_value\",\n) -> dict:\n    return locals()\n```\nHere we have a UUID path parameter, a required query parameter of `float` type, a body of type `BodyStructWithConstraints`, and an optional query parameter which is a `string`, the endpoint will return a `dictionary` of unknown types.\n\nCurrently there is only one reserved keyword; `body`. This tries to convert either `request.data` or `request.form` to the specified type.\n\nSimilar to how `Litestar` works, keywords that are neither path parameters or reserved keywords are considered query parameters.\n\nThe return type can be set either:\n- via the `return_model` keyword in `validate` decorator, or\n- by annotating the function return type. (`return_model` keyword takes priority)\n\nSequences/iterables can also be used for return type, e.g.; `list[ResponseModel]`.\n\nThe successful response status code can also be changed in two ways:\n- via setting the `status_code` keyword in `validate` decorator, or\n- by using the standard Flask syntax of returning a tuple.\n\nReturning a tuple with a status code will override the value set by the `status_code` keyword.\n\nAs you might have noticed mixing these together most likely will cause issues or just going to be annoying to annotate.\\\n***Avoid using the standard Flask tuple return syntax to change the status code if you are also using the function return type to annotate the return model. This will cause issues; first with a static type checker, then with the code handling return values and conversion.***\\\nIn my opinion the response model should be set via annotating the return type of the function and if the status code needs to be changed use the `status_code` keyword. Additionally, if you need to set headers, then set both, the return type and status code using the keywords of the `validate` decorator.\n\nYou can use `msgspec.Struct`, `dataclass`, and most built-in types natively supported by `msgspec`.\n\n*More examples and others stuff will be added soon:tm:.*\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "msgspec integration for Flask",
    "version": "0.0.2",
    "project_urls": {
        "Changelog": "https://github.com/floxay/flask-msgspec/releases",
        "Homepage": "https://github.com/floxay/flask-msgspec",
        "Issue Tracker": "https://github.com/floxay/flask-msgspec/issues",
        "Repository": "https://github.com/floxay/flask-msgspec"
    },
    "split_keywords": [
        "api",
        "http",
        "flask",
        "msgspec",
        "validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9b357a3d65dbc3806ad3529e596446558f8310d1dcb39ba022438e40b6ab82ea",
                "md5": "a65eecececd8bf0a546a56a425b84caa",
                "sha256": "6c4851aa365e172477b62e01a6f23aa80d1971856a3dc1f7feeb29e4656c9ef1"
            },
            "downloads": -1,
            "filename": "flask_msgspec-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a65eecececd8bf0a546a56a425b84caa",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4",
            "size": 5542,
            "upload_time": "2023-08-26T21:41:59",
            "upload_time_iso_8601": "2023-08-26T21:41:59.999584Z",
            "url": "https://files.pythonhosted.org/packages/9b/35/7a3d65dbc3806ad3529e596446558f8310d1dcb39ba022438e40b6ab82ea/flask_msgspec-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e7c2d5fc0f5501120f994543f41e32902d3d6777ae00a880c3058733fa5b9eeb",
                "md5": "2f9146ed3892b35d004d7d3202cdce3e",
                "sha256": "a30cec9d91c5d2f7da0408f06764f02665d5d8fdda285b40240ccbf16e4a9764"
            },
            "downloads": -1,
            "filename": "flask_msgspec-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "2f9146ed3892b35d004d7d3202cdce3e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4",
            "size": 5523,
            "upload_time": "2023-08-26T21:42:01",
            "upload_time_iso_8601": "2023-08-26T21:42:01.505195Z",
            "url": "https://files.pythonhosted.org/packages/e7/c2/d5fc0f5501120f994543f41e32902d3d6777ae00a880c3058733fa5b9eeb/flask_msgspec-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-26 21:42:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "floxay",
    "github_project": "flask-msgspec",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "flask-msgspec"
}
        
Elapsed time: 0.27532s