Skip to main content

A Django app to help make building APIs great.

Project description

Django Dans API Toolkit

Lint Test Types codecov

Python Versions Django Versions PyPI Version Downloads License Codacy grade Code style

Description

Django Dans API Toolkit is a Django app to help make building APIs great.

It provides a collection of tools for common API tasks, intended to complement Django Rest Framework (DRF) rather than replace it.

Available on PyPi

Features

The toolkit includes:

  • API Response Handler (api_response_handler.py): Standardizes API response formats with enhanced error logging that automatically captures stack traces for Exception objects. Now with robust error message extraction that supports arbitrarily nested error structures (for example, DRF ValidationError details like { 'user': { 'profile': { 'email': ['Invalid email.'] } }, 'password': ['Too short.'] }). The handler will always surface the first relevant string error it finds, even if it is several levels deep in a dict or list, ensuring user-friendly error messages regardless of how complex the error structure is. Type annotations and docstrings have also been improved for clarity and best practices.
  • API Response Renderer (api_response_renderer.py): Ensures consistent response rendering across your APIs.
  • Base Serializer (serializers/base.py): Simplifies serializer creation with masking and reference functionality.

Quick Start

Installation

  1. Install the package via pip:

    pip install django-dans-api-toolkit
    
  2. Add django_dans_api_toolkit to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        ...
        'django_dans_api_toolkit',
    ]
    
  3. Run migrations to update your database schema:

    python manage.py migrate
    
  4. Use the provided tools in your views and serializers.

Example Usage

API Response Handler

from django_dans_api_toolkit.api_response_handler import ApiResponseHandler
from django.core.exceptions import ValidationError

def my_view(request):
    handler = ApiResponseHandler()
    
    try:
        # Your API logic here
        serializer.is_valid(raise_exception=True)
        data = {"key": "value"}
        return handler.response_success(results=data)
    except ValidationError as e:
        # Exception objects automatically get full stack traces in logs! 
        # Error message extraction also supports arbitrarily nested error structures.
        return handler.response_error(
            message="Validation failed",
            error=e, 
            error_fields=serializer.errors
        )

Base Serializer

from django_dans_api_toolkit.serializers.base import BaseSerializer
from myapp.models import MyModel

class MyModelSerializer(BaseSerializer):
    class Meta:
        model = MyModel
        fields = '__all__'
        ref_fields = ['field1', 'field2']
        masked_fields = ['field3']

API Response Renderer

Ensure DEFAULT_AUTHENTICATION_CLASSES and DEFAULT_PAGINATION_CLASS are set in your settings.py for proper API functionality. Set the renderer itself as well.

Example configuration:

REST_FRAMEWORK = {
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
    "PAGE_SIZE": 20,
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework.authentication.TokenAuthentication",
    ),
}

Requirements

  • Python 3.10 - 3.11
  • Django 3.1 or higher
  • Django Rest Framework
    • NOTE: not only must you have this installed, you must have set DEFAULT_AUTHENTICATION_CLASSES and DEFAULT_PAGINATION_CLASS in your settings.py to work with the APIs properly. An example config would be:

https://danielnazarian.com
Copyright 2025 © Daniel Nazarian.

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

django-dans-api-toolkit-1.2.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_dans_api_toolkit-1.2.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file django-dans-api-toolkit-1.2.0.tar.gz.

File metadata

  • Download URL: django-dans-api-toolkit-1.2.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for django-dans-api-toolkit-1.2.0.tar.gz
Algorithm Hash digest
SHA256 24269fd7c0a2372a45c3990537a1d5a90769924700f09f3b19abe0a130ead4dc
MD5 cb7e7446d033ccabef0873c38f119962
BLAKE2b-256 b3deb96535efd37ca632e4d034b542172c995c249f2c4b49d08819b0267173fb

See more details on using hashes here.

File details

Details for the file django_dans_api_toolkit-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dans_api_toolkit-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fffadd33717d2d9c3e20339e31f5efa2872e651d2a96272f249c9637afe9fa3
MD5 e1345339bcb4a15aebad209b86321105
BLAKE2b-256 2fdbff4bd1eafd817da8d5bc2fcdfb660821288c1ae835dd775371e851ec00fc

See more details on using hashes here.

Supported by

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