fsrest
fsrest provides reusable single-resource REST CRUD orchestration with action
names familiar to Django REST Framework users. It is framework-independent:
request and response objects are Pydantic models, while persistence is supplied
through a small repository protocol.
Designed and developed by Codex.
Install
pip install fsrest
Python 3.9+ and Pydantic 1.10/2.x are supported.
DRF-style actions
CrudViewSet follows DRF's standard action vocabulary:
listretrievecreateupdatepartial_updatedestroy
It is deliberately not an HTTP view and does not depend on Django. A FastAPI, Flask, Django, or other framework adapter can call these actions after request validation.
Bind a repository to CrudViewSet. Request schemas provide the conversion
methods needed to turn HTTP-facing data into repository fields.
from pydantic import BaseModel
from fsrest import CrudViewSet, PageRequest, PageResponse
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(PageRequest):
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) -> PageResponse[Item]:
return PageResponse[Item](
items=page_data.items,
total=page_data.total,
page=self.page,
page_size=self.page_size,
)
class ItemRepository:
@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 ItemViewSet(CrudViewSet):
repository = ItemRepository
Framework code can now use familiar action names:
page = ItemViewSet.list(query=query)
item = ItemViewSet.retrieve(query=lookup)
created = ItemViewSet.create(payload=create_payload)
updated = ItemViewSet.update(payload=update_payload)
patched = ItemViewSet.partial_update(payload=patch_payload)
result = ItemViewSet.destroy(payload=delete_payload)
Override perform_create, perform_update, or perform_destroy to add audit
logging, events, or transaction behavior around persistence.
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 ItemViewSet(CrudViewSet):
repository = ItemRepository
error_class = ApplicationApiError
Migrating from 0.1
The 0.1 API remains available for compatibility. New code should prefer these names:
| 0.1 API | 0.2 API |
|---|---|
RestCrudLogicBase |
CrudViewSet |
dao_rest_crud |
repository |
list_items |
list |
get_item |
retrieve |
create_item |
create |
update_item |
update |
delete_item |
destroy |
RestPageReqSchema |
PageRequest |
RestPageRespSchema |
PageResponse |
RestDeleteRespSchema |
DestroyResponse |
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
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 fsrest-0.2.0.tar.gz.
File metadata
- Download URL: fsrest-0.2.0.tar.gz
- Upload date:
- Size: 32.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7afaf9c33904309d7ba8cb60354ceb28ae64207fbeebe456bad03f9ea9682b10
|
|
| MD5 |
5e804fdcb941ad6124d221934635891f
|
|
| BLAKE2b-256 |
b587d8e33a6fbfcda6ffbcb73af7c5a812b4893ad451eec0b8298a15cd6666b1
|
File details
Details for the file fsrest-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fsrest-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
178e8ca7c8a053d9f578558a92a4023655fb29554b748b7c3c2ae4c49ee1deea
|
|
| MD5 |
e31e2bf1173052efe28fe09938e7aa4c
|
|
| BLAKE2b-256 |
8b4b0a87f864cecc0079f7fb69976862d12f003f89cbe5f99686d6fe62ff06ad
|