Python JSON-RPC 2.0 client library.
Project description
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)
client.subtract(2, 3)
client.divide(3, 2)
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__}:')
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
jsonrpc2-pyclient-1.0.0.tar.gz
(15.2 kB
view hashes)