Skip to main content

Python JSON-RPC 2.0 client library.

Project description

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.

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

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

jsonrpc2-pyclient-3.2.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

jsonrpc2_pyclient-3.2.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file jsonrpc2-pyclient-3.2.0.tar.gz.

File metadata

  • Download URL: jsonrpc2-pyclient-3.2.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.12 Linux/6.4.6-76060406-generic

File hashes

Hashes for jsonrpc2-pyclient-3.2.0.tar.gz
Algorithm Hash digest
SHA256 93bec73dd5d4a91e3cf13add2f4af1036bfc8a25e1db57b649b98d08d26bb32f
MD5 ad8541bb4a9a53ca130f9dedc21cd7a9
BLAKE2b-256 ad01f94a67da8d67f268ee2a66d0e1785e9d9c4f58fb9797fa534c508398ef77

See more details on using hashes here.

File details

Details for the file jsonrpc2_pyclient-3.2.0-py3-none-any.whl.

File metadata

  • Download URL: jsonrpc2_pyclient-3.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.12 Linux/6.4.6-76060406-generic

File hashes

Hashes for jsonrpc2_pyclient-3.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f76daa60ae4e1c9d7b9c38a56f9d239a9e4b9a76577b545c595a7d230e37ab1a
MD5 80a8fbd88aa24130d45d1abca619669e
BLAKE2b-256 91d8fe1a05e7f296ed0bdf6643cbbe17b6aa2c902d6a33100fd6fc44108f185f

See more details on using hashes here.

Supported by

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