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.

Control Operations Included in the Schema

Controlling which operations are included in the generated schema can be useful. Your application might depend on libraries that also use the django-api-decorator package to build APIs, and you may prefer not to include those in your schema.

This is achieved by defining a set of tags on each view method.

@api(method="GET", tags=["django-api-decorator"])
def view(request: HttpRequest) -> None:
    ...

Views without the tags property set will always be included in the schema.

You can either exclude tags you don't want in your schema or select tags you want to include. However, you cannot both include and exclude tags simultaneously.

Including Tags in the Schema

Specify the tags to include in your schema in the Django settings file:

API_DECORATOR_SCHEMA_INCLUDE_TAGS = ["app", ...]

Excluding Tags from the Schema

Specify the tags to exclude from your schema in the Django settings file:

API_DECORATOR_SCHEMA_EXCLUDE_TAGS = ["library", ...]

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.5.2.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

django_api_decorator-0.5.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_api_decorator-0.5.2.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for django_api_decorator-0.5.2.tar.gz
Algorithm Hash digest
SHA256 d713570eeebcf5a1b84b7aa6460f6688ab7e582277b22c244e5227b8854af681
MD5 de1742b6182bed23b642e90aa85c2d81
BLAKE2b-256 50488a0e179fe28f35d11e1efc96390f4bb0bd2415111eab583cd0ae9f1f3a8c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_api_decorator-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.11.11 Linux/6.5.0-1025-azure

File hashes

Hashes for django_api_decorator-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 54cb043c9f7e9c34207fc47a3fecc47fbc9ecaabd5945b9f5f3d1491036003d2
MD5 0ff941b8d4a1f6c05b3be5c3c4f459b7
BLAKE2b-256 1b103f1febca1476663de526d2bbe870400fd493260fda1aa6fa61fb3e0e2d4b

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