Skip to main content

OpenRPC provides classes to rapidly develop an OpenRPC server.

Project description

OpenRPC

OpenRPC provides classes to rapidly develop an OpenRPC server.

License: MIT Code style: black PyPI version Code Coverage Contributions Welcome

Installation

OpenRPC is on PyPI and can be installed with:

pip install openrpc
poetry add openrpc

Usage

This library provides an RPCServer class that can be used to quickly create an OpenRPC Server.

from openrpc import RPCServer

rpc = RPCServer(title="Demo Server", version="1.0.0")

Register a function as an RPC Method

To register a method with the RPCServer add the @rpc.method decorator to a function.

@rpc.method
def add(a: int, b: int) -> int:
    return a + b

Process JSON RPC Request

OpenRPC is transport agnostic. To use it, pass JSON RPC requests as strings or byte strings to the process_request or process_request_async method.

The process_request will return a JSON RPC response as a string.

req = """
{
  "id": 1,
  "method": "add",
  "params": {"a": 2, "b": 2},
  "jsonrpc": "2.0"
}
"""
await rpc.process_request_async(req)
# returns -> '{"id": 1, "result": 4, "jsonrpc": "2.0"}'

Pydantic Support

For data classes to work properly use Pydantic. RPCServer will use Pydantic for JSON serialization/deserialization when calling methods and when generating schemas with rpc.discover.

RPC Discover

The rpc.discover method is automatically generated. It relies heavily on type hints.

Example Using Sanic

A quick example using OpenRPC exposing the methods using Sanic.

from sanic import HTTPResponse, Request, Sanic, text

from openrpc import RPCServer

app = Sanic("DemoServer")
rpc = RPCServer(title="DemoServer", version="1.0.0")


@rpc.method
def add(a: int, b: int) -> int:
    return a + b


@app.post("/api/v1/")
def process_rpc(request: Request) -> HTTPResponse:
    return text(rpc.process_request(request.body) or "Notify complete.")


if __name__ == "__main__":
    app.run()

Example In

[
  {
    "id": 1,
    "method": "add",
    "params": {
      "a": 1,
      "b": 3
    },
    "jsonrpc": "2.0"
  },
  {
    "id": 2,
    "method": "add",
    "params": [
      11,
      "thirteen"
    ],
    "jsonrpc": "2.0"
  }
]

Example Result Out

[
  {
    "id": 1,
    "result": 4,
    "jsonrpc": "2.0"
  },
  {
    "id": 2,
    "error": {
      "code": -32603,
      "message": "Internal error",
      "data": "Failed to deserialize request param [thirteen] to type [<class 'int'>]"
    },
    "jsonrpc": "2.0"
  }
]

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

openrpc-6.0.5.tar.gz (14.2 kB view hashes)

Uploaded Source

Built Distribution

openrpc-6.0.5-py3-none-any.whl (15.2 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