adrf


Nameadrf JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttps://github.com/em1208/adrf
SummaryAsync support for Django REST framework
upload_time2024-04-06 02:08:30
maintainerNone
docs_urlNone
authorEnrico Massa
requires_python>=3.8
licenseMIT
keywords async django rest framework drf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Async Django REST framework

**Async support for Django REST framework**

# Requirements

* Python 3.8+
* Django 4.1+

We **highly recommend** and only officially support the latest patch release of
each Python and Django series.

# Installation

Install using `pip`...

    pip install adrf

Add `'adrf'` to your `INSTALLED_APPS` setting.
```python
INSTALLED_APPS = [
    ...
    'adrf',
]
```

# Examples

# Async Views

When using Django 4.1 and above, this package allows you to work with async class and function based views.

For class based views, all handler methods must be async, otherwise Django will raise an exception. For function based views, the function itself must be async.

For example:

```python
from adrf.views import APIView

class AsyncAuthentication(BaseAuthentication):
    async def authenticate(self, request) -> tuple[User, None]:
        return user, None

class AsyncPermission:
    async def has_permission(self, request, view) -> bool:
        if random.random() < 0.7:
            return False

        return True

    async def has_object_permission(self, request, view, obj):
        if obj.user == request.user or request.user.is_superuser:
            return True

        return False

class AsyncThrottle(BaseThrottle):
    async def allow_request(self, request, view) -> bool:
        if random.random() < 0.7:
            return False

        return True

    def wait(self):
        return 3

class AsyncView(APIView):
    authentication_classes = [AsyncAuthentication]
    permission_classes = [AsyncPermission]
    throttle_classes = [AsyncThrottle]

    async def get(self, request):
        return Response({"message": "This is an async class based view."})

from adrf.decorators import api_view

@api_view(['GET'])
async def async_view(request):
    return Response({"message": "This is an async function based view."})
```
# Async ViewSets

For viewsets, all handler methods must be async too.

views.py
```python
from django.contrib.auth import get_user_model
from rest_framework.response import Response

from adrf.viewsets import ViewSet


User = get_user_model()


class AsyncViewSet(ViewSet):

    async def list(self, request):
        return Response(
            {"message": "This is the async `list` method of the viewset."}
        )

    async def retrieve(self, request, pk):
        user = await User.objects.filter(pk=pk).afirst()
        return Response({"user_pk": user and user.pk})

```

urls.py
```python
from django.urls import path, include
from rest_framework import routers

from . import views

router = routers.DefaultRouter()
router.register(r"async_viewset", views.AsyncViewSet, basename="async")

urlpatterns = [
    path("", include(router.urls)),
]

```

# Async Serializers

serializers.py

```python
from adrf.serializers import Serializer
from rest_framework import serializers

class AsyncSerializer(Serializer):
    username = serializers.CharField()
    password = serializers.CharField()
    age = serializers.IntegerField()
```

views.py

