Exemple #1
0
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)
Exemple #2
0
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"
         },
     }
Exemple #5
0
 def test_both_positional_and_keyword(self):
     self.assertEqual(
         {
             'jsonrpc': '2.0',
             'method': 'find',
             'params': ['Foo', {
                 'age': 42
             }]
         }, Notification('find', 'Foo', age=42))
Exemple #6
0
    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))
Exemple #7
0
 def test_keyword(self):
     self.assertEqual(
         {
             'jsonrpc': '2.0',
             'method': 'find',
             'params': {
                 'name': 'Foo'
             }
         }, Notification('find', name='Foo'))
Exemple #8
0
 def test_method_name_directly(self):
     self.assertEqual(
         {'jsonrpc': '2.0', 'method': 'cat'},
         Notification.cat() #pylint:disable=no-member
     )
Exemple #9
0
 def test_positional(self):
     self.assertEqual({
         'jsonrpc': '2.0',
         'method': 'sqrt',
         'params': [1]
     }, Notification('sqrt', 1))
Exemple #10
0
 def test_method_name_directly(self):
     self.assertEqual({
         'jsonrpc': '2.0',
         'method': 'cat'
     }, Notification.cat())
Exemple #11
0
 def test_str(self):
     self.assertEqual('{"jsonrpc": "2.0", "method": "get"}',
                      str(Notification('get')))
Exemple #12
0
 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],
     }