AsyncAPIView allows you to use async handlers keeping the compatibility with django-rest-framework
Project description
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.http import HttpRequest, JsonResponse
from django.contrib.auth.models import User
from product.models import Product
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):
await asyncio.sleep(0.01)
return True
class AsyncThrottle(BaseThrottle):
async def allow_request(self, request: AsyncRequest, view: AsyncAPIView):
await asyncio.sleep(0.01)
return True
class ProductsView(AsyncAPIView):
authentication_classes = [AsyncAuthentication]
permission_classes = [AsyncPermission]
throttle_classes = [AsyncThrottle]
async def post(self, request: HttpRequest):
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,
)
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
drfasyncview-0.1.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for drfasyncview-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc5ddbbf59813aa55d4de28c29b2942f3da0b477e44540eaa7735aa2192dd05d |
|
MD5 | 289468c6c2c5dde3b24d7ceef608d86b |
|
BLAKE2b-256 | 8a7225dfb297f6e129b1e39cc2a8f8e182ddb0532a1132fbcfcec265910f0589 |