Skip to main content

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?

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?

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

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

Metadata

Release files for pyfa-converter 1.0.5.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 pyfa-converter 1.0.5.0
File Size Uploaded Provenance Actions
pyfa_converter-1.0.5.0.tar.gz 6.0 kB Without provenance Details for pyfa_converter-1.0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyfa-converter 1.0.5.0
File Interpreter ABI Platform Actions
pyfa_converter-1.0.5.0-py3-none-any.whl Python 3 none any Details for pyfa_converter-1.0.5.0-py3-none-any.whl

Total release size: 15.7 kB

Release files / pyfa_converter-1.0.5.0.tar.gz

Download URL pyfa_converter-1.0.5.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
feada234adc0164a863d591810cbacbfefd1c3b89cbed62d7e6805012d83e4c1
BLAKE2b-256 checksum
How to use checksums
a7c2f10b933ed41a673373b4cfb14958f507e6442465bd1e51577eb3a56684a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.2 Linux/6.2.0-1019-azure

Release files / pyfa_converter-1.0.5.0-py3-none-any.whl

Download URL pyfa_converter-1.0.5.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
559fbbd57a10b0819410ae078dc04fae2a0e41fdfacc5ac7cc4f6121fcfaeaeb
BLAKE2b-256 checksum
How to use checksums
0520491f71127ac4ac558b65f733d36351e2d17046d99d69270405107fc17e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.2 Linux/6.2.0-1019-azure

Security for pyfa-converter 1.0.5.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.

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