https://github.com/imbolc/lesync
Http helpers for django-channels
https://github.com/imbolc/lesync
asgi asyncio daphne django-channels uvicorn
Last synced: about 2 months ago
JSON representation
Http helpers for django-channels
- Host: GitHub
- URL: https://github.com/imbolc/lesync
- Owner: imbolc
- License: mit
- Created: 2018-09-14T14:46:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-31T06:53:29.000Z (over 4 years ago)
- Last Synced: 2025-02-12T19:19:34.847Z (2 months ago)
- Topics: asgi, asyncio, daphne, django-channels, uvicorn
- Language: Python
- Size: 16.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Lesync
======**[Deprecated]** With native async-views support, introduced in Django-3.1,
it's now deprecated in favor of [django-raw-api](https://github.com/imbolc/django-raw-api).Http helpers for `django-channels`
Hello world
-----------
```python
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`:
```python
from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.sessions import SessionMiddlewareStack
from lesync import ApiConsumerapplication = ProtocolTypeRouter({
# ... websockets or something
'http': SessionMiddlewareStack(
URLRouter([
path('/async/', 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` decoratorsResponse
--------
In addition to `django.http.HttpResponse`
you can use sugary json responses and streaming:```python
async def standard(request):
return HttpResponse('hello, world')async def json_data(request):
return {'hello': 'world'}async def with_status(request):
return {'bad': 'request'}, 400async 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:```python
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.```python
from lesync.trafaret import validate_json@validate_json({'ids': [int], 'hello?': str})
async def foo(request):
return request.json
```Tests
-----
```bash
python -m venv .venv
source .venv/bin/activate
pip install -Ur requirements-dev.txt
python -m pytest tests/
```