drfasyncview-0.2.3
توضیحات
AsyncAPIView allows you to use async handlers keeping the compatibility with django-rest-framework
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | drfasyncview-0.2.3 |
| نام | drfasyncview |
| نسخه کتابخانه | 0.2.3 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | hisdream86 |
| ایمیل نویسنده | hisdream86@gmail.com |
| آدرس صفحه اصلی | https://github.com/hisdream86/drf-async-view |
| آدرس اینترنتی | https://pypi.org/project/drfasyncview/ |
| مجوز | MIT |
# drf-async-view
Django supports [AsyncView](https://docs.djangoproject.com/en/4.1/releases/4.1/#asynchronous-handlers-for-class-based-views) 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:
```sh
$ pip install drfasyncview
```
## How to use
### Example
```python
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,
)
```
نیازمندی
| مقدار | نام |
|---|---|
| >=4.1.0 | django |
| >=3.13.0 | djangorestframework |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.8 | Python |
نحوه نصب
نصب پکیج whl drfasyncview-0.2.3:
pip install drfasyncview-0.2.3.whl
نصب پکیج tar.gz drfasyncview-0.2.3:
pip install drfasyncview-0.2.3.tar.gz