File: rpc_example.txt

package info (click to toggle)
python-jsonrpc2 0.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 200 kB
  • ctags: 73
  • sloc: python: 298; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
JSON-RPC2 Example
=====================================================

use raw rpc processor::

 >>> from jsonrpc2 import JsonRpc
 >>> rpc = JsonRpc()

sample procedures::

 >>> def subtract(minuend, subtrahend):
 ...     return minuend - subtrahend
 >>> def update(*args):
 ...     pass
 >>> def foobar():
 ...     pass

register procedures with dict interface::

 >>> rpc['subtract'] = subtract
 >>> rpc['update'] = update
 >>> rpc['foobar'] = foobar

Procedure Call with positional parameters::

 >>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1})
 {'jsonrpc': '2.0', 'id': 1, 'result': 19}

 >>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2})
 {'jsonrpc': '2.0', 'id': 2, 'result': -19}

Procedure Call with named parameters::

 >>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})
 {'jsonrpc': '2.0', 'id': 3, 'result': 19}

 >>> rpc({"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4})
 {'jsonrpc': '2.0', 'id': 4, 'result': 19}

Notification::

 >>> rpc({"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]})
 >>> rpc({"jsonrpc": "2.0", "method": "foobar"})

Procedure Call of non-existent procedure::

 >>> del rpc['foobar']
 >>> rpc({"jsonrpc": "2.0", "method": "foobar", "id": "1"})
 {'jsonrpc': '2.0', 'id': '1', 'error': {'message': 'Method Not Found', 'code': -32601}}

Procedure Call with invalid JSON-RPC::

 >>> rpc([1,2,3])
 {'jsonrpc': '2.0', 'id': None, 'error': {'message': 'Invalid Request', 'code': -32600}}

 >>> rpc({"jsonrpc": "2.0", "method": 1, "params": "bar"})
 {'jsonrpc': '2.0', 'id': None, 'error': {'message': 'Invalid Request', 'code': -32600}}


Batched Call::

 >>> rpc['sum'] = lambda *args: reduce(lambda a, b: a + b, args)
 >>> def get_data():
 ...     return ["hello", 5]
 >>> rpc['get_data'] = get_data
 >>> result = rpc ([ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
 ...      {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
 ...      {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
 ...      {"foo": "boo"},
 ...      {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
 ...      {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ])
 >>> from pprint import pprint
 >>> pprint(result)
 [{'id': '1', 'jsonrpc': '2.0', 'result': 7},
  {'error': {'code': -32601, 'message': 'Method Not Found'},
   'id': None,
   'jsonrpc': '2.0'},
  {'id': '2', 'jsonrpc': '2.0', 'result': 19},
  {'error': {'code': -32600, 'message': 'Invalid Request'},
   'id': None,
   'jsonrpc': '2.0'},
  {'error': {'code': -32601, 'message': 'Method Not Found'},
   'id': '5',
   'jsonrpc': '2.0'},
  {'id': '9', 'jsonrpc': '2.0', 'result': ['hello', 5]}]