Metadata-Version: 2.4
Name: pronto-rest
Version: 0.0.1
Summary: Shared API response envelope for FastAPI projects
Author-email: Til <info@justtil.dev>
License: Copyright (c) 2026 Til Schwarze
        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: api,fastapi,pronto,pydantic,response
Requires-Python: >=3.10
Requires-Dist: pydantic
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Description-Content-Type: text/markdown

# pronto-rest

Shared API utilities for FastAPI projects.

## What's included

**`pronto_rest.v1`** — stable public API:

- `ApiResponse[T]` — generic response envelope that every endpoint returns. Carries either a typed `data` payload (on success) or an `ApiError` (on failure), never both.
- `ApiError` — structured error model with a machine-readable `code`, a human-readable `message`, and an optional `details` dict.
- `EmptyData` — use as the `data` type when an operation succeeds but returns no domain payload.

## Installation

```bash
pip install pronto-rest
```

Or, for local development with editable install:

```bash
pip install -e ".[dev]"
```

## Usage

```python
from pronto_rest.v1 import ApiResponse, ApiError

# Success response
response = ApiResponse.ok(
    data={"id": "123", "name": "Alice"},
    operation="create_user",
    endpoint="/api/v1/users/create",
)

# Error response
response = ApiResponse.fail(
    code="USER_NOT_FOUND",
    message="No user with that ID exists.",
    operation="get_user",
    endpoint="/api/v1/users/123",
    details={"user_id": "123"},
)
```

### With FastAPI

```python
from fastapi import APIRouter
from pronto_rest.v1 import ApiResponse, EmptyData

router = APIRouter()

@router.post("/users/create", response_model=ApiResponse[UserOut])
def create_user(body: UserIn) -> ApiResponse[UserOut]:
    user = db.create(body)
    return ApiResponse.ok(
        data=UserOut.model_validate(user),
        operation="create_user",
        endpoint="/api/v1/users/create",
    )
```

### Response envelope shape

Every response shares this envelope:

| Field | Type | Description |
|---|---|---|
| `api_version` | `str` | Always `"1.0"` |
| `status` | `"success" \| "error"` | Outcome of the operation |
| `correlation_id` | `str` (UUID) | Trace ID; echoed from `X-Correlation-ID` header if present |
| `timestamp` | `datetime` | UTC timestamp of when the response was generated |
| `operation` | `str` | Logical operation name (e.g. `create_user`) |
| `endpoint` | `str` | Request path (e.g. `/api/v1/users/create`) |
| `data` | `T \| null` | Response payload; `null` on error |
| `error` | `ApiError \| null` | Error details; `null` on success |

`status="success"` requires `data` and forbids `error`. `status="error"` requires `error` and forbids `data`.

## Development

Run tests:

```bash
pytest
```

## Versioning

Versions are derived from git tags via [hatch-vcs](https://github.com/ofek/hatch-vcs). The file `src/pronto_rest/_version.py` is auto-generated at build time and is not tracked in version control — the git tag is the source of truth.

To release a new version, tag a commit and build:

```bash
git tag v0.2.0
hatch build
```
