Skip to main content

Lesync

Http helpers for django-channels

Hello world

from lesync import login_required

@login_required
async def hello(request):
    return {'hello': request.user.username}

Install

pip install lesync

Add ApiConsumer into your routing.py:

from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.sessions import SessionMiddlewareStack
from lesync import ApiConsumer

application = ProtocolTypeRouter({
    # ... websockets or something
    'http': SessionMiddlewareStack(
        URLRouter([
            path('/async/<path:path>', ApiConsumer),
            # ... fallback to sync views
        ]),
    ),
})

That's it. Now you can add async views which will be served win /async/ url prefix.

Request

Subclass of channels.http.AsgiRequest with:

  • json attribute
  • async load_user() method - which returns standard django user
  • user attribute - it's available after calling load_user() of if you use @login_required or @staff_member_required decorators

Response

In addition to django.http.HttpResponse you can use sugary json responses and streaming:

async def standard(request):
    return HttpResponse('hello, world')

async def json_data(request):
    return {'hello': 'world'}

async def with_status(request):
    return {'bad': 'request'}, 400

async def with_headers(request):
    return {}, 200, {'my': 'header'}

async def streaming(request):
    async def stream():
        for i in range(1, 1000):
            yield f'{i},{i**2}\n'
            await asyncio.sleep(.1)
    return stream(), 200, {'Content-Type': 'text/csv'}

Auth

Familiar @login_required and @staff_member_required decorators:

from lesync import staff_member_required

@staff_member_required
async def hello(request):
    return {'admin': request.user.username}

Request validation

You can use @validate_query and @validate_json decorators to validate requests data. They're using trafaret library to perform validation, so make sure it's installed.

from lesync.trafaret import validate_json

@validate_json({'ids': [int], 'hello?': str})
async def foo(request):
    return request.json

Tests

    python -m venv .venv
    source .venv/bin/activate
    pip install -Ur requirements-dev.txt
    python -m pytest tests/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lesync-0.3.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

lesync-0.3.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file lesync-0.3.0.tar.gz.

File metadata

  • Download URL: lesync-0.3.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for lesync-0.3.0.tar.gz
Algorithm Hash digest
SHA256 371446df0c031827e645eb2cf3a36580bf7ac3917ab357b35f81f5e5d88a4dba
MD5 6e3cf2d4acd49ac0abbe3ad67f469f4f
BLAKE2b-256 bb16be6f3c7008cc0889d973a2a668f7c559c272981a570373a447bf2096e0a8

See more details on using hashes here.

File details

Details for the file lesync-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: lesync-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for lesync-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0614f9bed13902eb406a81c2b82bf20783c76c6dfce322270b7cb38deb65fbc
MD5 ac5dac60f918273c372d7c2e6470878f
BLAKE2b-256 8f331741cb6ae7810b1ad68e2ca045f31981951e3dbec47bfe02733880b1db87

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