示例#1
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")

            if method not in self.methods:
                # Check if requested method is signed as *rpcmethod*
                _method = getattr(self, method, None)
                if (_method and callable(_method)
                        and getattr(_method, "rpcmethod", False)):
                    self.methods[method] = _method

            if method not in self.methods:
                # Method not found error
                responses.append(
                    rpcresponse.Response(
                        jsonrpc=jsonrpc,
                        id=id,
                        error=rpcerror.MethodNotFound(
                            data=u"Method name: '%s'" % method)))

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                result = rpc_function(*positional_params, **named_params)
                # No return value is OK if we don´t have an ID (=notification)
                if result is None:
                    if id:
                        responses.append(
                            rpcresponse.Response(
                                jsonrpc=jsonrpc,
                                id=id,
                                error=rpcerror.InternalError(
                                    data=u"No result from JSON-RPC method.")))
                else:
                    # Successful response
                    responses.append(
                        rpcresponse.Response(jsonrpc=jsonrpc,
                                             id=id,
                                             result=result))
            except TypeError, err:
                traceback_info = "".join(
                    traceback.format_exception(*sys.exc_info()))
                if "takes exactly" in unicode(err) and "arguments" in unicode(
                        err):
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc=jsonrpc,
                            id=id,
                            error=rpcerror.InvalidParams(data=traceback_info)))
                else:
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc=jsonrpc,
                            id=id,
                            error=rpcerror.InternalError(data=traceback_info)))
            except rpcerror.JsonRpcError, err:
                responses.append(
                    rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=err))
示例#2
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")

            if method not in self.methods:
                # Check if requested method is signed as *rpcmethod*
                _method = getattr(self, method, None)
                if (
                    _method and
                    callable(_method) and
                    getattr(_method, "rpcmethod", False)
                ):
                    self.methods[method] = _method

            if method not in self.methods:
                # Method not found error
                error = rpcerror.MethodNotFound(
                    data = u"Method name: '%s'" % method
                )
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = error
                    )
                )
                # Logging error
                logging.error(
                    u"{error} -- {data}".format(
                        error = safe_unicode(error),
                        data = safe_unicode(error.data)
                    )
                )

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                result = rpc_function(*positional_params, **named_params)
                responses.append(
                    rpcresponse.Response(jsonrpc = jsonrpc, id = id, result = result)
                )
            except TypeError, err:
                traceback_info = rpcerror.get_traceback_string()
                if "takes exactly" in safe_unicode(err) and "arguments" in safe_unicode(err):
                    error = rpcerror.InvalidParams(data = traceback_info)
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = error
                        )
                    )
                    # Logging error
                    logging.error(
                        u"{error} -- {data}".format(
                            error = safe_unicode(error),
                            data = safe_unicode(error.data)
                        )
                    )
                else:
                    error = rpcerror.InternalError(data = traceback_info)
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = error
                        )
                    )
                    # Logging error
                    logging.error(
                        u"{error} -- {data}".format(
                            error = safe_unicode(error),
                            data = safe_unicode(error.data)
                        )
                    )
            except rpcerror.JsonRpcError, err:
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = err
                    )
                )
                # Logging error
                logging.error(
                    u"{error} -- {data}".format(
                        error = safe_unicode(err),
                        data = safe_unicode(err.data)
                    )
                )
示例#3
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")

            if not method in self.methods:
                # Check if requested method is signed as *rpcmethod*
                _method = getattr(self, method, None)
                if (
                    _method and
                    callable(_method) and
                    getattr(_method, "rpcmethod", False)
                ):
                    self.methods[method] = _method

            if not method in self.methods:
                # Method not found error
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = rpcerror.MethodNotFound(
                            data = u"Method name: '%s'" % method
                        )
                    )
                )

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                result = rpc_function(*positional_params, **named_params)
                # No return value is OK if we don´t have an ID (=notification)
                if result is None:
                    if id:
                        responses.append(
                            rpcresponse.Response(
                                jsonrpc = jsonrpc,
                                id = id,
                                error = rpcerror.InternalError(
                                    data = u"No result from JSON-RPC method."
                                )
                            )
                        )
                else:
                    # Successful response
                    responses.append(
                        rpcresponse.Response(jsonrpc = jsonrpc, id = id, result = result)
                    )
            except TypeError, err:
                traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
                if "takes exactly" in unicode(err) and "arguments" in unicode(err):
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = rpcerror.InvalidParams(data = traceback_info)
                        )
                    )
                else:
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = rpcerror.InternalError(data = traceback_info)
                        )
                    )
            except BaseException, err:
                traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
                if hasattr(err, "data"):
                    error_data = err.data
                else:
                    error_data = None
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = rpcerror.InternalError(
                            data = error_data or traceback_info
                        )
                    )
                )
