A lightweight, framework-agnostic Python package for standardized API responses.
Project description
apiformat
A lightweight, framework-agnostic Python package that enforces a single, consistent response format across all API endpoints.
Installation
pip install apiformat
Core Module (No Dependencies)
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
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
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
apiformat-0.1.0.tar.gz
(4.9 kB
view details)
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 apiformat-0.1.0.tar.gz.
File metadata
- Download URL: apiformat-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d7e484002039ae673626a1aae2caabb76f970d26b0d5ffe98fead7ca9651f75
|
|
| MD5 |
b622791ef93be002a756d3b219464c2b
|
|
| BLAKE2b-256 |
4ae13dd0bbd23fc65bbad81c3d265b775952c700a0efba5f4d6219c3ebdad7ea
|
File details
Details for the file apiformat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: apiformat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
026e31b905f55a2709dc21ed3b3dd36e76a60000a705d46d6385b00278959a06
|
|
| MD5 |
d448947768804b23042e9b77efc73423
|
|
| BLAKE2b-256 |
f199556c343029a30108af3e69dae3347f32a3ab67088af0c154ae662b8b82e2
|