Skip to main content

FastAPI-like interface plugin for Flask

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flastapi-0.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

flastapi-0.1.0-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flastapi-0.1.0.tar.gz.

File metadata

  • Download URL: flastapi-0.1.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for flastapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9e264258c453b030c6363e001f4e6642e3a73ed10adf6c2939fa5feb3a448c7b
MD5 73c78ec4026f1fc0bf78b12cf4dacbcd
BLAKE2b-256 881a1cc382cde671d02016b4c6d6392f1f295c47bc5089a55c3befbe95217e5d

See more details on using hashes here.

File details

Details for the file flastapi-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: flastapi-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for flastapi-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 94d0fdd220e9e64a7eb46574dd9abf5bc67ba8299589258633db2f0c84c4624c
MD5 b9a815756c5a0d512d4996238210e257
BLAKE2b-256 caa553f1db0b40cc2c2e350440f6854612438f4956488405cc9c309e9b950903

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page