Skip to main content

JSON RPC PyClient

License: AGPL v3 Code style: black PyPI version Contributions Welcome

A library for creating JSON RPC 2.0 clients in Python with async support

Install

poetry add jsonrpc2-pyclient
pip install jsonrpc2-pyclient

Example

Example of the client decorator implementing rpc methods from reading method signatures.

import asyncio

from pydantic import BaseModel

from jsonrpc2pyclient.decorator import rpc_client
from jsonrpc2pyclient.httpclient import AsyncRPCHTTPClient

transport = AsyncRPCHTTPClient("http://127.0.0.1:8000/api/v1")


class Vector3(BaseModel):
    x: float = 1.0
    y: float = 1.0
    z: float = 1.0


@rpc_client(transport=transport)
class TestClient:
    async def add(self, a: int, b: int) -> int: ...
    async def get_distance(self, a: Vector3, b: Vector3) -> Vector3: ...


async def main() -> None:
    client = TestClient()
    assert await client.add(3, 4) == 7
    assert await client.get_distance(Vector3(), Vector3()) == Vector3(x=0, y=0, z=0)


if __name__ == "__main__":
    asyncio.run(main())

RPCClient Abstract Class

JSON-RPC 2.0 is transport agnostic. This library provides an abstract class that can be extended to create clients for different transports.

Transports

To make client for a transport, extend the RPCClient class and implement the _send_and_get_json which takes a request as a str and is expected to return a JSON-RPC 2.0 response as a str or byte string. RPCClient has a call method that uses this internally.

A default HTTP and Websocket implementation is provided.

Usage

The RPCClient will handle forming requests and parsing responses. To call a JSON-RPC 2.0 method with an implementation of RPCClient, call the call method, passing it the name of the method to call and the params.

If the response is JSON-RPC 2.0 result object, only the result will be returned, none of the wrapper.

If the response is JSON-RPC 2.0 error response, and exception will be thrown for the error.

from jsonrpc2pyclient.httpclient import RPCHTTPClient
from jsonrpcobjects.errors import JSONRPCError

client = RPCHTTPClient("http://localhost:5000/api/v1/")
try:
    res = client.call("divide", [0, 0])
    print(f"JSON-RPC Result: {res}")
except JSONRPCError as e:
    print(f"JSON-RPC Error: {e}")

Client Decorator

The rpc_client decorator can be used to quickly put together a client with typed methods. When a class is decorated, each method defined in that class will make RPC requests using the provided transport and parse the result. The name of the method will be used in the RPC request.

The method body must end with ... for the decorator to implement it.

transport = RPCHTTPClient("http://127.0.0.1:8000/api/v1")

@rpc_client(transport=transport)
class TestClient:
    def add(self, a: int, b: int) -> int: ...


client = TestClient()
assert client.add(3, 4) == 7

Metadata

Release files for jsonrpc2-pyclient 5.2.3

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

Source distribution (sdist)

Source distribution for jsonrpc2-pyclient 5.2.3
File Size Uploaded
jsonrpc2_pyclient-5.2.3.tar.gz 20.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonrpc2-pyclient 5.2.3
File Interpreter ABI Platform
jsonrpc2_pyclient-5.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 41.7 kB

Release files / jsonrpc2_pyclient-5.2.3.tar.gz

Download URL jsonrpc2_pyclient-5.2.3.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b9bde2c2051b669a89db2fe65c8f786949d04f486df68f2babaf170434384dbf
BLAKE2b-256 checksum
How to use checksums
07f7796eeaa786c560880fb96e07e1a8f6ae0bc3b33b204f0713aa2d5008f85f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.13.5 Linux/6.15.9-201.fc42.x86_64

Release files / jsonrpc2_pyclient-5.2.3-py3-none-any.whl

Download URL jsonrpc2_pyclient-5.2.3-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
129ccb3153d4867bbfb54720f19a372416c0571669c1ea861f5ecb9f4c740ef9
BLAKE2b-256 checksum
How to use checksums
7908b30d1db43faa7b3dfcb2a6ac21bbfe1c82fc23988f60ecd74e8f499a810f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.13.5 Linux/6.15.9-201.fc42.x86_64

Release history Release notifications | RSS feed

This release

5.2.3 This release

2 release files

5.2.0

2 release files

5.0.0

2 release files

4.3.5

2 release files

4.3.3

2 release files

4.3.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.2.0

2 release files

2.2.12

2 release files

2.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

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