Exemple #1
0
def parse_request_json(json_string):
    """
    Returns RPC-request as dictionary or as list with requests
    """

    # No JSON-String
    if json_string is None:
        raise rpcerror.InvalidRequest()

    # Parse
    try:
        data = json.loads(json_string)
    except JsonParseError:
        traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
        raise rpcerror.ParseError(data=traceback_info)

    # Create request(s)
    if isinstance(data, list):
        requests = []
        for item in data:
            requests.append(
                Request(jsonrpc=item.get("jsonrpc"),
                        method=str(item.get("method", "")),
                        id=item.get("id"),
                        params=item.get("params", [])))
        return requests
    else:
        return Request(jsonrpc=data.get("jsonrpc"),
                       method=str(data.get("method", "")),
                       id=data.get("id"),
                       params=data.get("params", []))
    def from_string(cls, json_string):
        """
        Parses the Json-string and returns a Request-object or a
        list with Request-objects.

        :returns: Request-object or list with Request-objects

        :rtype: Request
        """

        # No JSON-String
        if not json_string:
            raise rpcerror.InvalidRequest()

        # Parse
        try:
            data = rpcjson.loads(json_string)
        except rpcjson.JsonParseError:
            traceback_info = "".join(traceback.format_exception(*sys.exc_info()))
            raise rpcerror.ParseError(data = traceback_info)

        # Create request(s)
        if isinstance(data, list):
            requests = []
            for item in data:
                requests.append(cls(
                    jsonrpc = item.get("jsonrpc"),
                    method = item.get("method"),
                    id = item.get("id"),
                    params = item.get("params")
                ))
            return requests
        else:
            return cls(
                jsonrpc = data.get("jsonrpc"),
                method = data.get("method"),
                id = data.get("id"),
                params = data.get("params")
            )