MCPcopy Index your code
hub / github.com/blimpllc/django-websocket-request

github.com/blimpllc/django-websocket-request @0.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.1.1 ↗ · + Follow
52 symbols 243 edges 4 files 3 documented · 6%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

django-websocket-request

Build Status PyPI version

Overview

This package provides support for transport agnostic routing to allow the Django's request/response lifecycle to automatically WebSocket messages. It borrows a WebSocket message format from Sails.js.

Installation

Install using pip...

$ pip install django-websocket-request

Usage

ws.py

# Set Django Environment
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'

from wsrequest import WebSocketRequest

message = '{"method": "GET", "url": "/api/projects/"}'
request = WebSocketRequest(message)
response = request.get_response()

print(response.content)

WebSocket message format

The method key can be any HTTP method: GET, POST, PUT, DELETE, PATCH, HEAD, or OPTIONS. The url key is an absolute URL without the domain name. The data key is an optional dictionary in which the key-value pairs in used to create the method’s data payload. The token key is also optional, and used to recreate an HTTP Authorization header, Authorization: JWT YOUR_TOKEN_HERE. If you're using Django REST framework, you might like to check out django-rest-framework-jwt.

{
  "method": "POST",
  "url": "/api/companies/",
  "data": {
    "name": "Acme Inc."
  },
  "token": "MY_JSON_WEB_TOKEN"
}

Live Demo

Check out GetBlimp/django-websocket-request-example for an example implementation and a live demo.

Example implementation with sockjs + tornado

import os
import json

from tornado import web, ioloop
from sockjs.tornado import SockJSRouter, SockJSConnection

# Set Django Environment
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'

from wsrequest import WebSocketRequest


class RESTAPIConnection(SockJSConnection):
    def on_open(self, info):
        self.send(json.dumps({'connected': True}))

    def on_message(self, data):
        response = WebSocketRequest(data).get_response()
        self.send(response.content)


if __name__ == '__main__':
    import logging

    port = 8080

    logging.getLogger().setLevel(logging.INFO)

    Router = SockJSRouter(RESTAPIConnection, '/ws/api')

    app = web.Application(Router.urls)
    app.listen(port)

    logging.info(" [*] Listening on 0.0.0.0:{}".format(port))

    ioloop.IOLoop.instance().start()

Core symbols most depended-on inside this repo

is_valid_message
called by 12
wsrequest/__init__.py
is_valid
called by 9
wsrequest/__init__.py
get_factory
called by 6
wsrequest/__init__.py
get_url_resolver_match
called by 5
wsrequest/__init__.py
set_error
called by 4
wsrequest/__init__.py
get_request
called by 4
wsrequest/__init__.py
get_response
called by 4
wsrequest/__init__.py
get_url
called by 3
wsrequest/__init__.py

Shape

Method 44
Class 4
Function 4

Languages

Python100%

Modules by API surface

wsrequest/tests.py33 symbols
wsrequest/__init__.py15 symbols
setup.py3 symbols
run_tests.py1 symbols

For agents

$ claude mcp add django-websocket-request \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact