Metadata-Version: 2.4
Name: apiformat
Version: 0.1.0
Summary: A lightweight, framework-agnostic Python package for standardized API responses.
Author: Core Developer
License: MIT
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: django
Requires-Dist: django; extra == "django"
Provides-Extra: fastapi
Requires-Dist: fastapi; extra == "fastapi"
Requires-Dist: pydantic; extra == "fastapi"
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Dynamic: license-file

# apiformat

A lightweight, framework-agnostic Python package that enforces a single, consistent response format across all API endpoints.

## Installation

```bash
pip install apiformat
```

## Core Module (No Dependencies)

```python
from apiformat import success, error

# Success response
response = success(
    data={"id": 1, "name": "John"},
    message="User created successfully"
)
# Output:
# {
#   "status": "success",
#   "message": "User created successfully",
#   "data": {"id": 1, "name": "John"},
#   "meta": {}
# }

# Error response
response = error(
    message="User not found",
    status_code=404
)
# Output:
# {
#   "status": "error",
#   "message": "User not found",
#   "data": None,
#   "meta": {}
# }
```

## Django Integration

```python
from apiformat.integrations.django import success_response, error_response

def create_user(request):
    user = User.objects.create(name="John")
    return success_response(
        data={"id": user.id, "name": user.name},
        message="User created",
        status=201
    )

def get_user(request, user_id):
    try:
        user = User.objects.get(id=user_id)
        return success_response(data={"id": user.id, "name": user.name})
    except User.DoesNotExist:
        return error_response(message="User not found", status=404)
```

## FastAPI Integration

```python
from fastapi import FastAPI
from apiformat.integrations.fastapi import success_response, error_response

app = FastAPI()

@app.post("/users")
async def create_user(name: str):
    user = User(name=name)
    user.save()
    return success_response(
        data={"id": user.id, "name": user.name},
        message="User created",
        status_code=201
    )

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    user = User.get_by_id(user_id)
    if not user:
        return error_response(message="User not found", status_code=404)
    return success_response(data={"id": user.id, "name": user.name})
```

## License

MIT
