Beispiel #1
0
 def unmarshal(self, data):
     try:
         d = json_decode_binary(data)
     except ValueError as e:
         raise JSONError(error=e)
     if not isinstance(d, dict):
         raise JSONError(error=_('Request must be a dict'))
     if 'method' not in d:
         raise JSONError(error=_('Request is missing "method"'))
     if 'params' not in d:
         raise JSONError(error=_('Request is missing "params"'))
     method = d['method']
     params = d['params']
     _id = d.get('id')
     if not isinstance(params, (list, tuple)):
         raise JSONError(error=_('params must be a list'))
     if len(params) != 2:
         raise JSONError(error=_('params must contain [args, options]'))
     args = params[0]
     if not isinstance(args, (list, tuple)):
         raise JSONError(error=_('params[0] (aka args) must be a list'))
     options = params[1]
     if not isinstance(options, dict):
         raise JSONError(error=_('params[1] (aka options) must be a dict'))
     options = dict((str(k), v) for (k, v) in options.items())
     return (method, args, options, _id)
Beispiel #2
0
 def unmarshal(self, data):
     try:
         d = json.loads(data)
     except ValueError as e:
         raise JSONError(error=e)
     if not isinstance(d, dict):
         raise JSONError(error=_('Request must be a dict'))
     if 'method' not in d:
         raise JSONError(error=_('Request is missing "method"'))
     if 'params' not in d:
         raise JSONError(error=_('Request is missing "params"'))
     d = json_decode_binary(d)
     method = d['method']
     params = d['params']
     _id = d.get('id')
     if not isinstance(params, (list, tuple)):
         raise JSONError(error=_('params must be a list'))
     if len(params) != 2:
         raise JSONError(error=_('params must contain [args, options]'))
     args = params[0]
     if not isinstance(args, (list, tuple)):
         raise JSONError(error=_('params[0] (aka args) must be a list'))
     options = params[1]
     if not isinstance(options, dict):
         raise JSONError(error=_('params[1] (aka options) must be a dict'))
     options = dict((str(k), v) for (k, v) in options.iteritems())
     return (method, args, options, _id)
Beispiel #3
0
class jsonserver(WSGIExecutioner, HTTP_Status):
    """
    JSON RPC server.

    For information on the JSON-RPC spec, see:

        http://json-rpc.org/wiki/specification
    """

    content_type = 'application/json'

    def __call__(self, environ, start_response):
        '''
        '''

        self.debug('WSGI jsonserver.__call__:')

        response = super(jsonserver, self).__call__(environ, start_response)
        return response

    def marshal(self,
                result,
                error,
                _id=None,
                version=VERSION_WITHOUT_CAPABILITIES):
        if error:
            assert isinstance(error, PublicError)
            error = dict(
                code=error.errno,
                message=error.strerror,
                name=unicode(error.__class__.__name__),
            )
        principal = getattr(context, 'principal', 'UNKNOWN')
        response = dict(
            result=result,
            error=error,
            id=_id,
            principal=unicode(principal),
            version=unicode(VERSION),
        )
        response = json_encode_binary(response, version)
        return json.dumps(response, sort_keys=True, indent=4)

    def unmarshal(self, data):
        try:
            d = json.loads(data)
        except ValueError, e:
            raise JSONError(error=e)
        if not isinstance(d, dict):
            raise JSONError(error=_('Request must be a dict'))
        if 'method' not in d:
            raise JSONError(error=_('Request is missing "method"'))
        if 'params' not in d:
            raise JSONError(error=_('Request is missing "params"'))
        d = json_decode_binary(d)
        method = d['method']
        params = d['params']
        _id = d.get('id')
        if not isinstance(params, (list, tuple)):
            raise JSONError(error=_('params must be a list'))
        if len(params) != 2:
            raise JSONError(error=_('params must contain [args, options]'))
        args = params[0]
        if not isinstance(args, (list, tuple)):
            raise JSONError(error=_('params[0] (aka args) must be a list'))
        options = params[1]
        if not isinstance(options, dict):
            raise JSONError(error=_('params[1] (aka options) must be a dict'))
        options = dict((str(k), v) for (k, v) in options.iteritems())
        return (method, args, options, _id)