Over-simple JSON-RPC Python implementation for aiohttp
Project description
Over-Simple JSON RPC Implementation
Over-simple JSON-RPC Python implementation for aiohttp
Install
pip install osjsonrpc
Usage
Example:
from aiohttp import web
from osjsonrpc import JsonRpcEndpoint
def ping():
return "pong"
def multiply(a, b):
return {"a": a, "b": b, "a*b": a * b}
rpc = (
JsonRpcEndpoint()
.register_method(ping)
.register_method(multiply)
)
app = web.Application()
app.add_routes([rpc.route("/api")])
web.run_app(app)
Call the ping
method without arguments:
~ curl -s -H "Content-Type: application/json" -H "Accept: application/json" -X POST \
-d '{"jsonrpc":"2.0", "method": "ping", "id": 1}' \
http://localhost:8080/api | jq
{
"jsonrpc": "2.0",
"result": "pong",
"id": 1
}
Call the multiply
with a couple of positional arguments:
~ curl -s -H "Content-Type: application/json" -H "Accept: application/json" -X POST \
-d '{"jsonrpc":"2.0", "method": "multiply", "params": [3, 5], "id": 1}' \
http://localhost:8080/api | jq
{
"jsonrpc": "2.0",
"result": {
"a": 3,
"b": 5,
"a*b": 15
},
"id": 1
}
Call the ping
argument with invalid keyword argument:
~ curl -s -H "Content-Type: application/json" -H "Accept: application/json" -X POST \
-d '{"jsonrpc":"2.0", "method": "ping", "params": {"key": "value"}, "id": 1}' \
http://localhost:8080/api | jq
{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params",
"data": {
"detail": "TypeError: ping() got an unexpected keyword argument 'key'",
"request": {
"jsonrpc": "2.0",
"method": "ping",
"params": {
"key": "value"
},
"id": 1
}
}
},
"id": 1
}
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
osjsonrpc-0.2.0.tar.gz
(5.2 kB
view hashes)
Built Distribution
Close
Hashes for osjsonrpc-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 996fb5a92c9b781460048636e468bd617278c763ac0c7fe301e470e59335dfdc |
|
MD5 | 513b725f9a1b1492761c635f377ff0e8 |
|
BLAKE2b-256 | 51a739da8dc0fa317efa7e4ee77d5a3a9e38bb8b8b5a1717a9760e299c7ef9ee |