A Django REST Framework utility for standardized API responses, pagination, and exception handling.
Project description
drf-standardized-responses
A Django REST Framework utility for standardized API responses, pagination, and exception handling.
Features
- Consistent Response Format: All API responses follow a standardized structure (
success,message,data, with optionalmetaanderrorsfields). - Custom Renderer: A
StandardResponseRendererthat formats all responses consistently, preventing common issues like double-wrapping. - Standardized Pagination:
StandardPaginationclass that provides rich metadata, includingcount,total_pages, andcurrent_page. - Custom Exception Handler: A handler that catches DRF exceptions and formats them into the standard error response structure.
- Helper Functions: Utilities to easily create standardized success and error responses.
Standard Response Structure
Success Response
{
"success": true,
"message": "Operation successful",
"data": {
"id": 1,
"name": "Item 1"
}
}
Success Response with Pagination
{
"success": true,
"message": "Operation successful",
"data": [
{
"id": 1,
"name": "Item 1"
}
],
"meta": {
"pagination": {
"count": 100,
"next": "http://api.example.org/items?page=2",
"previous": null,
"total_pages": 10,
"current_page": 1,
"page_size": 10
}
}
}
Error Response
{
"success": false,
"message": "Validation failed",
"data": {},
"errors": {
"name": ["This field is required"],
"email": ["Enter a valid email address"]
}
}
Installation
pip install drf-standardized-responses
Quick Start
Add the following to your Django settings.py:
REST_FRAMEWORK = {
# Use the custom exception handler
'EXCEPTION_HANDLER': 'drf_standardized_responses.exceptions.standardized_exception_handler',
# Use the custom pagination class
'DEFAULT_PAGINATION_CLASS': 'drf_standardized_responses.pagination.StandardPagination',
# Use the custom renderer
'DEFAULT_RENDERER_CLASSES': [
'drf_standardized_responses.renderers.StandardResponseRenderer',
# Add other renderers if needed, like BrowsableAPIRenderer
'rest_framework.renderers.BrowsableAPIRenderer',
],
}
API Reference
StandardResponse
A utility class for creating standardized API responses.
from drf_standardized_responses.responses import StandardResponse
# Create a success response
response = StandardResponse.success(
data={"key": "value"},
message="Data retrieved successfully",
meta={"custom_meta": "value"},
status_code=200
)
# Create an error response
response = StandardResponse.error(
message="Validation failed",
errors={"field": ["This field is required"]},
status_code=400
)
StandardResponseRenderer
This renderer automatically wraps your API responses in the standard structure. It correctly handles both error and success responses.
StandardPagination
A pagination class that integrates with the standardized response format to provide consistent pagination metadata.
standardized_exception_handler
An exception handler that catches DRF exceptions and formats them into standardized error responses.
Testing
Run the test suite:
pytest
License
BSD-3-Clause
Author
Project details
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 drf_standardized_responses-0.1.3.tar.gz.
File metadata
- Download URL: drf_standardized_responses-0.1.3.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cde16442876c244d8f15ed5f38e25558c2811b0f8e57a1ac26c97bbdbdb1aacf
|
|
| MD5 |
2e0ece5f1637c0e01bc5087a745336d8
|
|
| BLAKE2b-256 |
8a91b4ebb77c0c1332fc2c97632f6eff16f4b4fc828502a36284f3a0fd7844a6
|
File details
Details for the file drf_standardized_responses-0.1.3-py3-none-any.whl.
File metadata
- Download URL: drf_standardized_responses-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8457c798165e2285d36823db3bf3cf2d1a18dbe680b18f7e040d944680482e4
|
|
| MD5 |
a33204d59659117dd30113111c6733d1
|
|
| BLAKE2b-256 |
8cb70c20ceef75450801a8e75f94226916ec847fa30dc2ee2ce273237422da07
|