Skip to content

kdebowski/starlette-jsonrpc

Repository files navigation

starlette-jsonrpc

Build Status codecov Code style: black

Installation

pip install starlette-jsonrpc

Examples

Code:

import uvicorn
from starlette.applications import Starlette

from starlette_jsonrpc import dispatcher
from starlette_jsonrpc.endpoint import JSONRPCEndpoint


app = Starlette()


@dispatcher.add_method
async def subtract(params):
    return params["x"] - params["y"]


@dispatcher.add_method(name="SubtractMethod")
async def seconds_subtract(params):
    return params["x"] - params["y"]


@dispatcher.add_method
async def subtract_positional(x, y):
    return x - y


app.mount("/api", JSONRPCEndpoint)

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

Example of requests:

{
  "jsonrpc": "2.0",
  "method": "subtract",
  "params": {"x": 42, "y": 23},
  "id": "1"
}
{
  "jsonrpc": "2.0",
  "method": "SubtractMethod",
  "params": {"x": 42, "y": 23},
  "id": "1"
}
{
    "jsonrpc": "2.0",
    "method": "subtract_positional",
    "params": [42, 23],
    "id": "1"
}

Example of response:

{
  "jsonrpc": "2.0",
  "id": "1",
  "result": 19
}

Contributing

Thank you for your interest in contributing. Everyone is welcome to take part in developting this package. Please fFollow contributing guide in CONTRIBUTING.md.