コード例 #1
0
ファイル: rpcserver.py プロジェクト: wladich/freeipa
 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)
コード例 #2
0
ファイル: rpc.py プロジェクト: zhoubh/freeipa
    def __request(self, name, args):
        print_json = self.__verbose >= 2
        payload = {'method': unicode(name), 'params': args, 'id': 0}
        version = args[1].get('version', VERSION_WITHOUT_CAPABILITIES)
        payload = json_encode_binary(
            payload, version, pretty_print=print_json)

        if print_json:
            logger.info(
                'Request: %s',
                payload
            )

        response = self.__transport.request(
            self.__host,
            self.__handler,
            payload.encode('utf-8'),
            verbose=self.__verbose >= 3,
        )

        if print_json:
            logger.info(
                'Response: %s',
                json.dumps(json.loads(response), sort_keys=True, indent=4)
            )

        try:
            response = json_decode_binary(response)
        except ValueError as e:
            raise JSONError(error=str(e))

        error = response.get('error')
        if error:
            try:
                error_class = errors_by_code[error['code']]
            except KeyError:
                raise UnknownError(
                    code=error.get('code'),
                    error=error.get('message'),
                    server=self.__host,
                )
            else:
                kw = error.get('data', {})
                kw['message'] = error['message']
                raise error_class(**kw)

        return response['result']
コード例 #3
0
    def __request(self, name, args):
        payload = {'method': unicode(name), 'params': args, 'id': 0}
        version = args[1].get('version', VERSION_WITHOUT_CAPABILITIES)
        payload = json_encode_binary(payload, version)

        if self.__verbose >= 2:
            root_logger.info('Request: %s',
                             json.dumps(payload, sort_keys=True, indent=4))

        response = self.__transport.request(
            self.__host,
            self.__handler,
            json.dumps(payload),
            verbose=self.__verbose >= 3,
        )

        try:
            response = json_decode_binary(json.loads(response))
        except ValueError, e:
            raise JSONError(str(e))
コード例 #4
0
 def unmarshal(self, data):
     try:
         d = json.loads(data)
     except ValueError, e:
         raise JSONError(error=e)
コード例 #5
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)