Skip to main content

JSON-RPC 2.0 handler for Tornado.

Project description

🐍🌪️ tornado-jsonrpc2

Build Status codecov

A request handler for tornado that implements the JSON-RPC 2.0 specification.

The aim is to have a spec-compliant handler that can be set up in a flexible way.

Requirements

The current requirement for this is to have at least Python 3.6 and Tornado 5.

Python

The usage of json.JSONDecodeError leads to the minimum version Python 3.6.

The usage of async / await means the lowest ever supported version will be Python 3.5. While currently not in focus having this running on Python 3.5 may become an option.

Usage

To allow for flexible configuration the JSONRPCHandler will have to be passed a parameter response_creator which takes an callable that the request in form will get passed to. The request is an instance of tornado_jsonrpc2.jsonrpc.JSONRPCRequest with the attributes method and params (and id and version should they be required).

The following example shows how this can be used to execute methods on a backend instance. By using functools we are able to create a function that only requires the request as an parameter.

import functools
import tornado.web

from tornado_jsonrpc2.handler import JSONRPCHandler
from tornado_jsonrpc2.exceptions import MethodNotFound


class MyBackend:
    def subtract(self, minuend, subtrahend):
        return minuend - subtrahend


async def create_response(request, backend):
    try:
        method = getattr(backend, request.method)
    except AttributeError:
        raise MethodNotFound("Method {!r} not found!".format(request.method))

    try:
        params = request.params
    except AttributeError:
        return method()

    if isinstance(params, list):
        return method(*params)
    elif isinstance(params, dict):
        return method(**params)


def make_app():
    simple_creator = functools.partial(create_response,
                                       backend=MyBackend())

    return tornado.web.Application([
        (r"/jsonrpc", JSONRPCHandler, {"response_creator": simple_creator}),
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

You can access this example app on port 8888 with curl:

$ curl --insecure --data '{"jsonrpc": "2.0", "method": "subtract", "params": [5, 1], "id": 1}' http://localhost:8888/jsonrpc

{"jsonrpc": "2.0", "id": 1, "result": 4}

You can also name the parameters:

$ curl --insecure --data '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 5, "subtrahend": 1}, "id": 1}' http://localhost:8888/jsonrpc

{"jsonrpc": "2.0", "id": 1, "result": 4}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tornado-jsonrpc2-0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

tornado_jsonrpc2-0.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file tornado-jsonrpc2-0.1.tar.gz.

File metadata

  • Download URL: tornado-jsonrpc2-0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for tornado-jsonrpc2-0.1.tar.gz
Algorithm Hash digest
SHA256 9e38c62c6c5101920af2ed4294c69673a0c583243c397208e2e2578cf9c4338e
MD5 73e76cb429ee2549606ee6d8bc56cfa7
BLAKE2b-256 63ef085ea5fb40417ffc03ad129300a53a70b1f92faf991af12e25a1c8c6b9e2

See more details on using hashes here.

File details

Details for the file tornado_jsonrpc2-0.1-py3-none-any.whl.

File metadata

  • Download URL: tornado_jsonrpc2-0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for tornado_jsonrpc2-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7610b6a2bb98c66a07e54343cccf8f0c2e617bca5f2502485c71cc8b6618d9c8
MD5 274e7e4b5ed0d94f23e072615df32f09
BLAKE2b-256 e83ec68fc86684e05b460b18c38356fada80c5ee02663d38392862ec0a15a867

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page