Skip to main content

An essential package for structuring and optimizing your API responses.

Project description

API Response Shaper

License PyPI Release Pylint Score Supported Python Versions Supported Django Versions Pre-commit Open Issues Last Commit Languages Coverage

api-response-shaper is a Django middleware and decorator-based package that helps structure API responses in a consistent format. It includes dynamic configurations and various pre-built response formats, such as paginated responses, batch responses, minimal success responses, and error responses. This package allows developers to streamline response shaping for their Django REST Framework (DRF) APIs, ensuring a uniform response format across different API endpoints.

Features

  • Dynamic Middleware: Automatically structures API responses for successful and error cases.
  • Customizable Handlers: Easily switch between different response formats using decorators.
  • Pre-built Response Types: Supports a variety of response types, including:
    • Standard API responses
    • Paginated responses
    • Batch operation responses
    • Error responses
    • Authentication responses
    • Minimal success responses
  • Custom Response Handlers: Define custom success and error handlers for fine-tuned control.
  • Exclusion Paths: Skip specific routes from being processed by the middleware.
  • Django Version: Compatible with Django REST Framework (DRF) and Django middleware.

Project Detail

  • Language: Python >= 3.9
  • Framework: Django >= 4.2
  • Django REST Framework: >= 3.14

Documentation

The documentation is organized into the following sections:

Setup

Setting up the api-response-shaper is simple, folow these steps to setup:

  1. Install the Package:

To install the api-response-shaper, simply use pip:

$ pip install api-response-shaper
  1. Add to Installed Apps

Add response_shaper to your INSTALLED_APPS in your Django settings file:

INSTALLED_APPS = [
    # ...
    'response_shaper',
    # ...
]
  1. Configure Middleware:

Add DynamicResponseMiddleware middleware to the MIDDLEWARE list in your Django settings.py:

MIDDLEWARE = [
    # ...
    'response_shaper.middleware.DynamicResponseMiddleware',
    # ...
]

Once this middleware is added, all API responses will be dynamically structured. Whether it's a successful or an error response, the middleware ensures a consistent format across your API endpoints. You can customize the response format using settings or decorators for specific views, but by default, this middleware provides a standardized API response format. You can also configure the api-response-shaper for your project needs, for more details, please refer to the Settings section.


Usage

Decorators for Response Formatting

You can apply different response formats using decorators in your views. Available decorators:

  • @format_api_response: Applies the standard API response format.
  • @format_paginated_response: Applies the paginated response format.
  • @format_error_response: Applies the error response format.
  • @format_minimal_success_response: Applies the minimal success response format.
  • @format_batch_response: Applies the batch operation response format.
  • @format_auth_response: Applies the authentication response format.

Example usage:

from rest_framework.response import Response
from response_shaper.decorators import format_api_response, format_paginated_response

@format_api_response
def my_api_view(request):
    data = {"key": "value"}
    return Response(data, status=200)

@format_paginated_response
def paginated_view(request):
    paginated_data = {
        "data": [{"id": 1}, {"id": 2}],
        "page": 1,
        "total_pages": 5,
        "total_items": 50,
    }
    return Response(paginated_data, status=200)

Custom Success and Error Handlers

If you want more control over the response structure, you can define custom handlers. Use the RESPONSE_SHAPER settings to specify the paths to your custom handlers. for more details, please refer to the Settings.

For example, define a custom success handler in myapp.responses:

# myapp/responses.py
from rest_framework.response import Response

def custom_success_handler(response):
    return Response({
        "custom_status": "success",
        "code": response.status_code,
        "payload": response.data
    }, status=response.status_code)

Then configure this handler in your settings:

RESPONSE_SHAPER_SUCCESS_HANDLER = "path.to_your.custom_success_handler"

Built-In Response Types

  • Standard API Response:
api_response(
  success: bool = True,
  message: str = None,
  data: dict = None,
  errors: dict = None,
  status_code: int = 200
) -> Response

  • Paginated Response:
