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 main(context, method, request_type, id, send): """ 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, request_id=id, *positional, **named) # 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) # Otherwise, simply output the JSON-RPC request. else: click.echo(str(req)) sys.exit(exit_status)
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" }, }
def test_both_positional_and_keyword(self): self.assertEqual( { 'jsonrpc': '2.0', 'method': 'find', 'params': ['Foo', { 'age': 42 }] }, Notification('find', 'Foo', age=42))
def notify(self, method_name, *args, **kwargs): """Send a JSON-RPC request, without expecting a response. :param method_name: The remote procedure's method name. :param args: Positional arguments passed to the remote procedure. :param kwargs: Keyword arguments passed to the remote procedure. :return: The payload (i.e. the ``result`` part of the response). """ return self.send(Notification(method_name, *args, **kwargs))
def test_keyword(self): self.assertEqual( { 'jsonrpc': '2.0', 'method': 'find', 'params': { 'name': 'Foo' } }, Notification('find', name='Foo'))
def test_method_name_directly(self): self.assertEqual( {'jsonrpc': '2.0', 'method': 'cat'}, Notification.cat() #pylint:disable=no-member )
def test_positional(self): self.assertEqual({ 'jsonrpc': '2.0', 'method': 'sqrt', 'params': [1] }, Notification('sqrt', 1))
def test_method_name_directly(self): self.assertEqual({ 'jsonrpc': '2.0', 'method': 'cat' }, Notification.cat())
def test_str(self): self.assertEqual('{"jsonrpc": "2.0", "method": "get"}', str(Notification('get')))
def test(self): self.assertEqual({ 'jsonrpc': '2.0', 'method': 'get' }, Notification('get'))
async def notify(self, to, method, **params): mc = MethodCall(dest=to, method=method, source=self.name) req = Notification(mc.tos(), **params) return await self.send(req, to=to)
def test(self): assert Notification("get") == {"jsonrpc": "2.0", "method": "get"}
def test_str(self): assert str( Notification("get")) == '{"jsonrpc": "2.0", "method": "get"}'
def test_method_name_directly(self): assert {"jsonrpc": "2.0", "method": "cat"} == Notification.cat()
def test_positional(self): assert Notification("sqrt", 1) == { "jsonrpc": "2.0", "method": "sqrt", "params": [1], }