示例#4
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")

            if method not in self.methods:
                # Check if requested method is signed as *rpcmethod*
                _method = getattr(self, method, None)
                if _method and callable(_method) and getattr(_method, "rpcmethod", False):
                    self.methods[method] = _method

            if method not in self.methods:
                # Method not found error
                error = rpcerror.MethodNotFound(data=u"Method name: '%s'" % method)
                responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                # Logging error
                logging.error(u"{error} -- {data}".format(error=safe_unicode(error), data=safe_unicode(error.data)))

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                result = rpc_function(*positional_params, **named_params)
                responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, result=result))
            except TypeError as err:
                traceback_info = rpcerror.get_traceback_string()
                if "takes exactly" in safe_unicode(err) and "arguments" in safe_unicode(err):
                    error = rpcerror.InvalidParams(data=traceback_info)
                    responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                    # Logging error
                    logging.error(u"{error} -- {data}".format(error=safe_unicode(error), data=safe_unicode(error.data)))
                else:
                    error = rpcerror.InternalError(data=traceback_info)
                    responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                    # Logging error
                    logging.error(u"{error} -- {data}".format(error=safe_unicode(error), data=safe_unicode(error.data)))
            except rpcerror.JsonRpcError as err:
                responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=err))
                # Logging error
                logging.error(u"{error} -- {data}".format(error=safe_unicode(err), data=safe_unicode(err.data)))
            except StandardError as err:
                traceback_info = rpcerror.get_traceback_string()
                if hasattr(err, "data"):
                    error_data = err.data
                else:
                    error_data = None
                error = rpcerror.InternalError(
                    message=safe_unicode(err), data=safe_unicode(error_data or traceback_info)
                )
                responses.append(rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                # Logging error
                logging.error(u"{error} -- {data}".format(error=safe_unicode(error), data=safe_unicode(error.data)))

        # Convert responses to dictionaries and filter it
        responses_ = []
        for response in responses:
            if bool(response.id) or bool(unicode(response.id)) if response.id is not None else False:
                responses_.append(response.to_dict())
        responses = responses_

        # Return as JSON-string (batch or normal)
        if responses:
            if len(requests) == 1:
                return rpcjson.dumps(responses[0])
            elif len(requests) > 1:
                return rpcjson.dumps(responses)
示例#5
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")
            if not method in self.methods:
                # Method not found error
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = rpcerror.MethodNotFound(
                            data = u"Method name: '%s'" % method
                        )
                    )
                )

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                (result, centec_error_msg) = rpc_function(*positional_params, **named_params)
                # No return value is OK if we don´t have an ID (=notification)
                if result is None:
                    if id:
                        responses.append(
                            rpcresponse.Response(
                                jsonrpc = jsonrpc,
                                id = id,
                                error = rpcerror.InternalError(
                                    data = u"No result from JSON-RPC method."
                                )
                            )
                        )
                else:
                    #Centec
                    centec_error = None
                    if centec_error_msg != None:
                        centec_error = rpcerror.JsonRpcError(message = centec_error_msg['message'], data = result)
                        centec_error.code = centec_error_msg['code']
                    # Successful response
                    responses.append(
                        rpcresponse.Response(jsonrpc = jsonrpc, id = id, result = result, error=centec_error)
                    )
            except TypeError, err:
                traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
                if "takes exactly" in unicode(err) and "arguments" in unicode(err):
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = rpcerror.InvalidParams(data = traceback_info)
                        )
                    )
                else:
                    responses.append(
                        rpcresponse.Response(
                            jsonrpc = jsonrpc,
                            id = id,
                            error = rpcerror.InternalError(data = traceback_info)
                        )
                    )
            except BaseException, err:
                traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
                if hasattr(err, "data"):
                    error_data = err.data
                else:
                    error_data = None
                responses.append(
                    rpcresponse.Response(
                        jsonrpc = jsonrpc,
                        id = id,
                        error = rpcerror.InternalError(
                            data = error_data or traceback_info
                        )
                    )
                )
