FastAPI-HyperModel
Simple hypermedia for FastAPI
Documentation: https://jtc42.github.io/fastapi-hypermodel/
Source Code: https://github.com/jtc42/fastapi-hypermodel
FastAPI-HyperModel is a FastAPI + Pydantic extension for simplifying hypermedia-driven API development.
This module adds a new Pydantic model base-class, supporting dynamic href
generation based on object data.
Model | Response |
---|---|
class ItemSummary(HyperModel):
name: str
id: str
href = UrlFor(
"read_item", {"item_id": "<id>"}
) |
{
"name": "Foo",
"id": "item01",
"href": "/items/item01"
} |
class ItemSummary(HyperModel):
name: str
id: str
link = HALFor(
"read_item", {"item_id": "<id>"},
description="Read an item"
) |
{
"name": "Foo",
"id": "item01",
"link": {
"href": "/items/item01",
"method": "GET",
"description": "Read an item"
}
} |
Installation
pip install fastapi-hypermodel
Limitations
Currently, query parameters will not resolve correctly. When generating a resource URL, ensure all parameters passed are path parameters, not query parameters.
This is an upstream issue, being tracked here.
Attributions
Some functionality is based on Flask-Marshmallow URLFor
class.