{ "info": { "author": "Madra David", "author_email": "david@madradavid.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Topic :: Internet :: WWW/HTTP" ], "description": "Note\n-----------\nThis repo is cloned from `channels-api `__\n\nChannels API\n------------\n\n.. image:: https://travis-ci.org/linuxlewis/channels-api.svg?branch=master\n :target: https://travis-ci.org/linuxlewis/channels-api\n\nChannels API exposes a RESTful Streaming API over WebSockets using\nchannels. It provides a ``ResourceBinding`` which is comparable to Django\nRest Framework's ``ModelViewSet``. It is based on DRF serializer\nclasses.\n\nIt requires Python 3, Django 1.8, and Django Rest Framework 3.0\n\nTable of Contents\n-----------------\n\n- `Getting Started <#getting-started>`__\n- `ResourceBinding <#resourcebinding>`__\n- `Subscriptions <#subscriptions>`__\n- `Errors <#errors>`__\n- `Roadmap <#roadmap>`__\n\n\nHow does it work?\n-----------------\n\nThe API builds on top of channels' ``WebsocketBinding`` class. It works by having\nthe client send a ``stream`` and ``payload`` parameters. This allows\nus to route messages to different streams (or resources) for a particular\naction. So ``POST /user`` would have a message that looks like the following\n\n.. code:: javascript\n\n var msg = {\n stream: \"users\",\n payload: {\n action: \"create\",\n data: {\n email: \"test@example.com\",\n password: \"password\",\n }\n }\n }\n\n ws.send(JSON.stringify(msg))\n\nWhy?\n----\n\nYou're already using Django Rest Framework and want to expose similar\nlogic over WebSockets.\n\nWebSockets can publish updates to clients without a request. This is\nhelpful when a resource can be edited by multiple users across many platforms.\n\nGetting Started\n---------------\n\nThis tutorial assumes you're familiar with channels and have completed\nthe `Getting\nStarted `__\n\n- Add ``channels_framework`` to requirements.txt\n\n.. code:: bash\n\n pip install channels_framework\n\n- Add ``channels_framework`` to ``INSTALLED_APPS``\n\n.. code:: python\n\n\n INSTALLED_APPS = (\n 'rest_framework',\n 'channels',\n 'channels_framework'\n )\n\n- Add a ``WebsocketDemultiplexer`` to your ``channel_routing``\n\n.. code:: python\n\n # proj/routing.py\n\n\n from channels.generic.websockets import WebsocketDemultiplexer\n from channels.routing import route_class\n\n class APIDemultiplexer(WebsocketDemultiplexer):\n\n mapping = {\n 'questions': 'questions_channel'\n }\n\n channel_routing = [\n route_class(APIDemultiplexer)\n ]\n\n- Add your first resource binding\n\n.. code:: python\n\n\n # polls/bindings.py\n\n from channels_framework.bindings import ResourceBinding\n\n from .models import Question\n from .serializers import QuestionSerializer\n\n class QuestionBinding(ResourceBinding):\n\n model = Question\n stream = \"questions\"\n serializer_class = QuestionSerializer\n queryset = Question.objects.all()\n\n\n # proj/routing.py\n\n from channels.routing import route_class, route\n\n from polls.bindings import QuestionBinding\n\n channel_routing = [\n route_class(APIDemultiplexer),\n route(\"question_channel\", QuestionBinding.consumer)\n ]\n\nThat's it. You can now make REST WebSocket requests to the server.\n\n.. code:: javascript\n\n var ws = new WebSocket(\"ws://\" + window.location.host + \"/\")\n\n ws.onmessage = function(e){\n console.log(e.data)\n }\n\n var msg = {\n stream: \"questions\",\n payload: {\n action: \"create\",\n data: {\n question_text: \"What is your favorite python package?\"\n },\n request_id: \"some-guid\"\n }\n }\n ws.send(JSON.stringify(msg))\n // response\n {\n stream: \"questions\",\n payload: {\n action: \"create\",\n data: {\n id: \"1\",\n question_text: \"What is your favorite python package\"\n }\n errors: [],\n response_status: 200\n request_id: \"some-guid\"\n }\n }\n\n- Add the channels debugger page (Optional)\n\nThis page is helpful to debug API requests from the browser and see the\nresponse. It is only designed to be used when ``DEBUG=TRUE``.\n\n.. code:: python\n\n # proj/urls.py\n\n from django.conf.urls import include\n\n urlpatterns = [\n url(r'^channels-api/', include('channels_framework.urls'))\n ]\n\nResourceBinding\n---------------\n\nBy default the ``ResourceBinding`` implements the following REST methods:\n\n- ``create``\n- ``retrieve``\n- ``update``\n- ``list``\n- ``delete``\n- ``subscribe``\n\nSee the test suite for usage examples for each method.\n\n\nList Pagination\n---------------\n\nPagination is handled by `django.core.paginator.Paginator`\n\nYou can configure the ``DEFAULT_PAGE_SIZE`` by overriding the settings.\n\n\n.. code:: python\n\n # settings.py\n\n channels_framework = {\n 'DEFAULT_PAGE_SIZE': 25\n }\n\n\nSubscriptions\n-------------\n\nSubscriptions are a way to programmatically receive updates\nfrom the server whenever a resource is created, updated, or deleted\n\nBy default channels-api has implemented the following subscriptions\n\n- create a Resource\n- update any Resource\n- update this Resource\n- delete any Resource\n- delete this Resource\n\nTo subscribe to a particular event just use the subscribe action\nwith the parameters to filter\n\n.. code:: javascript\n\n // get an event when any question is updated\n\n var msg = {\n stream: \"questions\",\n payload: {\n action: \"subscribe\",\n data: {\n action: \"update\"\n }\n }\n }\n\n // get an event when question(1) is updated\n var msg = {\n stream: \"questions\",\n payload: {\n action: \"subscribe\"\n data: {\n action: \"update\",\n pk: \"1\"\n }\n }\n }\n", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/madra/channels-rest-framework/tarball/0.1", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/madra/channels-rest-framework", "keywords": null, "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "channelsrestframework", "package_url": "https://pypi.org/project/channelsrestframework/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/channelsrestframework/", "project_urls": { "Download": "https://github.com/madra/channels-rest-framework/tarball/0.1", "Homepage": "https://github.com/madra/channels-rest-framework" }, "release_url": "https://pypi.org/project/channelsrestframework/0.1.4/", "requires_dist": null, "requires_python": null, "summary": "Build a RESTful API on top of WebSockets using Django channels and Django Rest Framework.", "version": "0.1.4" }, "last_serial": 2383270, "releases": { "0.1.4": [ { "comment_text": "", "digests": { "md5": "3db796c24004fba2d8f66a1da7fbb7ef", "sha256": "a0cab242f0016cf1fb63eba486ce15b0dfb6e6ce08f1f76607fcb5f96e7d13e2" }, "downloads": -1, "filename": "channelsrestframework-0.1.4.tar.gz", "has_sig": false, "md5_digest": "3db796c24004fba2d8f66a1da7fbb7ef", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7853, "upload_time": "2016-10-06T01:40:16", "url": "https://files.pythonhosted.org/packages/33/d8/5dffcaf4f0dda20bc8a1b37ef23c5356ac5e8bff2224e2f18e6699cb142f/channelsrestframework-0.1.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "3db796c24004fba2d8f66a1da7fbb7ef", "sha256": "a0cab242f0016cf1fb63eba486ce15b0dfb6e6ce08f1f76607fcb5f96e7d13e2" }, "downloads": -1, "filename": "channelsrestframework-0.1.4.tar.gz", "has_sig": false, "md5_digest": "3db796c24004fba2d8f66a1da7fbb7ef", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7853, "upload_time": "2016-10-06T01:40:16", "url": "https://files.pythonhosted.org/packages/33/d8/5dffcaf4f0dda20bc8a1b37ef23c5356ac5e8bff2224e2f18e6699cb142f/channelsrestframework-0.1.4.tar.gz" } ] }