Skip to content

Commit d0e9302

Browse files
smagafurovpavlov99
authored andcommitted
Ability to get parsed json-rpc request
1 parent 6ad4ae3 commit d0e9302

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

jsonrpc/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def __init__(self, **kwargs):
6969
if 'result' not in kwargs and 'error' not in kwargs:
7070
raise ValueError("Either result or error should be used")
7171

72+
self.request = None # type: JSONRPCBaseRequest
73+
7274
@property
7375
def data(self):
7476
return self._data

jsonrpc/manager.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def handle_request(cls, request, dispatcher):
8080
return
8181

8282
if isinstance(request, JSONRPC20BatchRequest):
83-
return JSONRPC20BatchResponse(*responses)
83+
response = JSONRPC20BatchResponse(*responses)
84+
response.request = request
85+
return response
8486
else:
8587
return responses[0]
8688

@@ -95,19 +97,21 @@ def _get_responses(cls, requests, dispatcher):
9597
9698
"""
9799
for request in requests:
98-
def response(**kwargs):
99-
return cls.RESPONSE_CLASS_MAP[request.JSONRPC_VERSION](
100+
def make_response(**kwargs):
101+
response = cls.RESPONSE_CLASS_MAP[request.JSONRPC_VERSION](
100102
_id=request._id, **kwargs)
103+
response.request = request
104+
return response
101105

102106
try:
103107
method = dispatcher[request.method]
104108
except KeyError:
105-
output = response(error=JSONRPCMethodNotFound()._data)
109+
output = make_response(error=JSONRPCMethodNotFound()._data)
106110
else:
107111
try:
108112
result = method(*request.args, **request.kwargs)
109113
except JSONRPCDispatchException as e:
110-
output = response(error=e.error._data)
114+
output = make_response(error=e.error._data)
111115
except Exception as e:
112116
data = {
113117
"type": e.__class__.__name__,
@@ -119,13 +123,13 @@ def response(**kwargs):
119123

120124
if isinstance(e, TypeError) and is_invalid_params(
121125
method, *request.args, **request.kwargs):
122-
output = response(
126+
output = make_response(
123127
error=JSONRPCInvalidParams(data=data)._data)
124128
else:
125-
output = response(
129+
output = make_response(
126130
error=JSONRPCServerError(data=data)._data)
127131
else:
128-
output = response(result=result)
132+
output = make_response(result=result)
129133
finally:
130134
if not request.is_notification:
131135
yield output

jsonrpc/tests/test_examples20.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99

1010
from ..manager import JSONRPCResponseManager
11+
from ..jsonrpc2 import JSONRPC20Request, JSONRPC20BatchRequest
1112

1213
if sys.version_info < (2, 7):
1314
import unittest2 as unittest
@@ -171,3 +172,35 @@ def test_rpc_call_batch_all_notifications(self):
171172
]"""
172173
response = JSONRPCResponseManager.handle(req, self.dispatcher)
173174
self.assertEqual(response, None)
175+
176+
def test_rpc_call_response_request(self):
177+
req = '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}' # noqa
178+
response = JSONRPCResponseManager.handle(req, self.dispatcher)
179+
self.assertTrue(isinstance(
180+
response.request,
181+
JSONRPC20Request
182+
))
183+
self.assertTrue(isjsonequal(
184+
response.request.json,
185+
req
186+
))
187+
188+
def test_rpc_call_response_request_batch(self):
189+
req = """[
190+
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
191+
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
192+
{"jsonrpc": "2.0", "method": "subtract",
193+
"params": [42,23], "id": "2"},
194+
{"jsonrpc": "2.0", "method": "foo.get",
195+
"params": {"name": "myself"}, "id": "5"},
196+
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
197+
]"""
198+
response = JSONRPCResponseManager.handle(req, self.dispatcher)
199+
self.assertTrue(isinstance(
200+
response.request,
201+
JSONRPC20BatchRequest
202+
))
203+
self.assertTrue(isjsonequal(
204+
response.request.json,
205+
req
206+
))

0 commit comments

Comments
 (0)