Python JSON-RPC 2.0 client library.
Project description
Install
poetry add jsonrpc2-pyclient
pip install jsonrpc2-pyclient
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.
Implementations
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 implementation is provided:
class RPCHTTPClient(RPCClient):
"""A JSON-RPC HTTP Client."""
def __init__(self, url: str, headers: Optional[Headers] = None) -> None:
self._client = httpx.Client()
headers = headers or {}
headers["Content-Type"] = "application/json"
self._client.headers = headers
self.url = url
super(RPCHTTPClient, self).__init__()
def __del__(self) -> None:
self._client.close()
@property
def headers(self) -> Headers:
"""HTTP headers to be sent with each request."""
return self._client.headers
@headers.setter
def headers(self, headers) -> None:
self._client.headers = headers
def _send_and_get_json(self, request_json: str) -> Union[bytes, str]:
return self._client.post(self.url, content=request_json).content
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}")
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
Built Distribution
File details
Details for the file jsonrpc2-pyclient-2.2.12.tar.gz
.
File metadata
- Download URL: jsonrpc2-pyclient-2.2.12.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.18.10-76051810-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ea12d75f89c7eda6571f88b780a4a2e482ec901f1f596ade95cb0c021529950 |
|
MD5 | 3c27e297a447a15e61f364c6163e7d7b |
|
BLAKE2b-256 | 4d9f088de625c6836569a815acc0e65072b405e27c332e27375e9c6e7bc6a3e2 |
File details
Details for the file jsonrpc2_pyclient-2.2.12-py3-none-any.whl
.
File metadata
- Download URL: jsonrpc2_pyclient-2.2.12-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.18.10-76051810-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbeb00c278f8a533abb810383e1249473fa1fd71b5f28f6700c322ee40e8922d |
|
MD5 | 7a66b0f44e36cc803cb58ef24407dc64 |
|
BLAKE2b-256 | 00be9dfb39cff098ff6b0bd9e3a9f0dddd6353a70203a333cedfb1d89655f23d |