示例#6
0
    def call(self, json_request):
        """
        Parses the *json_request*, calls the function(s)
        and returns the *json_response*.

        :param json_request: JSON-RPC-string with one or more JSON-RPC-requests

        :return: JSON-RPC-string with one or more responses.
        """

        # List for the responses
        responses = []

        # List with requests
        requests = rpcrequest.parse_request_json(json_request)
        if not isinstance(requests, list):
            requests = [requests]

        # Every JSON-RPC request in a batch of requests
        for request in requests:

            # Request-Data
            jsonrpc = request.jsonrpc
            id = request.id
            method = request.get("method", "")

            if method not in self.methods:
                # Check if requested method is signed as *rpcmethod*
                _method = getattr(self, method, None)
                if (_method and callable(_method)
                        and getattr(_method, "rpcmethod", False)):
                    self.methods[method] = _method

            if method not in self.methods:
                # Method not found error
                error = rpcerror.MethodNotFound(data="Method name: '%s'" %
                                                method)
                responses.append(
                    rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                # Logging error
                logging_error_str = "{error} -- {data}"
                logging.error(
                    logging_error_str.format(error=safe_unicode(error),
                                             data=safe_unicode(error.data)))

                continue

            # split positional and named params
            positional_params, named_params = request.get_splitted_params()

            # Call the method with parameters
            try:
                rpc_function = self.methods[method]
                result = rpc_function(*positional_params, **named_params)
                responses.append(
                    rpcresponse.Response(jsonrpc=jsonrpc, id=id,
                                         result=result))
            except TypeError as err:
                traceback_info = rpcerror.get_traceback_string()
                if "takes exactly" in safe_unicode(
                        err) and "arguments" in safe_unicode(err):
                    error = rpcerror.InvalidParams(data=traceback_info)
                    responses.append(
                        rpcresponse.Response(jsonrpc=jsonrpc,
                                             id=id,
                                             error=error))
                    # Logging error
                    logging_error_str = "{error} -- {data}"
                    logging.error(
                        logging_error_str.format(error=safe_unicode(error),
                                                 data=safe_unicode(
                                                     error.data)))
                else:
                    error = rpcerror.InternalError(data=traceback_info)
                    responses.append(
                        rpcresponse.Response(jsonrpc=jsonrpc,
                                             id=id,
                                             error=error))
                    # Logging error
                    logging_error_str = "{error} -- {data}"
                    logging.error(
                        logging_error_str.format(error=safe_unicode(error),
                                                 data=safe_unicode(
                                                     error.data)))
            except rpcerror.JsonRpcError as err:
                responses.append(
                    rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=err))
                # Logging error
                logging_error_str = "{error} -- {data}"
                logging.error(
                    logging_error_str.format(error=safe_unicode(err),
                                             data=safe_unicode(err.data)))
            except Exception as err:
                traceback_info = rpcerror.get_traceback_string()
                if hasattr(err, "data"):
                    error_data = err.data
                else:
                    error_data = None
                error = rpcerror.InternalError(
                    message=safe_unicode(err),
                    data=safe_unicode(error_data or traceback_info))
                responses.append(
                    rpcresponse.Response(jsonrpc=jsonrpc, id=id, error=error))
                # Logging error
                logging_error_str = "{error} -- {data}"
                logging.error(
                    logging_error_str.format(error=safe_unicode(error),
                                             data=safe_unicode(error.data)))

        # Convert responses to dictionaries and filter it
        responses_ = []
        for response in responses:
            if (bool(response.id) or bool(unicode(response.id))
                    if response.id is not None else False):
                responses_.append(response.to_dict())
        responses = responses_

        # Return as JSON-string (batch or normal)
        if responses:
            if len(requests) == 1:
                return rpcjson.dumps(responses[0])
            elif len(requests) > 1:
                return rpcjson.dumps(responses)