Build simple APIs on top of falcon with pydantic data models (FastAPI style), automated OpenAPI spec generation included
Project description
falcon-swoop
Easy-to-define typed API resources for falcon based on pydantic models bringing some FastAPI style convenience to your favorite web framework. Automatic OpenAPI doc generation included. It is fully opt-in: Use it for all your resources, start attaching typed operations to an existing app or just add a single typed resource to your project. OpenAPI documentation can also be added manually for old or very complex operations that don't fit in this framework.
pip install falcon-swoop
(compatible with Falcon 4.x and Pydantic 2.x)
User Guide
Quickstart
To use falcon-swoop follow these steps:
- subclass from
SwoopResource - create a method for your API operation and type-hint it with Pydantic classes for input and output
- decorate that method with
@operation - wrap the falcon
AppwithSwoopAppand register the swoop resources there
import falcon.asgi
from falcon_swoop import SwoopApp, SwoopResource, operation
from pydantic import BaseModel, Field
def store_message_in_db(author: str, text: str) -> str:
# implement storage in database here!
return "new_id"
class CreateMessageInput(BaseModel):
author: str
text: str = Field(min_length=20)
class CreateMessageOutput(BaseModel):
message_uid: str
class NewMessageController(SwoopResource):
def __init__(self):
super().__init__(route="/api/message")
@operation(method="POST")
async def create_message(self, message: CreateMessageInput) -> CreateMessageOutput:
message_uid = store_message_in_db(message.author, message.text)
return CreateMessageOutput(message_uid=message_uid)
def build_app() -> falcon.asgi.App:
app = falcon.asgi.App()
swoop = SwoopApp(
app,
title="Example App",
version="0.1.0",
spec_json_route="/api/openapi.json",
spec_swagger_route="/api/swagger.html",
)
swoop.add_route(NewMessageController)
return app
Once the application is running, new JSON data can be submitted on POST /message according to CreateMessageInput,
similarly the application will respond with JSON according to CreateMessageOutput. The OpenAPI specification can then
be accessed as JSON on /api/openapi.json or human-readable at /api/swagger.html.
This concludes the basics, keep reading for more details!
Tips
- falcon-swoop works for both synchronous and asynchronous falcon applications
- for operations that are too complicated for falcon-swoop, but should still show up in the OpenAPI specification, the
@operation_docdecorator can be used to provide manual documentation on conventional falcon responder methods, such ason_get,on_postand so on - use
header_param,query_paramandpath_paramto model inputs other than the HTTP body - for binary input and output use
OpBinary(orOpAsgiBinaryfor async operations) - for more finegrained response control (status code, headers, ...) return
OpOutput[SomeModel]orOpOutput[OpBinary] - when access to the entire falcon request and response is required, add an input with
OpContext(orOpAsgiContextfor async operations)
Full Example
Check out src/falcon_swoop_example for a full example. To run it gunicorn needs to be
installed, then the application can be started with the commands below. Check the logs on which routes the OpenAPI
specification and the Swagger UI can be accessed.
cd src
./falcon_swoop_example.sh
Development Guide
pip install . # to install main dependencies
pip install -e ".[dev]" # to install main and dev dependencies
ruff check --fix
ruff format
mypy
pytest -v
nox # requires separate install
hatchling build -t wheel
Open Items
- add security schemes to openapi docgen
- warning for pydantic models that declare fields of type bytes
- handle missing annotations for input, params, context and return value
- add support for (de)serialization to yaml and other formats
- utility spec class for Multipart form data
- makes parsing easy
- integrates with OpenAPI generation
- make sure snake_case to camelCase works easily (especially for query and path params)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file falcon_swoop-4.0.0-py3-none-any.whl.
File metadata
- Download URL: falcon_swoop-4.0.0-py3-none-any.whl
- Upload date:
- Size: 31.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45a332a02f6fdb09b66fdf7c8f36631948abfe4fa38cc7ff69308665be503709
|
|
| MD5 |
c7913445d1dc84db5ad06cc5c4ec2450
|
|
| BLAKE2b-256 |
def2b327cdbf6c0db5e9079bf03c769b504e8ea2ca023b7154cef7ab8da8352f
|
Provenance
The following attestation bundles were made for falcon_swoop-4.0.0-py3-none-any.whl:
Publisher:
ci.yml on osanj/falcon-swoop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
falcon_swoop-4.0.0-py3-none-any.whl -
Subject digest:
45a332a02f6fdb09b66fdf7c8f36631948abfe4fa38cc7ff69308665be503709 - Sigstore transparency entry: 1097415756
- Sigstore integration time:
-
Permalink:
osanj/falcon-swoop@a73c82cee14eb692f15be74865eb72a7076c67ce -
Branch / Tag:
refs/tags/4.0.0 - Owner: https://github.com/osanj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a73c82cee14eb692f15be74865eb72a7076c67ce -
Trigger Event:
release
-
Statement type: