Python JSON-RPC 2.0 client library.
Project description
Install
pip install jsonrpc2-pyclient
Usage
RPC Client Abstract Class
The RPCClient abstract class provides methods to ease the development of an RPC Client for any transport method. It parses JSON-RPC 2.0 requests and responses.
To use, an implementation only needs to override the
_send_and_get_json
method. This method is used internally.
JSONRPCClient will pass it a request as a JSON string and expect a
response as JSON string.
A simple implementation:
class RPCHTTPClient(RPCClient):
def __init__(self, url: str) -> None:
self.url = url
super(RPCHTTPClient, self).__init__()
def _send_and_get_json(self, request_json: str) -> Union[bytes, str]:
return requests.post(url=self.url, data=request_json).content
Default HTTP Client
This module provides a default HTTP implementation of the RPCClient.
Example HTTP Client Usage
If a JSON RPC server defines the methods "add", "subtract", and "divide", expecting the following requests:
{
"id": 1,
"method": "add",
"params": [2, 3],
"jsonrpc": "2.0"
}
{
"id": 2,
"method": "subtract",
"params": [2, 3],
"jsonrpc": "2.0"
}
{
"id": 3,
"method": "divide",
"params": [3, 2],
"jsonrpc": "2.0"
}
Defining and using the corresponding client would look like this:
class MathClient(RPCHTTPClient):
def add(self, a: int, b: int) -> int:
return self.call('add', [a, b])
def subtract(self, a: int, b: int) -> int:
return self.call('subtract', [a, b])
def divide(self, a: int, b: int) -> float:
return self.call('divide', [a, b])
client = MathClient('http://localhost:5000/api/v1')
client.add(2, 3) # 5
client.subtract(2, 3) # -1
client.divide(2, 2) # 1
Notice, just the result field of the JSON-RPC response object is
returned by call
, not the whole object.
Errors
If the server responds with an error, an RpcError is thrown.
There is an RpcError for each standard JSON RPC 2.0 error, each of them extends RpcError.
client = MathClient('http://localhost:5000/api/v1')
try:
client.add('two', 'three')
except InvalidParams as e:
log.exception(f'{type(e).__name__}:')
try:
client.divide(0, 0)
except ServerError as e:
log.exception(f'{type(e).__name__}:')
Async Support (v1.1+)
Async alternatives to the RPCClient ABC and RPCHTTPClient are available.
class AsyncMathClient(AsyncRPCHTTPClient):
async def add(self, a: int, b: int) -> int:
return await self.call('add', [a, b])
async def subtract(self, a: int, b: int) -> int:
return await self.call('subtract', [a, b])
async def divide(self, a: int, b: int) -> float:
return await self.call('divide', [a, b])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file jsonrpc2-pyclient-2.1.1.tar.gz
.
File metadata
- Download URL: jsonrpc2-pyclient-2.1.1.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | edb688098aed3056d897b01b0ef6b92a96a83905d02fe598a13bc5e97e9f0ee3 |
|
MD5 | ef25cba4b4de181ab45c9e3b5410ec66 |
|
BLAKE2b-256 | 496964b01a830fdef457d50eb6512a4a34924c690c817ca8584515b74105221d |