SlxJsonRpc JsonRpc helper class, that uses pydantic.
Project description
slxjsonrpc
SlxJsonRpc is a JsonRpc helper class, that uses pydantic.
SlxJsonRpc keep track of the JsonRpc schema, and procedure for each method. It also ensures to route each message to where it is expected.
SlxJsonRpc is build to be both a JsonRpc server & client. To enable the JsonRpc-server, the method_map need to be given.
Installation using pip
The slxjsonrpc package can be installed using PIP (Python Package Index) as follows:
$ pip install slxjsonrpc
Use case Examples
The given use case show how to use the slxJsonRpc Package. It expected that you have a send & a receive function/method to transport the Json RPC messages to and from the package.
The Client example code:
from typing import List, Union, Literal
from enum import Enum
import slxjsonrpc
def send(data: str) -> None: ...
def receive() -> Union[str, bytes, dict]: ...
class MethodList(str, Enum):
ADD = "add"
PING = "ping"
params = {
MethodList.ADD: List[Union[int, float]],
MethodList.PING: None,
}
result = {
MethodList.ADD: Union[int, float],
MethodList.PING: Literal["pong"]
}
client_jsonrpc = slxjsonrpc.SlxJsonRpc(
methods=MethodsList,
result=result,
params=params,
)
ok = None
def reply(reply_data):
nonlocal ok
ok = reply_data # Will be "pong"
ping_package = client_jsonrpc.create_request(method=MethodList.PING, callback=reply)
send(ping_package.json(exclude_none=True))
data = receive()
client_jsonrpc.parser(data)
print(f"OK: {ok}")
The Server example code:
from typing import List, Union, Literal
from enum import Enum
import slxjsonrpc
def send(data: str) -> None: ...
def receive() -> Union[str, bytes, dict]: ...
class MethodList(str, Enum):
ADD = "add"
PING = "ping"
params = {
MethodList.ADD: List[Union[int, float]],
MethodList.PING: None,
}
result = {
MethodList.ADD: Union[int, float],
MethodList.PING: Literal["pong"]
}
method_map = {
MethodList.ADD: lambda data: sum(data),
MethodList.PING: lambda data: "pong",
}
server_jsonrpc = slxjsonrpc.SlxJsonRpc(
methods=MethodsList,
result=result,
params=params,
method_cb=method_map,
)
data = receive()
return_data = server_jsonrpc.parser(data)
if return_data:
send(return_data.json(exclude_none=True))
License
This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details.
Known Bugs
- Can not have 2 independent slxJsonRpcs running in same code base.
TODO List
Code base
- Add more/better logging logs.
- Enforce the result Schema. schema/jsonrpc.py:217-225
- Push to pip.
- Refactor so the same code can have multiple independent slxJsonRpc.
- Use case Examples.
Tests
- Add more test to get a 100%-ish testing coverage.
- Test Notification with unknown Method, and method Enum not set. jsonrpc.py:330
- Test Notification, where params set, when they should not be.
- Test Request with unknown Method, and method Enum not set. jsonrpc.py:348 schema/jsonrpc.py:131
- Test Request, where params set, when they should not be.
- Test response with unknown id
- Test RpcError, when no Error callback is set.
- Test if the Bulking receiving works as intended.
- Test with params as pydantic BaseModel.
v0.9.1 (August 4, 2023)
Fixed
- Type hinting errors.
- A method to param mapping error.
v0.9.0 (August 3, 2023)
Changed
- Now using Pydantic version 2.
- Now require python 3.7 or later.
v0.8.2 (December 13, 2021)
Added
- Change-log
Changed
- How
create_request
adds a Result reply handling. So now it uses_add_result_handling
, which mean it will be easier to hook into for work-a-rounds.
v0.8.1 (November 25, 2021)
Fixed
- Type hinting errors.
v0.8.0 (November 12, 2021)
Added
- First Release.
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
Built Distribution
File details
Details for the file slxjsonrpc-0.9.1.tar.gz
.
File metadata
- Download URL: slxjsonrpc-0.9.1.tar.gz
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f5cf662af71504542ae6041e0a61e925191acef80bba1ed95d5a1c003077ab2 |
|
MD5 | 5cbb53016a48772fc208dc25eb22c978 |
|
BLAKE2b-256 | 7add34555145053f9f0437e6156e404150d1dfafe1e1493a2f054f8fae76fbc5 |
File details
Details for the file slxjsonrpc-0.9.1-py3-none-any.whl
.
File metadata
- Download URL: slxjsonrpc-0.9.1-py3-none-any.whl
- Upload date:
- Size: 28.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada3da692303c5611cd6ebd5d5f6f7447867c6dfbf52861182243d4d1714ae75 |
|
MD5 | cfb6abe54d85b7ed023e0ca6f14df79c |
|
BLAKE2b-256 | d1fc8b09f049c271694c80f12cd3156dbc95b81f780d0113ae41d89126ae4480 |