Skip to main content

slxjsonrpc

versions Test Status Lint Status Coverage pypi license SeluxitA/S Wappsto

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.

v0.9.2 (August 17, 2023)

Added

  • RcpBatch is now iterable and behave more list a list.

Fixed

  • Type hinting errors.
  • Most issues where the wrong combo of exclude_unset & exclude_none given to model_dump_json would result in a invalid JsonRpc.

Changed

  • Notifications can now not trigger an Error Rpc reply. as per the specification.

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.

Metadata

Release files for slxjsonrpc 0.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slxjsonrpc 0.9.2
File Size Uploaded Provenance Actions
slxjsonrpc-0.9.2.tar.gz 32.4 kB Without provenance Details for slxjsonrpc-0.9.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for slxjsonrpc 0.9.2
File Interpreter ABI Platform Actions
slxjsonrpc-0.9.2-py3-none-any.whl Python 3 none any Details for slxjsonrpc-0.9.2-py3-none-any.whl

Total release size: 60.9 kB

Release files / slxjsonrpc-0.9.2.tar.gz

Download URL slxjsonrpc-0.9.2.tar.gz
Size 32.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bf00ad5dad1f8f14bbac2e7b1a0a03fd954b41114c316bcba34f4322902676fd
BLAKE2b-256 checksum
How to use checksums
a55ac2585a538b147220a6936b107e21e1734e5ca63f8a09b0610fb933777c0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / slxjsonrpc-0.9.2-py3-none-any.whl

Download URL slxjsonrpc-0.9.2-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4092da50bbd307490d1bd85994ab2456023115e13a3aaf873ff9a23576cda727
BLAKE2b-256 checksum
How to use checksums
29b314c348b796556592638c53a68cd1908b4ba40769e96068a7d45db3a46374
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for slxjsonrpc 0.9.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page