Skip to main content

fsrest

fsrest extracts reusable single-resource REST CRUD orchestration from application code. It is framework-independent: request/response objects are Pydantic models, while persistence is supplied through a small DAO protocol.

Install

pip install fsrest

Python 3.9+ and Pydantic 1.10/2.x are supported.

Usage

Bind a DAO to RestCrudLogicBase. Request schemas provide the small conversion methods needed to turn HTTP-facing data into DAO fields.

from pydantic import BaseModel
from fsrest import RestCrudLogicBase, RestPageReqSchema, RestPageRespSchema

class Item(BaseModel):
    id: str
    name: str

class Filters(BaseModel):
    name: str | None = None

class Ordering(BaseModel):
    field: str = "id"

class PageData(BaseModel):
    items: list[Item]
    total: int

class ListQuery(RestPageReqSchema):
    name: str | None = None

    def build_filters(self) -> Filters:
        return Filters(name=self.name)

    def build_ordering(self) -> Ordering:
        return Ordering()

    def build_response(self, *, page_data: PageData) -> RestPageRespSchema[Item]:
        return RestPageRespSchema[Item](
            items=page_data.items,
            total=page_data.total,
            page=self.page,
            page_size=self.page_size,
        )

class ItemDao:
    @classmethod
    def list_schema_page(cls, *, filters, ordering, page, page_size) -> PageData:
        ...

    # Also implement get_schema_by_id, create_schema,
    # update_schema_by_id, and delete_by_id.

class ItemLogic(RestCrudLogicBase):
    dao_rest_crud = ItemDao

The library raises RestApiError for missing records and failed deletes. To integrate with an application's existing exception middleware, subclass it and bind error_class:

class ApplicationApiError(RestApiError):
    error_code = 400455

class ItemLogic(RestCrudLogicBase):
    dao_rest_crud = ItemDao
    error_class = ApplicationApiError

Development and publishing

From the pytools repository root, use the unified release script:

python make.py fsrest test
python make.py fsrest build
python make.py fsrest publish

publish uploads the artifacts under fsrest/dist/ using the PyPI credentials configured in ~/.pypirc. Before publishing a new release, update the version in pyproject.toml, run tests, and build fresh artifacts.

Download files

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

Source Distribution

fsrest-0.1.0.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fsrest-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fsrest-0.1.0.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.2

File hashes

Hashes for fsrest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0fd56eb583c903880e0536ae8cf5b57b59b302a23481f9b668d18c7542fa3085
MD5 e8c3f5bc33b677a25c031332bcfc0788
BLAKE2b-256 39f017cac5e95936c763623c7f36198e7466708f7e83dcb95066adf7631e328a

See more details on using hashes here.

File details

Details for the file fsrest-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fsrest-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.2

File hashes

Hashes for fsrest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9618610dbd992153447e63e7244eb5c8e3b154f22bde77b00d52fb3a6e48479
MD5 51341c8239690ee02b9ab04d8e1b8a74
BLAKE2b-256 9d6d4bbfc4697cad884c7a2d71106849875b1174f0fc1abba13d4548418f63c2

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

This release

0.1.0 This release

2 files

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