def main(context: click.core.Context, method: str, request_type: str, id: Any, send: str) -> None: """ Create a JSON-RPC request. """ exit_status = 0 # Extract the jsonrpc arguments positional = [a for a in context.args if "=" not in a] named = { a.split("=")[0]: a.split("=")[1] for a in context.args if "=" in a } # Create the request if request_type == "notify": req = Notification(method, *positional, **named) else: req = Request(method, *positional, request_id=id, **named) # type: ignore # Sending? if send: client = HTTPClient(send) try: response = client.send(req) except JsonRpcClientError as e: click.echo(str(e), err=True) exit_status = 1 else: click.echo(response.text) # Otherwise, simply output the JSON-RPC request. else: click.echo(str(req)) sys.exit(exit_status)
def test_positional(self): assert Notification("sqrt", 1) == { "jsonrpc": "2.0", "method": "sqrt", "params": [1], } assert Notification("sqrt", [1, 2, 3]) == { "jsonrpc": "2.0", "method": "sqrt", "params": [[1, 2, 3]], } assert Notification("sqrt", {"name": "Foo"}) == { "jsonrpc": "2.0", "method": "sqrt", "params": [{"name": "Foo"}], }
def test_both(self): assert Notification("find", "Foo", age=42) == { "jsonrpc": "2.0", "method": "find", "params": ["Foo", { "age": 42 }], }
def test_keyword(self): assert Notification("find", name="Foo") == { "jsonrpc": "2.0", "method": "find", "params": { "name": "Foo" }, }
async def main(): response = await client.send( [Request("ping"), Notification("ping"), Request("ping")]) for data in response.data: if data.ok: print("{}: {}".format(data.id, data.result)) else: logging.error("%d: %s", data.id, data.message)
async def main(): async with websockets.connect("ws://localhost:5000") as ws: requests = [Request("ping"), Notification("ping"), Request("ping")] response = await WebSocketsClient(ws).send(requests) for data in response.data: if data.ok: print("{}: {}".format(data.id, data.result)) else: logging.error("%d: %s", data.id, data.message)
async def main(loop): async with aiohttp.ClientSession(loop=loop) as session: client = AiohttpClient(session, "http://localhost:5000") requests = [Request("ping"), Notification("ping"), Request("ping")] response = await client.send(requests) for data in response.data: if data.ok: print("{}: {}".format(data.id, data.result)) else: logging.error("%d: %s", data.id, data.message)
import logging from jsonrpcclient.clients.zeromq_client import ZeroMQClient from jsonrpcclient.requests import Request, Notification requests = [Request("ping"), Notification("ping"), Request("ping")] response = ZeroMQClient("tcp://localhost:5000").send(requests) for data in response.data: if data.ok: print("{}: {}".format(data.id, data.result)) else: logging.error("%d: %s", data.id, data.message)
import logging from jsonrpcclient.clients.http_client import HTTPClient from jsonrpcclient.requests import Request, Notification client = HTTPClient("http://localhost:5000") response = client.send( [Request("ping"), Notification("ping"), Request("ping")]) for data in response.data: if data.ok: print("{}: {}".format(data.id, data.result)) else: logging.error("%d: %s", data.id, data.message)
def test_method_name_directly(self): assert {"jsonrpc": "2.0", "method": "cat"} == Notification.cat()
def test_str(self): assert str(Notification("get")) == '{"jsonrpc": "2.0", "method": "get"}'
def test(self): assert Notification("get") == {"jsonrpc": "2.0", "method": "get"}
def test_positional(self): assert Notification("sqrt", 1) == { "jsonrpc": "2.0", "method": "sqrt", "params": [1], }