```python
from . import serializers
from adrf.views import APIView

class AsyncView(APIView):
    async def get(self, request):
        data = {
            "username": "test",
            "password": "test",
            "age": 10,
        }
        serializer = serializers.AsyncSerializer(data=data)
        serializer.is_valid()
        return await serializer.adata
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/em1208/adrf",
    "name": "adrf",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "async, django, rest, framework, drf",
    "author": "Enrico Massa",
    "author_email": "enrico.massa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c7/33/580b2b55b477e096171de62c582c8b0b3173ba03cc254c9f257d95fff752/adrf-0.1.6.tar.gz",
    "platform": null,
    "description": "# Async Django REST framework\n\n**Async support for Django REST framework**\n\n# Requirements\n\n* Python 3.8+\n* Django 4.1+\n\nWe **highly recommend** and only officially support the latest patch release of\neach Python and Django series.\n\n# Installation\n\nInstall using `pip`...\n\n    pip install adrf\n\nAdd `'adrf'` to your `INSTALLED_APPS` setting.\n```python\nINSTALLED_APPS = [\n    ...\n    'adrf',\n]\n```\n\n# Examples\n\n# Async Views\n\nWhen using Django 4.1 and above, this package allows you to work with async class and function based views.\n\nFor class based views, all handler methods must be async, otherwise Django will raise an exception. For function based views, the function itself must be async.\n\nFor example:\n\n```python\nfrom adrf.views import APIView\n\nclass AsyncAuthentication(BaseAuthentication):\n    async def authenticate(self, request) -> tuple[User, None]:\n        return user, None\n\nclass AsyncPermission:\n    async def has_permission(self, request, view) -> bool:\n        if random.random() < 0.7:\n            return False\n\n        return True\n\n    async def has_object_permission(self, request, view, obj):\n        if obj.user == request.user or request.user.is_superuser:\n            return True\n\n        return False\n\nclass AsyncThrottle(BaseThrottle):\n    async def allow_request(self, request, view) -> bool:\n        if random.random() < 0.7:\n            return False\n\n        return True\n\n    def wait(self):\n        return 3\n\nclass AsyncView(APIView):\n    authentication_classes = [AsyncAuthentication]\n    permission_classes = [AsyncPermission]\n    throttle_classes = [AsyncThrottle]\n\n    async def get(self, request):\n        return Response({\"message\": \"This is an async class based view.\"})\n\nfrom adrf.decorators import api_view\n\n@api_view(['GET'])\nasync def async_view(request):\n    return Response({\"message\": \"This is an async function based view.\"})\n```\n# Async ViewSets\n\nFor viewsets, all handler methods must be async too.\n\nviews.py\n```python\nfrom django.contrib.auth import get_user_model\nfrom rest_framework.response import Response\n\nfrom adrf.viewsets import ViewSet\n\n\nUser = get_user_model()\n\n\nclass AsyncViewSet(ViewSet):\n\n    async def list(self, request):\n        return Response(\n            {\"message\": \"This is the async `list` method of the viewset.\"}\n        )\n\n    async def retrieve(self, request, pk):\n        user = await User.objects.filter(pk=pk).afirst()\n        return Response({\"user_pk\": user and user.pk})\n\n```\n\nurls.py\n```python\nfrom django.urls import path, include\nfrom rest_framework import routers\n\nfrom . import views\n\nrouter = routers.DefaultRouter()\nrouter.register(r\"async_viewset\", views.AsyncViewSet, basename=\"async\")\n\nurlpatterns = [\n    path(\"\", include(router.urls)),\n]\n\n```\n\n# Async Serializers\n\nserializers.py\n\n```python\nfrom adrf.serializers import Serializer\nfrom rest_framework import serializers\n\nclass AsyncSerializer(Serializer):\n    username = serializers.CharField()\n    password = serializers.CharField()\n    age = serializers.IntegerField()\n```\n\nviews.py\n\n```python\nfrom . import serializers\nfrom adrf.views import APIView\n\nclass AsyncView(APIView):\n    async def get(self, request):\n        data = {\n            \"username\": \"test\",\n            \"password\": \"test\",\n            \"age\": 10,\n        }\n        serializer = serializers.AsyncSerializer(data=data)\n        serializer.is_valid()\n        return await serializer.adata\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Async support for Django REST framework",
    "version": "0.1.6",
    "project_urls": {
        "Homepage": "https://github.com/em1208/adrf",
        "Repository": "https://github.com/em1208/adrf"
    },
    "split_keywords": [
        "async",
        " django",
        " rest",
        " framework",
        " drf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1a820185c26d23f4e68c2f50b990051969ff88f0a4df98a017cc3e84748206a6",
                "md5": "1538f2917de757a163a0d18893ec239d",
                "sha256": "4ed8a07159e97602b27b61cccef5a9f4088d07be6899d62fec1300286a6c500f"
            },
            "downloads": -1,
            "filename": "adrf-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1538f2917de757a163a0d18893ec239d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 15563,
            "upload_time": "2024-04-06T02:08:28",
            "upload_time_iso_8601": "2024-04-06T02:08:28.546920Z",
            "url": "https://files.pythonhosted.org/packages/1a/82/0185c26d23f4e68c2f50b990051969ff88f0a4df98a017cc3e84748206a6/adrf-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c733580b2b55b477e096171de62c582c8b0b3173ba03cc254c9f257d95fff752",
                "md5": "3c7ecba6b714a5c7c306184e40065cad",
                "sha256": "8057be8ac17e1ed47fbec17de4bf1772d467243422fd5f7dd78e97735d395ed3"
            },
            "downloads": -1,
            "filename": "adrf-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "3c7ecba6b714a5c7c306184e40065cad",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13210,
            "upload_time": "2024-04-06T02:08:30",
            "upload_time_iso_8601": "2024-04-06T02:08:30.428493Z",
            "url": "https://files.pythonhosted.org/packages/c7/33/580b2b55b477e096171de62c582c8b0b3173ba03cc254c9f257d95fff752/adrf-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-06 02:08:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "em1208",
    "github_project": "adrf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "adrf"
}
        
Elapsed time: 0.22768s