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.1.0.tar.gz (19.2 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.1.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-dans-api-toolkit-1.1.0.tar.gz
  • Upload date:
  • Size: 19.2 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.1.0.tar.gz
Algorithm Hash digest
SHA256 b0b97d038069c34432d11c723c3a8d7097f33e71d8141d38229a81f336869ea2
MD5 62d50ea3fcf73a27027c30896b7017d2
BLAKE2b-256 525705f9a6fb758e4e28ef4617ad8631190efe61b8a3a4c401211f59c144628d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_dans_api_toolkit-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d0150cd503813e690e5d6d232ae76df8f15db6e4e05d60c88ac318a23a13339
MD5 a215e1e7b545438344227a16731695ab
BLAKE2b-256 a80767998bfd2099489bccd1876f89e7c9e6dfdb0e1871628b8d7c5d9f73b076

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