Skip to main content

Django channels endpoints

Project description

Django channels endpoints

This package provides endpoints as functions for django channels package.

Installation

pip install channels-endpoints

Usage

In your django app create dce.py file:

from channels_endpoints.main import endpoint, Response
from django.contrib.auth.models import User
from django.core import serializers
from .permissions import UsersPermissions
from asgiref.sync import sync_to_async


users_logger = logging.getLogger('users')


@endpoint(logger=users_logger, permissions=[UsersPermissions], timeout=42)
async def get_users(request):
    
    print(request.data)
    
    def _get():
        return serializers.serialize('python', User.objects.all(), fields=('username', 'id'))

    await request.consumer.send(text_data=Response(None, f'hello', consumers=['SomeConsumer']))
    
    return Response(
        request,
        await sync_to_async(_get)()
    )

Python client:

from channels_endpoints.client import Dce, DceException, consumer

@consumer
async def SomeConsumer(response):
    print(f'SomeConsumer: {response["data"]}')
    
dce = Dce('http://127.0.0.1:8000/ws/')

try:
    data = await dce.request('myapp.get_users', {'some': 'data'})
except DceException as e:
    print(e.__repr__())

dce.close()

Javascript client:

import {dce, consumer} from "channels_endpoints"

consumer('SomeConsumer', (response) => {
    console.log('SomeConsumer: ', response.data)
})

dce('myapp.get_users', {some: "data"}).then(
    response => {
        console.log('users:', response)
    }
)

see js client package https://www.npmjs.com/package/channels_endpoints

Examples

For complete usage examples see example chat

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

channels_endpoints-1.0.3.tar.gz (9.8 kB view hashes)

Uploaded Source

Built Distribution

channels_endpoints-1.0.3-py3-none-any.whl (10.5 kB view hashes)

Uploaded Python 3

Supported by

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