JSON-RPC transport implementation for Python supporting both 1.0 and 2.0 protocols with Django and Flask backends
npx @tessl/cli install tessl/pypi-json-rpc@1.15.0A comprehensive Python implementation of JSON-RPC 1.0 and 2.0 transport specification. This library provides protocol implementation only, without transport functionality, making it transport-agnostic and suitable for integration with any transport mechanism like HTTP, WebSockets, or message queues.
pip install json-rpcfrom jsonrpc import JSONRPCResponseManager, dispatcherFor specific components:
from jsonrpc import Dispatcher
from jsonrpc.manager import JSONRPCResponseManager
from jsonrpc.exceptions import JSONRPCError, JSONRPCDispatchException
from jsonrpc.jsonrpc import JSONRPCRequestfrom jsonrpc import JSONRPCResponseManager, dispatcher
# Method 1: Using the global dispatcher
@dispatcher.add_method
def add(a, b):
return a + b
@dispatcher.add_method
def subtract(a, b):
return a - b
# Handle JSON-RPC request
request_data = '{"jsonrpc": "2.0", "method": "add", "params": [5, 3], "id": 1}'
response = JSONRPCResponseManager.handle(request_data, dispatcher)
print(response.json)
# Output: {"jsonrpc": "2.0", "result": 8, "id": 1}
# Method 2: Creating a custom dispatcher
from jsonrpc import Dispatcher
my_dispatcher = Dispatcher()
my_dispatcher.add_method(lambda x: x * 2, name="double")
my_dispatcher["echo"] = lambda s: s
request_data = '{"jsonrpc": "2.0", "method": "double", "params": [21], "id": 2}'
response = JSONRPCResponseManager.handle(request_data, my_dispatcher)
print(response.json)
# Output: {"jsonrpc": "2.0", "result": 42, "id": 2}The json-rpc library follows a clean separation of concerns:
This design enables the library to serve as a foundation for JSON-RPC implementations across different transport mechanisms and frameworks.
Complete implementation of JSON-RPC 1.0 and 2.0 protocols with automatic version detection, batch request support, and comprehensive error handling.
class JSONRPCResponseManager:
@classmethod
def handle(cls, request_str: str, dispatcher: dict, context: dict = None): ...
@classmethod
def handle_request(cls, request, dispatcher: dict, context: dict = None): ...Structured request and response objects for both JSON-RPC versions with automatic serialization, validation, and batch processing support.
class JSONRPCRequest:
@classmethod
def from_json(cls, json_str: str): ...
@classmethod
def from_data(cls, data: dict): ...
class JSONRPC20Request:
def __init__(self, method: str = None, params = None, _id = None, is_notification: bool = None): ...
class JSONRPC10Request:
def __init__(self, method: str = None, params = None, _id = None, is_notification: bool = None): ...Flexible method registration and dispatch system supporting functions, classes, objects, and decorators with context injection.
class Dispatcher:
def __init__(self, prototype = None): ...
def add_method(self, f = None, name: str = None, context_arg: str = None): ...
def add_class(self, cls): ...
def add_object(self, obj): ...
def add_dict(self, dict: dict, prefix: str = ''): ...Comprehensive error handling with predefined JSON-RPC error types and custom exception support for robust error reporting.
class JSONRPCError:
def __init__(self, code: int = None, message: str = None, data = None): ...
class JSONRPCDispatchException(Exception):
def __init__(self, code: int = None, message: str = None, data = None, *args, **kwargs): ...Ready-to-use integrations for Django and Flask frameworks providing HTTP endpoints, URL routing, and request handling.
# Django backend
from jsonrpc.backend.django import JSONRPCAPI
# Flask backend
from jsonrpc.backend.flask import JSONRPCAPI# Core dispatcher type
dispatcher: Dispatcher
# Version information
__version__: str
version: str
# Project information
__project__: str
PROJECT: str