🪛 A simple pydantic to Form FastAPI model converter.

Overview

pyfa-converter

Makes it pretty easy to create a model based on Field [pydantic] and use the model for www-form-data.

How to install?

pip install pyfa_converter

How to simplify your life?

image


What do I need to do with the model?

  • We put the decorator @PydanticConverter.body for the model and enjoy.
  • data: YourPydanticModel = FormBody(YourPydanticModel)

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

You might also like...
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

pwy - A simple weather tool.
pwy - A simple weather tool.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging. Name changed from wwy to pwy.

A simple python script to execute a command when a YubiKey is disconnected

YubiKeyExecute A python script to execute a command when a YubiKey / YubiKeys are disconnected. ‏‏‎ ‎ How to use: 1. Download the latest release and d

A super simple wallet application for the NANO cryptocurrency that runs in the terminal
A super simple wallet application for the NANO cryptocurrency that runs in the terminal

Nano Terminal Wallet A super simple wallet application for the NANO cryptocurrency that runs in the terminal Written in 2021 by NinjaSnail1080 (Discor

This a simple tool to query the awesome ippsec.rocks website from your terminal
This a simple tool to query the awesome ippsec.rocks website from your terminal

ippsec-cli This a simple tool to query the awesome ippsec.rocks website from your terminal Installation and usage cd /opt git clone https://github.com

Simple Tool To Grab Like-Card Coupon
Simple Tool To Grab Like-Card Coupon

Simple Tool To Grab Like-Card Coupon

(BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming.

blush 😳 (BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming. Note: The Linux executables were made on Ubu

A simple reverse shell in python

RevShell A simple reverse shell in python Getting started First, start the server python server.py Finally, start the client (victim) python client.py

Un module simple pour demander l'accord de l'utilisateur dans une CLI.
Un module simple pour demander l'accord de l'utilisateur dans une CLI.

Demande de confirmation utilisateur pour CLI Présentation ask_lib est un module pour le langage Python proposant une seule fonction; ask(). Le but pri

Comments
  • Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Using the

    from pyfa_converter import FormDepends
    

    and get the error

    TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'
    

    python: 3.9

    opened by Terryhung 4
  • Add License

    Add License

    Hi! The code in this repo seems super useful for us working with pydantic + fastapi, but we can't use it without a LICENSE. Could you add LICENSE file to the repo? MIT is a permissive one if you don't have an opinion otherwise

    opened by dzcode 3
  • Big update 1.0.0.0!

    Big update 1.0.0.0!

    • The decorator above the model is no longer required and will be removed in the next version!
    • New syntax:
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Header)
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Form)
    
    data: MyCustomModel = FormDepends(MyCustomModel)
    data: MyCustomModel = QueryDepends(MyCustomModel)
    
    • Added support for parameters specific only to FastAPI types in Pydantic Field. Example - Field(None, convert_underscores=True)
    opened by dotX12 0
  • error when adding Field constraint

    error when adding Field constraint

    On:

    python: 3.10 and 3.11 pyfa-converter: 1.0.3.0 (and master, which shows as 1.0.1.0) pydantic: 1.10.2

    If you add a Field constraint, pydantic raises a ValueError:

    For example, for the class in this repo, if you change it to (adding a gt constraint of 10:

    class PostContractSmallDoubleBodySchema(BaseModel):
        id: Optional[int] = Field(None, description="gwa", gt=10)
        title: Optional[str] = Field(None)
        data: Optional[List[int]]
    

    pydantic raises the following exception

    ImportError while loading conftest '/home/ben/repos/pyfa-converter/tests/conftest.py'.
    tests/conftest.py:6: in <module>
        from examples.main import app
    examples/main.py:47: in <module>
        @app.post("/test")
    .venv/lib/python3.11/site-packages/fastapi/routing.py:630: in decorator
        self.add_api_route(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:569: in add_api_route
        route = route_class(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:438: in __init__
        self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:292: in get_dependant
        sub_dependant = get_param_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:122: in get_param_sub_dependant
        return get_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:158: in get_sub_dependant
        sub_dependant = get_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:299: in get_dependant
        param_field = get_param_field(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:388: in get_param_field
        annotation = get_annotation_from_field_info(annotation, field_info, param_name)
    pydantic/schema.py:1011: in pydantic.schema.get_annotation_from_field_info
        ???
    E   ValueError: On field "id" the following field constraints are set but not enforced: gt.
    E   For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints
    

    I spent some time investigating and I'm still not sure why this is happening

    bug fixed 
    opened by falkben 9
Releases(1.0.4.0a)
Python wrapper and CLI utility to render LaTeX markup and equations as SVG using dvisvgm and svgo.

latex2svg Python wrapper and CLI utility to render LaTeX markup and equations as SVG using dvisvgm and svgo. Based on the original work by Tino Wagner

Matthias C. Hormann 4 Feb 18, 2022
A command line tool that creates a super timeline from SentinelOne's Deep Visibility data

S1SuperTimeline A command line tool that creates a super timeline from SentinelOne's Deep Visibility data What does it do? The script accepts a S1QL q

Juan Ortega 2 Feb 08, 2022
EODAG is a command line tool and a plugin-oriented Python framework for searching, aggregating results and downloading remote sensed images while offering a unified API for data access regardless of the data provider

EODAG (Earth Observation Data Access Gateway) is a command line tool and a plugin-oriented Python framework for searching, aggregating results and downloading remote sensed images while offering a un

CS GROUP 205 Jan 03, 2023
flora-dev-cli (fd-cli) is command line interface software to interact with flora blockchain.

Install git clone https://github.com/Flora-Network/fd-cli.git cd fd-cli python3 -m venv venv source venv/bin/activate pip install -e . --extra-index-u

14 Sep 11, 2022
A simple command line virtual operating system, written in python

Virtual operating system A simple virtual operating system written in python. (Under development). Currently, the following commands are supported: Co

B.Jothin kumar 7 Nov 15, 2022
Baseline is a cross-platform library and command-line utility that creates file-oriented baselines of your systems.

Baselining, on steroids! Baseline is a cross-platform library and command-line utility that creates file-oriented baselines of your systems. The proje

Nelson 4 Dec 09, 2022
Turdshovel is an interactive CLI tool that allows users to dump objects from .NET memory dumps

Turdshovel Description Turdshovel is an interactive CLI tool that allows users to dump objects from .NET memory dumps without having to fully understa

Leron Gray 41 Jul 27, 2022
Unofficial Open Corporates CLI: OpenCorporates is a website that shares data on corporations under the copyleft Open Database License. This is an unofficial open corporates python command line tool.

Unofficial Open Corporates CLI OpenCorporates is a website that shares data on corporations under the copyleft Open Database License. This is an unoff

Richard Mwewa 30 Sep 08, 2022
RSS reader client for CLI (Command Line Interface),

rReader is RSS reader client for CLI(Command Line Interface)

Lee JunHaeng 10 Dec 24, 2022
GitFun - A Simplified Automated CLI tool for GIT😃

GitFun A Simplified Automated CLI tool for GIT, It's for Lazy Developers and Newbies 😜 Table of contents GitFun Installation Usage Development Contri

Astaqc 8 Feb 22, 2022
WebApp Maker make web apps (Duh). It is open source and make with python and shell.

WebApp Maker make web apps (Duh). It is open source and make with python and shell. This app can take any website and turn it into an app. I highly recommend turning these few websites into webapps:

2 Jan 09, 2022
pypyr task-runner cli & api for automation pipelines.

pypyr task-runner cli & api for automation pipelines. Automate anything by combining commands, different scripts in different languages & applications into one pipeline process.

pypyr 471 Dec 15, 2022
Centauro - a command line tool with some network management functionality

Centauro Ferramenta de rede O Centauro é uma ferramenta de linha de comando com

1 Jan 01, 2022
A Python-based Wordle solver and CLI player

Wordle A Python-based Wordle solver and CLI player This was created using Python 3.9.7. SPOILER ALERT: the data directory contains spoilers for upcomi

Will Fitzgerald 1 Jul 24, 2022
A Bot Which Send Automatically Commands To Karuta Hub to Gain it's Currency

A Bot Which Send Automatically Commands To Karuta Hub to Gain it's Currency

HarshalWaykole 1 Feb 09, 2022
An ZFS administration tool inspired on Midnight commander

ZC - ZFS Commander An ZFS administration tool inspired on Midnight commander Work in Progress Description ZFS Commander is a simple front-end for the

34 Dec 07, 2022
Microsoft Azure CLI - Azure Command-Line Interface

A great cloud needs great tools; we're excited to introduce Azure CLI, our next generation multi-platform command line experience for Azure.

Microsoft Azure 3.4k Dec 30, 2022
A set of libraries and functions for simplifying automating Cisco devices through SecureCRT.

This is a set of libraries for automating Cisco devices (and to a lesser extent, bash prompts) over ssh/telnet in SecureCRT.

Matthew Spangler 7 Mar 30, 2022
A python-based terminal application that displays current cryptocurrency prices

CryptoAssetPrices A python-based terminal application that displays current cryptocurrency prices. Covered Cryptocurrencies Bitcoin (BTC) Ethereum (ET

3 Apr 21, 2022
A simple terminal-based localhost chat application written in python

Chat House A simple terminal-based localhost chat application written in python How to Use? Clone the repo git clone https://github.com/heksadecimal/c

Heks 10 Nov 09, 2021