Skip to main content

Table of contents

Introduction

Flastapi is a small flask plugin to enable a Fastapi-like interface to build API endpoints using pydantic.

Current features are:

  • path parameters using flask paths
  • query parameters
  • body parameters using pydantic
  • depends (including context dependencies)
  • dependency_overrides

Fastapi did a great job at integrating pydantic, as a marshaller for API endpoints, in an intuitive way. With this library I wanted to expose these capabilities in flast as well, for those who haven't found the ability to transition to newer techs (Or those who have a hard time dealing with change ;) )

Quickstart

from flask import Flask
from flastapi import FlastAPI, Router

app = Flask(__name__)
flastapi = FlastAPI(app)
router = Router("my_router")

@router.get("/index"):
def index():
    return {}

flastapi.add_router(router)

app.run()

API Overview

Path parameters

Both Flask and Fastapi offer similar path operations. As such, it didn't make sense to build a translation layer from a Fastapi notation to a Flask notation.

In short: We'll stick to flask path notations :)

Example endpoint

@router.get("/test/<int:some_param>")
def index(some_param: int):
    return {"some_param": some_param}

Example call

>>> client.get("/test/1")
{"some_param": 1}

Query parameters

Parameters annotated with builtin base types will automatically be flagged as query parameters. It's possible to gather query parameters in a pydantic model, check out the "Query dependency" section for more info.

Example endpoint

@router.get("/test")
def index(some_param: int):
    return {"some_param": some_param}

Example call

>>> client.get("/test?some_param=1")
{"some_param": 1}

Body parameters

Parameters annotated with pydantic models will automatically be flagged as json typed body parameters.

Example endpoint

class SomeParam(BaseModel):
    some_int: int


@router.post("/test")
def index(some_param: SomeParam):
    return some_param

Example call

>>> client.post("/test", json={"some_int": 1})
{"some_int": 1}

Multi body parameters

It's possible to annotate multiple pydantic models. If this is the case, the parameter names will be used as a lookup key in the json body.

Example endpoint

class SomeParam(BaseModel):
    some_int: int


class AnotherParam(BaseModel):
    some_str: str


@router.post("/test")
def index(some_param: SomeParam, another_param: AnotherParam):
    return [SomeParam, AnotherParam]

Example call

>>> client.post("/test", json={
    "some_param": {"some_int": 1},
    "another_param": {"some_str": "blah"}
})
[{"some_int": 1}, {"some_str": "blah"}]

Query dependency

If you'd like to group your query parameters in a pydantic model (or load them through another function), you can use a dependency.

from flastapi import Depends


class SomeParam(BaseModel):
    some_int: int


@router.get("/test")
def index(some_param: SomeParam = Depends(SomeParam)):
    return some_param

Example call

>>> client.get("/test?some_int=1")
{"some_int": 1}

Context dependency

A dependency also supports contexts, if you'd like a context to be started before handling the request, and closed after the request is handled.

from flastapi import Depends


def get_session():
    engine = create_engine("sqlite:////tmp/some.db")
    with Session(engine) as session:
        yield session


@router.get("/test")
def index(session: Session = Depends(get_session)):
    return {}

Testing dependencies

Overrides

You can override dependencies for your unit tests by replacing the wanted dependency with the one you'd like to run in your tests

Code

from flastapi import Depends


def get_session():
    engine = create_engine("sqlite:////tmp/some.db")
    with Session(engine) as session:
        yield session


@my_router.get("/test")
def index(session: Session = Depends(get_session)):
    return {}

Tests

from pytest import fixture
from flastapi import FlastAPI

from my_project import get_session, my_router


def get_test_session():
    engine = create_engine("sqlite:////tmp/another.db")
    with Session(engine) as session:
        yield session


@pytest.fixture
def app():
    app = Flask(__name__)
    flastapi = FlastAPI()
    flastapi.add_router(my_router)

    flastapi.dependency_overrides[get_session] = get_test_session

Using requests as test client

If you'd like to use requests as test client, check out Requests-flask-adapter

Roadmap

Stuff I'd still like to add

  • Response type
  • openAPI/Swagger/ReDoc support
  • I need to check out how this whole typing thing works in IDEs (Sorry, I'm a text editor kinda guy)

Requesting features

If you feel like stuff is missing, feel free to open an issue to request features. I'm but a poor programmer, fiddling for fun in his evenings, so I'll do my best to facilitate.

Metadata

Release files for flastapi 0.1.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 flastapi 0.1.0
File Size Uploaded Provenance
flastapi-0.1.0.tar.gz 10.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for flastapi 0.1.0
File Interpreter ABI Platform
flastapi-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.9 kB

Release files / flastapi-0.1.0.tar.gz

Download URL flastapi-0.1.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9e264258c453b030c6363e001f4e6642e3a73ed10adf6c2939fa5feb3a448c7b
BLAKE2b-256 checksum
How to use checksums
881a1cc382cde671d02016b4c6d6392f1f295c47bc5089a55c3befbe95217e5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / flastapi-0.1.0-py2.py3-none-any.whl

Download URL flastapi-0.1.0-py2.py3-none-any.whl
Size 8.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
94d0fdd220e9e64a7eb46574dd9abf5bc67ba8299589258633db2f0c84c4624c
BLAKE2b-256 checksum
How to use checksums
caa553f1db0b40cc2c2e350440f6854612438f4956488405cc9c309e9b950903
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Security for flastapi 0.1.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.

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