Skip to main content

JSON-RPC transport implementation

Project description

Build Status Coverage Status https://readthedocs.org/projects/json-rpc/badge/?version=latest https://www.codacy.com/project/badge/34e0c2c696214041ae4fd5cfcb4af401 Latest PyPI version Supported Python versions

JSON-RPC2.0 and JSON-RPC1.0 transport specification implementation. Supports Python 2.6+, Python 3.3+, PyPy. Has optional Django and Flask support. 200+ tests.

Features

This implementation does not have any transport functionality realization, only protocol. Any client or server implementation is easy based on current code, but requires transport libraries, such as requests, gevent or zmq, see examples.

  • Vanilla Python, no dependencies.

  • 200+ tests for multiple edge cases.

  • Optional backend support for Django, Flask.

  • json-rpc 1.1 and 2.0 support.

Install

pip install json-rpc

Tests

Quickstart

This is an essential part of the library as there are a lot of edge cases in JSON-RPC standard. To manage a variety of supported python versions as well as optional backends json-rpc uses tox:

tox

Continuous integration

This project uses Travis for continuous integration. All of the python supported versions are managed via tox.ini and .travis.yml files. Only master and develop branches are tested. Master branch test status is displayed on the badge in the beginning of this document.

Test matrix

json-rpc supports multiple python versions: 2.6+, 3.3+, pypy. This introduces difficulties with testing libraries and optional dependencies management. For example, python before version 3.3 does not support mock and there is a limited support for unittest2. Every dependency translates into if-then blocks in the source code and adds complexity to it. Hence, while cross-python support is a core feature of this library, cross-Django or cross-Flask support is limited. In general, json-rpc uses latest stable release which supports current python version. For example, python 2.6 is compatible with Django 1.6 and not compatible with any future versions.

Below is a testing matrix:

Python

Tests

mock

unittest

Django

Flask

2.6

nose

2.0.0

unittest2

1.6

0.12.2

2.7

nose2

2.0.0

1.11

0.12.2

3.3

nose2

1.11

0.12.2

3.4

nose2

1.11

0.12.2

3.5

nose2

1.11

0.12.2

3.6

nose2

1.11

0.12.2

pypy

nose2

2.0.0

1.11

0.12.2

pypy3

nose2

1.11

0.12.2

Quickstart

Server (uses Werkzeug)

from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple

from jsonrpc import JSONRPCResponseManager, dispatcher


@dispatcher.add_method
def foobar(**kwargs):
    return kwargs["foo"] + kwargs["bar"]


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["echo"] = lambda s: s
    dispatcher["add"] = lambda a, b: a + b

    response = JSONRPCResponseManager.handle(
        request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':
    run_simple('localhost', 4000, application)

Client (uses requests)

import requests
import json


def main():
    url = "http://localhost:4000/jsonrpc"
    headers = {'content-type': 'application/json'}

    # Example echo method
    payload = {
        "method": "echo",
        "params": ["echome!"],
        "jsonrpc": "2.0",
        "id": 0,
    }
    response = requests.post(
        url, data=json.dumps(payload), headers=headers).json()

    assert response["result"] == "echome!"
    assert response["jsonrpc"]
    assert response["id"] == 0

if __name__ == "__main__":
    main()

Competitors

There are several libraries implementing JSON-RPC protocol. List below represents python libraries, none of the supports python3. tinyrpc looks better than others.

Contributing

TODO: Add your name to the author list. It follows chronological order, so add yours at the end.

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

json-rpc-1.10.5.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_rpc-1.10.5-py2.py3-none-any.whl (40.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file json-rpc-1.10.5.tar.gz.

File metadata

  • Download URL: json-rpc-1.10.5.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for json-rpc-1.10.5.tar.gz
Algorithm Hash digest
SHA256 95f4d859b2e7a5d244067e4838ef86373bbf7f0c8dd7c19e1a800955597698f8
MD5 4cf186533d9146c8662ce2ec267dfabb
BLAKE2b-256 876b5827316f84483f57bf6cd41c14215920a85d3443019a96f5ed86c4cd3f54

See more details on using hashes here.

File details

Details for the file json_rpc-1.10.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for json_rpc-1.10.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 17c77894820d0be04317f28c3ea7134d0646079cbcc2cbb8419dc5cd0ab07e8c
MD5 56e1a3a8be0db0adffa6e8ac2e32b0d5
BLAKE2b-256 d6579f1defb67f13cc7d8761961086dcefc437d96e68875f1fac658db2446259

See more details on using hashes here.

Supported by

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