{"id":19935972,"url":"https://github.com/imbolc/lesync","last_synced_at":"2025-03-01T12:19:38.114Z","repository":{"id":57438615,"uuid":"148802223","full_name":"imbolc/lesync","owner":"imbolc","description":"Http helpers for django-channels","archived":false,"fork":false,"pushed_at":"2020-08-31T06:53:29.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-12T19:19:34.847Z","etag":null,"topics":["asgi","asyncio","daphne","django-channels","uvicorn"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imbolc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-14T14:46:24.000Z","updated_at":"2020-08-31T06:53:31.000Z","dependencies_parsed_at":"2022-09-08T08:03:03.963Z","dependency_job_id":null,"html_url":"https://github.com/imbolc/lesync","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Flesync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Flesync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Flesync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Flesync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imbolc","download_url":"https://codeload.github.com/imbolc/lesync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241361994,"owners_count":19950486,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asgi","asyncio","daphne","django-channels","uvicorn"],"created_at":"2024-11-12T23:22:52.340Z","updated_at":"2025-03-01T12:19:38.083Z","avatar_url":"https://github.com/imbolc.png","language":"Python","readme":"Lesync\n======\n\n**[Deprecated]** With native async-views support, introduced in Django-3.1,\nit's now deprecated in favor of [django-raw-api](https://github.com/imbolc/django-raw-api).\n\nHttp helpers for `django-channels`\n\nHello world\n-----------\n```python\nfrom lesync import login_required\n\n@login_required\nasync def hello(request):\n    return {'hello': request.user.username}\n```\n\nInstall\n-------\n\n    pip install lesync\n\nAdd `ApiConsumer` into your `routing.py`:\n\n```python\nfrom django.urls import path\nfrom channels.routing import ProtocolTypeRouter, URLRouter\nfrom channels.sessions import SessionMiddlewareStack\nfrom lesync import ApiConsumer\n\napplication = ProtocolTypeRouter({\n    # ... websockets or something\n    'http': SessionMiddlewareStack(\n        URLRouter([\n            path('/async/\u003cpath:path\u003e', ApiConsumer),\n            # ... fallback to sync views\n        ]),\n    ),\n})\n```\n\nThat's it. Now you can add async views which will be served win `/async/` url\nprefix.\n\n\nRequest\n-------\nSubclass of `channels.http.AsgiRequest` with:\n\n- `json` attribute\n- `async load_user()` method - which returns standard django user\n- `user` attribute - it's available after calling `load_user()` of if you use\n    `@login_required` or `@staff_member_required` decorators\n\n\nResponse\n--------\nIn addition to `django.http.HttpResponse`\nyou can use sugary json responses and streaming:\n\n```python\nasync def standard(request):\n    return HttpResponse('hello, world')\n\nasync def json_data(request):\n    return {'hello': 'world'}\n\nasync def with_status(request):\n    return {'bad': 'request'}, 400\n\nasync def with_headers(request):\n    return {}, 200, {'my': 'header'}\n\nasync def streaming(request):\n    async def stream():\n        for i in range(1, 1000):\n            yield f'{i},{i**2}\\n'\n            await asyncio.sleep(.1)\n    return stream(), 200, {'Content-Type': 'text/csv'}\n```\n\n\nAuth\n----\nFamiliar `@login_required` and `@staff_member_required` decorators:\n\n```python\nfrom lesync import staff_member_required\n\n@staff_member_required\nasync def hello(request):\n    return {'admin': request.user.username}\n```\n\n\nRequest validation\n------------------\nYou can use `@validate_query` and `@validate_json` decorators\nto validate requests data. They're using `trafaret` library to perform\nvalidation, so make sure it's installed.\n\n```python\nfrom lesync.trafaret import validate_json\n\n@validate_json({'ids': [int], 'hello?': str})\nasync def foo(request):\n    return request.json\n```\n\n\nTests\n-----\n```bash\n    python -m venv .venv\n    source .venv/bin/activate\n    pip install -Ur requirements-dev.txt\n    python -m pytest tests/\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbolc%2Flesync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimbolc%2Flesync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbolc%2Flesync/lists"}