drfasyncview


Namedrfasyncview JSON
Version 0.2.3 PyPI version JSON
download
home_pagehttps://github.com/hisdream86/drf-async-view
SummaryAsyncAPIView allows you to use async handlers keeping the compatibility with django-rest-framework
upload_time2022-12-26 02:17:00
maintainer
docs_urlNone
authorhisdream86
requires_python>=3.8
licenseMIT
keywords django djangorestframework async
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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,
        )
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hisdream86/drf-async-view",
    "name": "drfasyncview",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "django,djangorestframework,async",
    "author": "hisdream86",
    "author_email": "hisdream86@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/85/29/640eae0f839adc8db83acdb3a84133ee24595d8496a41a9ab382fa66b156/drfasyncview-0.2.3.tar.gz",
    "platform": null,
    "description": "# drf-async-view\n\nDjango 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.\n\n`AsyncAPIView` allows you to use async handlers keeping the compatibility with django-rest-framework as well.\n\n## Installation\n\nYou can install the latest release from pypi:\n\n```sh\n$ pip install drfasyncview\n```\n\n## How to use\n\n### Example\n\n```python\nimport asyncio\n\nfrom django.contrib.auth.models import User\nfrom django.db import models\nfrom django.http import HttpRequest, JsonResponse\nfrom rest_framework.authentication import BaseAuthentication\nfrom rest_framework.permissions import BasePermission\nfrom rest_framework.throttling import BaseThrottle\nfrom typing import Optional, Tuple\n\nfrom drfasyncview import AsyncRequest, AsyncAPIView\n\n\nclass AsyncAuthentication(BaseAuthentication):    \n    async def authenticate(self, request: AsyncRequest) -> Optional[Tuple[User, str]]:\n        await asyncio.sleep(0.01)\n        return None\n\n\nclass AsyncPermission(BasePermission):\n    async def has_permission(self, request: AsyncRequest, view: AsyncAPIView) -> bool:\n        await asyncio.sleep(0.01)\n        return True\n\n\nclass AsyncThrottle(BaseThrottle):\n    async def allow_request(self, request: AsyncRequest, view: AsyncAPIView) -> bool:\n        await asyncio.sleep(0.01)\n        return True\n\n\nclass Product(models.Model):\n    name = models.CharField(max_length=256, unique=True)\n    price = models.IntegerField()\n\n\nclass ProductsView(AsyncAPIView):\n    authentication_classes = [AsyncAuthentication]\n    permission_classes = [AsyncPermission]\n    throttle_classes = [AsyncThrottle]\n\n    async def post(self, request: HttpRequest) -> JsonResponse:\n        name = request.data[\"name\"]\n        price = request.data[\"price\"]\n\n        product = await Product.objects.acreate(name=name, price=price)\n\n        return JsonResponse(\n            data={\"name\": product.name, \"price\": product.price},\n            status=200,\n        )\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "AsyncAPIView allows you to use async handlers keeping the compatibility with django-rest-framework",
    "version": "0.2.3",
    "split_keywords": [
        "django",
        "djangorestframework",
        "async"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "b8306229a8aec70daf799b43b13f19fd",
                "sha256": "9fa61a4b4b91f1de82d162d946e23d8ea78bb6e94aa2d5241fcf001cf95642b5"
            },
            "downloads": -1,
            "filename": "drfasyncview-0.2.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b8306229a8aec70daf799b43b13f19fd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5414,
            "upload_time": "2022-12-26T02:16:58",
            "upload_time_iso_8601": "2022-12-26T02:16:58.724418Z",
            "url": "https://files.pythonhosted.org/packages/3e/2c/b244ea5053226c1fdb2dcd8bc86ad21555a3645178349e78e1545e7a5476/drfasyncview-0.2.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "b6a38dff0bd704bc6c98ffab282e2306",
                "sha256": "74a139137bdaa622586c86332c95970b0498e37b26383a0c02894f5f1544a9ed"
            },
            "downloads": -1,
            "filename": "drfasyncview-0.2.3.tar.gz",
            "has_sig": false,
            "md5_digest": "b6a38dff0bd704bc6c98ffab282e2306",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 5044,
            "upload_time": "2022-12-26T02:17:00",
            "upload_time_iso_8601": "2022-12-26T02:17:00.172316Z",
            "url": "https://files.pythonhosted.org/packages/85/29/640eae0f839adc8db83acdb3a84133ee24595d8496a41a9ab382fa66b156/drfasyncview-0.2.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-26 02:17:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hisdream86",
    "github_project": "drf-async-view",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "drfasyncview"
}
        
Elapsed time: 0.02263s