Skip to main content

drf-async-view

Django supports AsyncView from 4.1 to support writing asynchronous handlers.

AsyncAPIView allows you to use async handlers keeping the compatibility with django-rest-framework as well.

Installation

You can install the latest release from pypi:

$ pip install drfasyncview

How to use

Example

import asyncio

from django.contrib.auth.models import User
from django.db import models
from django.http import HttpRequest, JsonResponse
from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission
from rest_framework.throttling import BaseThrottle
from typing import Optional, Tuple

from drfasyncview import AsyncRequest, AsyncAPIView


class AsyncAuthentication(BaseAuthentication):    
    async def authenticate(self, request: AsyncRequest) -> Optional[Tuple[User, str]]:
        await asyncio.sleep(0.01)
        return None


class AsyncPermission(BasePermission):
    async def has_permission(self, request: AsyncRequest, view: AsyncAPIView) -> bool:
        await asyncio.sleep(0.01)
        return True


class AsyncThrottle(BaseThrottle):
    async def allow_request(self, request: AsyncRequest, view: AsyncAPIView) -> bool:
        await asyncio.sleep(0.01)
        return True


class Product(models.Model):
    name = models.CharField(max_length=256, unique=True)
    price = models.IntegerField()


class ProductsView(AsyncAPIView):
    authentication_classes = [AsyncAuthentication]
    permission_classes = [AsyncPermission]
    throttle_classes = [AsyncThrottle]

    async def post(self, request: HttpRequest) -> JsonResponse:
        name = request.data["name"]
        price = request.data["price"]

        product = await Product.objects.acreate(name=name, price=price)

        return JsonResponse(
            data={"name": product.name, "price": product.price},
            status=200,
        )

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drfasyncview-0.2.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

drfasyncview-0.2.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file drfasyncview-0.2.3.tar.gz.

File metadata

  • Download URL: drfasyncview-0.2.3.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for drfasyncview-0.2.3.tar.gz
Algorithm Hash digest
SHA256 74a139137bdaa622586c86332c95970b0498e37b26383a0c02894f5f1544a9ed
MD5 b6a38dff0bd704bc6c98ffab282e2306
BLAKE2b-256 8529640eae0f839adc8db83acdb3a84133ee24595d8496a41a9ab382fa66b156

See more details on using hashes here.

File details

Details for the file drfasyncview-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: drfasyncview-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for drfasyncview-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9fa61a4b4b91f1de82d162d946e23d8ea78bb6e94aa2d5241fcf001cf95642b5
MD5 b8306229a8aec70daf799b43b13f19fd
BLAKE2b-256 3e2cb244ea5053226c1fdb2dcd8bc86ad21555a3645178349e78e1545e7a5476

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

0.0.1

2 files

Supported by

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