pyfa-converter


Namepyfa-converter JSON
Version 1.0.5.0 PyPI version JSON
download
home_pagehttps://github.com/dotX12/pyfa-converter
SummaryPydantic to fastapi model converter.
upload_time2024-02-20 13:54:33
maintainer
docs_urlNone
authordotX12
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pyfa-converter
Allows you to convert pydantic models for fastapi param models - query, form, header, cookie, body, etc.



### How to install?
`pip install pyfa_converter`

### How to simplify your life?
```python3
from datetime import datetime
from typing import Optional

from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel, Field

from pyfa_converter import FormDepends, PyFaDepends

app = FastAPI()


class PostContractBodySchema(BaseModel):
    title: str = Field(..., description="Description title")
    date: Optional[datetime] = Field(
        None, description="Example: 2021-12-14T09:56:31.056Z"
    )


@app.post("/form-data-body")
async def example_foo_body_handler(
    data1: PostContractBodySchema = PyFaDepends(model=PostContractBodySchema, _type=Form),
    document: UploadFile = File(...),
):
    return {"title": data.title, "date": data.date, "file_name": document.filename}
```

---

### What do I need to do?
```python3
from pyfa_converter import PyFaDepends, FormDepends, QueryDepends
from fastapi import Header, Form
...

async def foo(data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Header)): ...
async def foo(data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Form)): ...

async def foo(data: MyCustomModel = FormDepends(MyCustomModel)): ...
async def foo(data: MyCustomModel = QueryDepends(MyCustomModel)): ...
```

---

If you want to accept a file on an endpoint, then the content-type for that endpoint changes from application/json to www-form-data.

FastAPI does not know how to override the pydantic schema so that parameters are passed as form.
Even if you do

`foo: CustomPydanticModel = Depends()`
all model attributes will be passed as query, but we want them to become body, that's what this library exists for.

### Usually you use something along the lines of:
![image](https://user-images.githubusercontent.com/64792903/161484700-642e3d0e-242f-49f6-82e8-45c5e912a2c2.png)

But, if we accept a lot of fields, then the function becomes very large (the number of attributes for the endpoint increases and it does not look very good).

Thanks to this library, it is possible to force the conversion of Field fields into fields of FastAPI Form with full preservation of all attributes (alias, gt, te, description, title, example and more...)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dotX12/pyfa-converter",
    "name": "pyfa-converter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "dotX12",
    "author_email": "dev@shitposting.team",
    "download_url": "https://files.pythonhosted.org/packages/a7/c2/f10b933ed41a673373b4cfb14958f507e6442465bd1e51577eb3a56684a6/pyfa_converter-1.0.5.0.tar.gz",
    "platform": null,
    "description": "# pyfa-converter\nAllows you to convert pydantic models for fastapi param models - query, form, header, cookie, body, etc.\n\n\n\n### How to install?\n`pip install pyfa_converter`\n\n### How to simplify your life?\n```python3\nfrom datetime import datetime\nfrom typing import Optional\n\nfrom fastapi import FastAPI, UploadFile, File, Form\nfrom pydantic import BaseModel, Field\n\nfrom pyfa_converter import FormDepends, PyFaDepends\n\napp = FastAPI()\n\n\nclass PostContractBodySchema(BaseModel):\n    title: str = Field(..., description=\"Description title\")\n    date: Optional[datetime] = Field(\n        None, description=\"Example: 2021-12-14T09:56:31.056Z\"\n    )\n\n\n@app.post(\"/form-data-body\")\nasync def example_foo_body_handler(\n    data1: PostContractBodySchema = PyFaDepends(model=PostContractBodySchema, _type=Form),\n    document: UploadFile = File(...),\n):\n    return {\"title\": data.title, \"date\": data.date, \"file_name\": document.filename}\n```\n\n---\n\n### What do I need to do?\n```python3\nfrom pyfa_converter import PyFaDepends, FormDepends, QueryDepends\nfrom fastapi import Header, Form\n...\n\nasync def foo(data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Header)): ...\nasync def foo(data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Form)): ...\n\nasync def foo(data: MyCustomModel = FormDepends(MyCustomModel)): ...\nasync def foo(data: MyCustomModel = QueryDepends(MyCustomModel)): ...\n```\n\n---\n\nIf you want to accept a file on an endpoint, then the content-type for that endpoint changes from application/json to www-form-data.\n\nFastAPI does not know how to override the pydantic schema so that parameters are passed as form.\nEven if you do\n\n`foo: CustomPydanticModel = Depends()`\nall model attributes will be passed as query, but we want them to become body, that's what this library exists for.\n\n### Usually you use something along the lines of:\n![image](https://user-images.githubusercontent.com/64792903/161484700-642e3d0e-242f-49f6-82e8-45c5e912a2c2.png)\n\nBut, if we accept a lot of fields, then the function becomes very large (the number of attributes for the endpoint increases and it does not look very good).\n\nThanks to this library, it is possible to force the conversion of Field fields into fields of FastAPI Form with full preservation of all attributes (alias, gt, te, description, title, example and more...)\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pydantic to fastapi model converter.",
    "version": "1.0.5.0",
    "project_urls": {
        "Homepage": "https://github.com/dotX12/pyfa-converter",
        "Repository": "https://github.com/dotX12/pyfa-converter"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0520491f71127ac4ac558b65f733d36351e2d17046d99d69270405107fc17e69",
                "md5": "34c8cf06e68e1e73700bb7c8734732b7",
                "sha256": "559fbbd57a10b0819410ae078dc04fae2a0e41fdfacc5ac7cc4f6121fcfaeaeb"
            },
            "downloads": -1,
            "filename": "pyfa_converter-1.0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "34c8cf06e68e1e73700bb7c8734732b7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 9638,
            "upload_time": "2024-02-20T13:54:32",
            "upload_time_iso_8601": "2024-02-20T13:54:32.066540Z",
            "url": "https://files.pythonhosted.org/packages/05/20/491f71127ac4ac558b65f733d36351e2d17046d99d69270405107fc17e69/pyfa_converter-1.0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7c2f10b933ed41a673373b4cfb14958f507e6442465bd1e51577eb3a56684a6",
                "md5": "547f78e7450d47911b428d835e6d8087",
                "sha256": "feada234adc0164a863d591810cbacbfefd1c3b89cbed62d7e6805012d83e4c1"
            },
            "downloads": -1,
            "filename": "pyfa_converter-1.0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "547f78e7450d47911b428d835e6d8087",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6022,
            "upload_time": "2024-02-20T13:54:33",
            "upload_time_iso_8601": "2024-02-20T13:54:33.150055Z",
            "url": "https://files.pythonhosted.org/packages/a7/c2/f10b933ed41a673373b4cfb14958f507e6442465bd1e51577eb3a56684a6/pyfa_converter-1.0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-20 13:54:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dotX12",
    "github_project": "pyfa-converter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pyfa-converter"
}
        
Elapsed time: 3.61156s