Ejemplo n.º 1
0
    def handle(self, request_string, dispatcher):
        """
        Method brings syntactic sugar into library.
        Given dispatcher it handles request (both single and batch) and handles errors.
        Request could be handled in parallel, it is server responsibility.

        :param request_string: JSON string.
            Will be converted into JSONRPCSingleRequest or JSONRPCBatchRequest
        :type request_string: str
        :type dispatcher: Dispatcher or dict
        :rtype: JSONRPCSingleResponse or JSONRPCBatchResponse
        """

        try:
            data = self.deserialize(request_string)
            if isinstance(data, list):
                request = JSONRPCBatchRequest(
                    data, serialize_hook=self.serialize_hook)
            elif isinstance(data, dict):
                request = JSONRPCSingleRequest(
                    data, serialize_hook=self.serialize_hook)
            else:
                raise JSONRPCInvalidRequestException
        except (TypeError, ValueError, JSONRPCParseException):
            return JSONRPCParseError().as_response()
        except JSONRPCInvalidRequestException:
            return JSONRPCInvalidRequest().as_response()
        else:
            return request.process(dispatcher)
Ejemplo n.º 2
0
    def handle(self, request_string, dispatcher):
        """
        Method brings syntactic sugar into library.
        Given dispatcher it handles request (both single and batch) and handles errors.
        Request could be handled in parallel, it is server responsibility.

        :param request_string: JSON string.
            Will be converted into JSONRPCSingleRequest or JSONRPCBatchRequest
        :type request_string: str
        :type dispatcher: Dispatcher or dict
        :rtype: JSONRPCSingleResponse or JSONRPCBatchResponse
        """

        try:
            data = self.deserialize(request_string)
            if isinstance(data, list):
                request = JSONRPCBatchRequest(data, serialize_hook=self.serialize_hook)
            elif isinstance(data, dict):
                request = JSONRPCSingleRequest(data, serialize_hook=self.serialize_hook)
            else:
                raise JSONRPCInvalidRequestException
        except (TypeError, ValueError, JSONRPCParseException):
            return JSONRPCParseError().as_response()
        except JSONRPCInvalidRequestException:
            return JSONRPCInvalidRequest().as_response()
        else:
            return request.process(dispatcher)
Ejemplo n.º 3
0
 def test_response_iterator(self):
     requests = [
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"}),
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"}),
     ]
     batch = JSONRPCBatchRequest(requests)
     for request in batch:
         self.assertIsInstance(request, JSONRPCSingleRequest)
         self.assertIsInstance(batch, JSONRPCBatchRequest)
         self.assertEqual(request.method, "devide")
Ejemplo n.º 4
0
 def test_batch_request_from_list_dicts(self):
     request = JSONRPCBatchRequest([
         {"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"},
         {"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"},
     ])
     self.assertEqual(json.loads(request.json), [
         {"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"},
         {"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"},
     ])
     self.assertTrue(request)
Ejemplo n.º 5
0
    def test_from_json_batch_one(self):
        req_data = {"method": "add", "params": [1, 2], "jsonrpc": "2.0", "id": 1}

        request = JSONRPCSingleRequest(req_data)
        requests = JSONRPCBatchRequest(request)
        self.assertIsInstance(requests, JSONRPCBatchRequest)
        self.assertEqual(len(requests), 1)
        r = requests[0]
        self.assertIsInstance(r, JSONRPCSingleRequest)
        self.assertEqual(r.method, "add")
        self.assertEqual(r.params, [1, 2])
        self.assertEqual(r.id, 1)
        self.assertFalse(r.is_notification)
Ejemplo n.º 6
0
    def test_from_json_batch(self):
        str_json = json.dumps([
            {"method": "add", "params": [1, 2], "jsonrpc": "2.0"},
            {"method": "mul", "params": [1, 2], "jsonrpc": "2.0"},
        ])

        requests = JSONRPCBatchRequest(str_json)
        self.assertIsInstance(requests, JSONRPCBatchRequest)
        for r in requests:
            self.assertIsInstance(r, JSONRPCSingleRequest)
            self.assertTrue(r.method in ["add", "mul"])
            self.assertEqual(r.params, [1, 2])
            self.assertEqual(r.id, None)
            self.assertTrue(r.is_notification)
Ejemplo n.º 7
0
 def test_incorrect_batch_request(self):
     with self.assertRaises(JSONRPCInvalidRequestException):
         JSONRPCBatchRequest(1.3)
Ejemplo n.º 8
0
 def test_returned_type_butch_response(self):
     request = JSONRPCBatchRequest([{'jsonrpc': '2.0', 'method': 'add', 'params': [], 'id': 0}])
     response = self.manager.handle(request.json, self.dispatcher)
     self.assertIsInstance(response, JSONRPCBatchResponse)