author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 12 Sep 2017 18:32:13 +0300 | |
changeset 1801 | 58ff55053518 |
parent 1780 | c52d1460cea8 |
child 1832 | 0f1081928d65 |
permissions | -rw-r--r-- |
371 | 1 |
import gluon.contrib.simplejson as simplejson |
2 |
import types |
|
3 |
import sys |
|
4 |
||
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset
|
5 |
|
371 | 6 |
class JSONRPCServiceBase: |
7 |
||
8 |
def __init__(self): |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
9 |
self.methods = {} |
371 | 10 |
|
11 |
def response(self, id, result): |
|
1740
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
12 |
return simplejson.dumps({'version': '1.1', 'id': id, |
b789b695b5c6
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
13 |
'result': result, 'error': None}) |
1751
c28db6f7616b
clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset
|
14 |
|
371 | 15 |
def error(self, id, code, message): |
1773
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
16 |
return simplejson.dumps({ |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
17 |
'id': id, |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
18 |
'version': '1.1', |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
19 |
'error': {'name': 'JSONRPCError', |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
20 |
'code': code, |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
21 |
'message': message} |
38fde37c3766
clean-up: fix PEP8 E124 closing bracket does not match visual indentation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset
|
22 |
}) |
371 | 23 |
|
24 |
def add_method(self, name, method): |
|
25 |
self.methods[name] = method |
|
26 |
||
27 |
def process(self, data): |
|
28 |
data = simplejson.loads(data) |
|
29 |
id, method, params = data["id"], data["method"], data["params"] |
|
30 |
if method in self.methods: |
|
31 |
try: |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
32 |
result = self.methods[method](*params) |
371 | 33 |
return self.response(id, result) |
34 |
except BaseException: |
|
35 |
etype, eval, etb = sys.exc_info() |
|
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
36 |
return self.error(id, 100, '%s: %s' % (etype.__name__, eval)) |
1780
c52d1460cea8
clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1773
diff
changeset
|
37 |
except Exception: |
371 | 38 |
etype, eval, etb = sys.exc_info() |
1742
92932cd370a4
clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset
|
39 |
return self.error(id, 100, 'Exception %s: %s' % (etype, eval)) |
371 | 40 |
else: |
41 |
return self.error(id, 100, 'method "%s" does not exist' % method) |
|
42 |
||
43 |
def listmethods(self): |
|
1730
64d8f52bc8c8
clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
728
diff
changeset
|
44 |
return self.methods.keys() |