Skip to main content

A collection of tools to build function based Django APIs

Project description

Django API Decorator

A collection of tools to build function based Django APIs.

Warning This project is still in early development. Expect breaking changes.

Installation

Django API Decorator can be installed from PyPI:

pip install django-api-decorator

Usage

The main interface of this library is the @api decorator. This handles input and output from your view, according to type annotations on the view. Pydantic is used to handle most of the encoding and decoding, but you are not limited to use pydantic models for your types. You can use any type supported by pydantic, from simple types to dataclasses and typed dicts.

Here's a simple example:

@api(method="GET")
def list_some_numbers(request: HttpRequest) -> list[int]:
    return [1, 2, 3, 4]

Under the hood the @api decorator will encode the list of numbers ot JSON and wrap it up in a response object for Django to handle like any other response.

You can also specify query parameters, that will be decoded according to the specified type annotations:

@api(method="GET", query_params=["count"])
def list_some_numbers(request: HttpRequest, count: int) -> list[int]:
    return [random.randint(0, 10) for _ in range(count)]

Here the decorator will extract the count query paramter from the request and make sure it's a valid integer.

The decorator can also decode the request body for you:

@api(method="POST")
def sum_of_numbers(request: HttpRequest, body: list[int]) -> int:
    return sum(body)

The views produced by the decorator are plain Django views and should be added in your urls module just like any other view:

urlpatterns = [
    path("/api/numbers/", list_some_numbers, name="list-some-numbers"),
]

If you want to handle multiple methods on the same url a method_router helper function is provided, which can be used like this:

urlpatterns = [
    path(
        "/api/numbers/",
        method_router(
            GET=list_some_numbers,
            POST=...
        ),
        name="list-some-numbers",
    ),
]

OpenAPI specification

This library can also generate an OpenAPI specification from your views. This is done by inspecting the urlpatterns of the Django project, finding all views using the @api decorator. The schema for the specification is generated using pydantic, so for details about how different types are treated see Pydantic's documentation.

The specification is generated using the generate_api_schemas management command.

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_api_decorator-0.4.1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

django_api_decorator-0.4.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file django_api_decorator-0.4.1.tar.gz.

File metadata

  • Download URL: django_api_decorator-0.4.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for django_api_decorator-0.4.1.tar.gz
Algorithm Hash digest
SHA256 2e531b9a7f8b550af3b725848a6e47b808d3f492e91885af8a7d3b7a32978b77
MD5 22eb853c33bb4591db4312eaadb7c111
BLAKE2b-256 205846040ddba27044df5502de3a0bc762be1f798fb5a6cf301bc6669481ddbb

See more details on using hashes here.

File details

Details for the file django_api_decorator-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_api_decorator-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aee3b76e1bbe2182d1eeb8e7662751a211b12753375396481f86d58ad50bfdc2
MD5 4e4a22b0319157e27bd92a3c9e7199e3
BLAKE2b-256 6bc8adc0bc42a01dca711b71d2fb56da52250bd903155162ebbd09c9e101ca22

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