Started fleshing out doodles in xmlrpc.execute()

This commit is contained in:
Jason Gerard DeRose 2008-11-25 11:54:51 -07:00
parent 2d458a1233
commit 7350ccbffe
3 changed files with 54 additions and 4 deletions

View File

@ -18,14 +18,24 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Dispatcher for RPC server.
Execute an RPC request.
"""
from xmlrpclib import dumps, loads, Fault
from ipalib import Backend
from ipalib.errors import CommandError
from ipalib.errors import HandledError, CommandError
from ipalib.rpc import xmlrpc_wrap, xmlrpc_unwrap
def params_2_args_options(params):
assert type(params) is tuple
if len(params) == 0:
return (tuple(), dict())
if type(params[-1]) is dict:
return (params[:-1], params[-1])
return (params, dict())
class xmlrpc(Backend):
def dispatch(self, method, params):
@ -34,4 +44,17 @@ class xmlrpc(Backend):
self.info('Received RPC call to %r', method)
if method not in self.Command:
raise CommandError(name=method)
params = xml_unwrap(params)
(args, options) = params_2_args_options(xmlrpc_unwrap(params))
result = self.Command[method](*args, **options)
return (xmlrpc_wrap(result),)
def execute(self, data, ccache=None, client_ip=None, locale=None):
try:
(params, method) = loads(data)
response = self.dispatch(method, params)
except Exception, e:
if not isinstance(e, HandledError):
e = UnknownError()
assert isinstance(e, HandledError)
response = Fault(e.code, e.message)
return dumps(response)

View File

@ -125,6 +125,9 @@ class HandledError(StandardError):
"""
Base class for errors that can be raised across a remote procecdure call.
"""
code = 1
def __init__(self, message=None, **kw):
self.kw = kw
if message is None:
@ -132,12 +135,22 @@ class HandledError(StandardError):
StandardError.__init__(self, message)
class UnknownError(HandledError):
"""
Raised when the true error is not a handled error.
"""
format = _('An unknown internal error has occurred')
class CommandError(HandledError):
format = _('Unknown command %(name)r')
class RemoteCommandError(HandledError):
format = 'Server at %(uri)r has no command %(command)r'
format = 'Server at %(uri)r has no command %(name)r'
class UnknownHelpError(InvocationError):

View File

@ -26,6 +26,20 @@ from ipalib import errors, Command
from ipa_server import rpc
def test_params_2_args_options():
"""
Test the `ipa_server.rpc.params_2_args_options` function.
"""
f = rpc.params_2_args_options
args = ('Hello', u'world!')
options = dict(one=1, two=u'Two', three='Three')
assert f(tuple()) == (tuple(), dict())
assert f(args) == (args, dict())
assert f((options,)) == (tuple(), options)
assert f(args + (options,)) == (args, options)
assert f((options,) + args) == ((options,) + args, dict())
class test_xmlrpc(PluginTester):
"""
Test the `ipa_server.rpc.xmlrpc` plugin.