Lesync
======
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 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:
```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'}, 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:
```python
from lesync import staff_member_required
@staff_member_required
async def hello(request):
return {'admin': request.user.username}
```
Tests
-----
```bash
pip install -Ur requirements-dev.txt
python -m pytest tests/
```
======
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 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:
```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'}, 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:
```python
from lesync import staff_member_required
@staff_member_required
async def hello(request):
return {'admin': request.user.username}
```
Tests
-----
```bash
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.0.3.tar.gz
(4.3 kB
view details)
File details
Details for the file lesync-0.0.3.tar.gz.
File metadata
- Download URL: lesync-0.0.3.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3f97c0ce0d27f9e1630702767c6ed388844c75fcd26e35dadbbd53931dc0651
|
|
| MD5 |
4c95a8ff8938a291a8e679a1e4b42ec8
|
|
| BLAKE2b-256 |
ae7b3fa7630fa59cc68516d431ba73b03257838fd86eb6c4f1bc78e0edd0822d
|