Skip to main content

Expose methods of an RPC server through a CLI.

Project description

RPC to CLI

Wrapper for Python OpenRPC to expose the methods of an RPC Server through a CLI.

Install

RPC CLI is on PyPI and can be installed with:

pip install rpc-cli

Or with Poetry

poetry add rpc-cli

Example

Given the following in a file demo.py.

from openrpc import RPCServer
from pydantic import BaseModel

from rpc_cli import cli

rpc = RPCServer()


class Vector3(BaseModel):
    x: float
    y: float
    z: float


@rpc.method()
def get_distance(a: Vector3, b: Vector3) -> Vector3:
    """Get distance between two points."""
    return Vector3(x=a.x - b.x, y=a.y - b.y, z=a.z - b.z)


@rpc.method()
def divide(a: int, b: int) -> float:
    """Divide two integers."""
    return a / b


@rpc.method()
def summation(numbers: list[int | float]) -> int | float:
    """Summ all numbers in a list."""
    return sum(numbers)


if __name__ == "__main__":
    cli(rpc).run()

You now have a CLI.

Demo

Using the CLI

Methods can be called as such, notice arrays and object parameters are passed as JSON strings.

python demo.py get_distance '{"x": 1, "y": 1, "z": 1}' '{"x": 1, "y": 1, "z": 1}'
python demo.py divide 6 2
python demo.py summation '[1, 2, 3]'

Auto Completions

This library uses cleo, auto completions can be made by following the instructions in the cleo docs.

Support The Developer

Buy Me A Coffee

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

rpc_cli-2.2.2.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

rpc_cli-2.2.2-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page