paginated_api_response(
  success: bool = True,
  data: list = None,
  page: int = None,
  total_pages: int = None,
  total_items: int = None,
  status_code: int = 200
) -> Response

  • Error Response:
error_api_response(
  message: str = None,
  errors: dict = None,
  error_code: str = None,
  status_code: int = 400
) -> Response

  • Minimal Success Response:
minimal_success_response(
  message: str = "Request successful",
  status_code: int = 200
) -> Response

  • Batch Operation Response:
batch_api_response(
  success: bool = True,
  results: list = None,
  errors: dict = None,
  status_code: int = 200
) -> Response`

  • Authentication Response:
auth_api_response(
  success: bool = True,
  message: str = None,
  token: str = None,
  user: dict = None,
  errors: dict = None,
  status_code: int = 200
) -> Response

Exception Handling

The middleware automatically handles Django exceptions and structures error responses for common errors:

  • ObjectDoesNotExist -> 404 Not Found
  • IntegrityError -> 400 Bad Request
  • ValidationError -> 400 Bad Request
  • Generic exceptions -> 500 Internal Server Error (with Traceback details if settings.DEBUG is True)

You can customize the exception response format by providing custom error handlers via the RESPONSE_SHAPER configuration.


Settings

In this section, we dive deep into the settings configuration and defaults.

Default configuration:

Here are the default settings that are automatically applied:

# settings.py

RESPONSE_SHAPER_DEBUG_MODE = False
RESPONSE_SHAPER_EXCLUDED_PATHS = ["/admin/", "/schema/swagger-ui/", "/schema/redoc/", "/schema/"]
RESPONSE_SHAPER_SUCCESS_HANDLER = ""
RESPONSE_SHAPER_ERROR_HANDLER = ""

RESPONSE_SHAPER_DEBUG_MODE

  • Type: bool
  • Description: When set to True, disables response shaping for debugging purposes.
  • Default: False

RESPONSE_SHAPER_EXCLUDED_PATHS

  • Type: List[str]
  • Description: A list of URL paths where the middleware will not shape responses. This is useful for excluding admin or documentation routes from being processed, ensuring that those paths retain their original behavior.
  • Default: ["/admin/", "/schema/swagger-ui/", "/schema/redoc/", "/schema/"]

RESPONSE_SHAPER_SUCCESS_HANDLER

  • Type: str
  • Description: Path to the custom handler to manage successful responses. you can specify a custom handler if you want to modify the success response format.
  • Default: Default Success handler in DynamicResponseMiddleware

RESPONSE_SHAPER_ERROR_HANDLER

  • Type: str
  • Description: Path to the custom handler to manage error responses. Similar to the success handler, it allows you to provide your own handler to modify the error response format if the default behavior does not meet your needs.
  • Default: Default Error handler in DynamicResponseMiddleware

Thank you for using api-response-shaper. We hope this package enhances your Django application's API responses. If you have any questions or issues, feel free to open an issue on our GitHub repository.

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

api_response_shaper-1.1.0.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

api_response_shaper-1.1.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file api_response_shaper-1.1.0.tar.gz.

File metadata

  • Download URL: api_response_shaper-1.1.0.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.7 Linux/6.8.0-1015-azure

File hashes

Hashes for api_response_shaper-1.1.0.tar.gz
Algorithm Hash digest
SHA256 eaf8e7d531701a27e23f37ac77d85f4b4b3bfc0e9a7d7fd60d2eebb247a8ce73
MD5 e8c213e8d81408382e985a1543e42ec3
BLAKE2b-256 818cd2a4a72b1bd33d875165b4d5862abde869fb5bfdafa70cfcaad93b41f6ef

See more details on using hashes here.

File details

Details for the file api_response_shaper-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for api_response_shaper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 682de5b8f4dcd431f14bd05563f02bff95369aacd6d723d26cdb9a92f7fe9d7d
MD5 40212a2c675a7e989a2ddd52804f6d35
BLAKE2b-256 4cad05f56aff8d93aee27229ed8cd3a8e9e6f2c11f9ae4a83f5fd8bbe4a8a21d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page