Metadata-Version: 2.5
Name: fsrest
Version: 0.1.0
Summary: Reusable, framework-agnostic REST CRUD logic for Pydantic applications
Project-URL: Homepage, https://github.com/pydtools/fsrest
Project-URL: Repository, https://github.com/pydtools/fsrest
Project-URL: Issues, https://github.com/pydtools/fsrest/issues
Author-email: huoyinghui <hyhlinux@gmail.com>
License: MIT License
        
        Copyright (c) 2026 huoyinghui
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
License-File: LICENSE
Keywords: crud,dao,fastapi,pydantic,rest
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: pydantic<3,>=1.10
Description-Content-Type: text/markdown

# 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

```bash
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.

```python
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`:

```python
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:

```bash
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.

