Skip to main content

A Python server implementaion of JSON-RPC 2.0 over HTTP

Project description

Rufmich

A Python server implementaion of JSON-RPC 2.0 over HTTP.

Introduction

rufmich is an HTTP server for JSON-RPC 2.0. To make the JSON-RPC 2.0 work over HTTP, following specifications are added:

  1. The transport protocol is HTTP. The response for an notification is an HTTP response with status code 204 (No Response) and empty content. The HTTP requests must have the header content-type: application/json, otherwise there will be an HTTP error 415 (Unsupported Media Type).
  2. A notification request will get an immediate response. Usually the server will start the procedure and return the response immediately (without having to wait for the procedure to finish). And there is no callback for the procedure, which means the client would not be aware of any errors.
  3. Method namespacing is supported (and recommended).

Notification

Notification is implemented using threading. A method without id will be invoked asynchronously in a new thread and the client will get an HTTP response with status code 204 after the creation of the thread.

Batch

If multiple requests are sent in a batch, they will be processed concurrently by a thread pool. Only results of non-notification requests are returned. In case of that all the requests are notifications, the HTTP response will be of status 204.

User Guide

Installation

pip install rufmich

Define methods

Create a folder with following structure:

- <your_methods_workspace>
    - root
        root.py
        - <A>
            <A.py>
            - <B>
                <B.py>
        - <C>
            <C.py>

An example is:

- my_methods
    - root
        root.py
        - registration
            registration.py
            - by_email
                by_email.py

Note that there MUST BE a directory named root under your workspace folder.

Each namespace folder MUST HAVE a .py file with the same name as the folder. The methods defined in those .py files will be indexed according to the folder hierarchy.

Examples:

  1. A method foobar defined in root.py is indexed to "foobar" and "::foobar"
  2. A method send_code_to_email defined in by_email.py in the above example is indexed to "registration::by_email::send_code_to_email" (and "::registration::by_email::send_code_to_email")

Application-specific errors

The error codes from -32000 to -32099 are reserved for implementation-defined server-errors. The RPC server developer can raise an application-specific error by using RMError:

from rufmich.error import RMError

def division(a, b):
    if b == 0:
        raise RMError(eid=0, message='divided by 0', data=[a, b])
    return a / b

Each application-specific error should have an unique error id eid (0 <= eid <= 99). The error is mapped to [-32000, -32099] by the function f: eid -> -32000-eid.

Run

from rufmich.server import RMServer

server = RMServer(load_path='/workspace/methods')
server.run(endpoint='/jsonrpc', port=8080)

Client examples

Check on the website of JSON-RPC 2.0 Specification.

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

rufmich-1.0.6.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

rufmich-1.0.6-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file rufmich-1.0.6.tar.gz.

File metadata

  • Download URL: rufmich-1.0.6.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.0 CPython/3.7.3

File hashes

Hashes for rufmich-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4e1c5e5230ef39a2dabe3e186114e9b2f59bbd061f60659248e79de6015b03bf
MD5 774e538c20f41513355e4a9064b84556
BLAKE2b-256 20683713c7398b1f599fb967cad4b9269019ebce3078b79d4c4c424f7350d7b4

See more details on using hashes here.

File details

Details for the file rufmich-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: rufmich-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.0 CPython/3.7.3

File hashes

Hashes for rufmich-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7e5a2c6153996b395d64c03f6002894f185d97ee1163812567e363029e560893
MD5 c700ddd0b0168389d85966f7fba505bd
BLAKE2b-256 a5c11521754b512578537531b8fc149b2eb05627a60160c56c7c6887793e0ac4

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