Skip to main content

Brings async, event-driven capabilities to Django.

Project description

rest_framework_channels

The enhanced modules for REST WebSockets using django channels.

Installation

pip install rest_framework_channels

After installing it, you should insert 'rest_framework_channels' in the INSTALLED_APPS.

INSTALLED_APPS = [
    # Websocket
    'daphne',
    'channels',
    'rest_framework_channels', # add
    ...

Introduction

rest_framework_channels is the enhanced modules for REST WebSockets using django channels.

You can use serializers and queryset in rest_framework in rest_framework_channels. Also, we are ready for similar permissions and generics too.

Example

We use the below model and serializer as example.

class TestModel(models.Model):
    """Simple model to test with."""

    title = models.CharField(max_length=255)
    content = models.CharField(max_length=1024)

class TestSerializer(ModelSerializer):
    class Meta:
        model = TestModel
        fields = '__all__'
from rest_framework_channels import generics
from rest_framework_channels.consumers import AsyncAPIConsumer
from rest_framework_channels.permissions import IsAuthenticated

class ChildActionHandler(generics.RetrieveAPIActionHandler):
    serializer_class = TestSerializer
    queryset = TestModel.objects.all()
    permission_classes = (IsAuthenticated,)

class ParentConsumer(AsyncAPIConsumer):
    # You can define the routing inside the consumer similar with original django's urlpatterns
    routepatterns = [
        re_path(
            r'test_child_route/(?P<pk>[-\w]+)/$',
            ChildActionHandler.as_aaah(),
        ),
    ]

When you send the below json after establishing the connection,

{
    'action': 'retrieve', # Similar with GET method of HTTP request
    'route': 'test_child_route/1/',
}

you will get the below response. This mechanism is very similar with original rest_framework!

{
    'errors': [],
    'data': {
        'id': 1,
        'title': 'title',
        'content': 'content'
    },
    'action': 'retrieve',
    'route': 'test_child_route/1/',
    'status': 200,
}

As you can see permission_classes, you will be rejected when you send that json without login.

{
    'errors': ['Some Error Messages']
    'data': None,
    'action': 'retrieve',
    'route': 'test_child_route/1/',
    'status': 403,
}

Details

For more details, see docs.

Development

code

pip install -e .
pip install twine

documentation

cd sphinx
sudo apt-get -y install plantuml
pip install -r requirements.txt
  • generate rst files and html files
cd sphinx
bash build.sh

Reference

This project is VERY inspired by djangochannelsrestframework.

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

rest_framework_channels-0.0.6.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

rest_framework_channels-0.0.6-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file rest_framework_channels-0.0.6.tar.gz.

File metadata

  • Download URL: rest_framework_channels-0.0.6.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.3

File hashes

Hashes for rest_framework_channels-0.0.6.tar.gz
Algorithm Hash digest
SHA256 0b5599d00fb11201ec4b1dcf8fd28cafbb30514e341d97e4b57ee1a416c2c569
MD5 9ec8bd827ee0188a41f43a06d3aeafb4
BLAKE2b-256 24f751537e0381d59c8438db0cb5106c9f62d0a07dcf02fad776d40d7f24985b

See more details on using hashes here.

File details

Details for the file rest_framework_channels-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for rest_framework_channels-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1b007d569578fa63263c9cc978824b35174c953bddea38c957f510823e49dfeb
MD5 628827ad656654f27cdaeb9395593dde
BLAKE2b-256 08d2c16b8ec3cdc0c4ea8b6e7f397049a2fc5c1b2870ed081f26e5619e79a7e2

See more details on using hashes here.

Supported by

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