File: README.rst

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 (110 lines) | stat: -rw-r--r-- 2,446 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
.. -*- restructuredtext -*-

.. image:: https://drone.io/bitbucket.org/aodag/jsonrpc2/status.png
   :target: https://drone.io/bitbucket.org/aodag/jsonrpc2/latest

jsonrpc2 is WSGI Framework for JSON RPC 2.0.

JSON RPC 2.0 Spec can be seen on http://www.jsonrpc.org/specification .

.. contents::

QuickStart
==========================================

install via pip::

 $ pip install jsonrpc2

write your procedures in hello.py::

 def greeting(name):
     return dict(message="Hello, %s!" % name)

run jsonrpc2 server::

 $ runjsonrpc2 hello


Integration with Paste Script
===============================================

create project with paste script template::

 $ paster create -t paster_jsonrpc2 myrpc
 $ cd myrpc

run server

 $ paster serve run.ini

access http://localhost:8080/


Internal
===============================

::

 >>> import json
 >>> from jsonrpc2 import JsonRpcApplication

sample procedure::

 >>> def greeting(name="world"):
 ...     return "Hello, %s!" % name

create rpc application::

 >>> app = JsonRpcApplication(rpcs=dict(greeting=greeting))

set up for test::

 >>> from webtest import TestApp
 >>> testapp = TestApp(app)

call procedure::

 >>> call_values = {'jsonrpc':'2.0', 'method':'greeting', 'id':'greeting'}
 >>> res = testapp.post('/', params=json.dumps(call_values), content_type="application/json")

got results::

 >>> res.json
 {u'jsonrpc': u'2.0', u'id': u'greeting', u'result': u'Hello, world!'}


lazy loading::

 >>> app.rpc.methods['sample.add'] = 'tests.sample:add'
 >>> call_values = {'jsonrpc':'2.0', 'method':'sample.add', 'id':'sample.add', 'params':[1, 2]}
 >>> res = testapp.post('/', params=json.dumps(call_values), content_type="application/json")
 >>> res.json
 {u'jsonrpc': u'2.0', u'id': u'sample.add', u'result': 3}


extra vars
==================

::

 >>> from jsonrpc2 import JsonRpc
 >>> rpc = JsonRpc()
 >>> rpc['add'] = lambda a, b: a + b
 >>> rpc({'jsonrpc': '2.0', 'method': 'add', 'id': 'rpc-1', 'params': {'a': 2}}, b=3)
 {'jsonrpc': '2.0', 'id': 'rpc-1', 'result': 5}

handle errors
=================

::

 >>> from jsonrpc2 import JsonRpc
 >>> class MyException(Exception):
 ...     pass
 >>> def my_rpc():
 ...     raise MyException()
 >>> rpc = JsonRpc({'call': my_rpc}, {MyException: -32001})
 >>> rpc({'jsonrpc': '2.0', 'method': 'call', 'id': 'rpc-1', 'params': []})
 {'jsonrpc': '2.0', 'id': 'rpc-1', 'error': {'message': '', 'code': -32001, 'data': '[]'}}