Skip to main content

🐍🌪️ tornado-jsonrpc2

Build Status codecov

A JSON-RPC request handler for Tornado.

It follows the specifications for JSON-RPC 2.0 and 1.0. Differences between the versions are described here in short form. By default both versions will be handled and answers are made according to the version detected for the request.

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}

Handling specific JSON-RPC versions

By default the handler will process JSON-RPC 1.0 and 2.0. It is possible to configure the handler to only work with one specific version by adding a key version with the value "1.0" or "2.0" to the route spec.

The following example adds a specific route for each version:

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

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

Metadata

Release files for tornado-jsonrpc2 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tornado-jsonrpc2 0.5
File Size Uploaded Provenance
tornado-jsonrpc2-0.5.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tornado-jsonrpc2 0.5
File Interpreter ABI Platform
tornado_jsonrpc2-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 25.0 kB

Release files / tornado-jsonrpc2-0.5.tar.gz

Download URL tornado-jsonrpc2-0.5.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cb37bbeefec0ecb2a3cd7902dcfa4b7f45c701bf56f759d6aeb842445163b770
BLAKE2b-256 checksum
How to use checksums
a360d9010369b7b15c355b2a43d3413655e8ada327a4db2566e88c82d71425aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / tornado_jsonrpc2-0.5-py3-none-any.whl

Download URL tornado_jsonrpc2-0.5-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0da80a3a5fb75ce04950cae8a67aa85075f709379e7c0325c244f1ec6df0b918
BLAKE2b-256 checksum
How to use checksums
966b9221f50eae28858dfccce6cc931d8b44e450109c17571a3b231173910898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Security for tornado-jsonrpc2 0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page