I've been implementing XML-RPC handlers under Quixote, and have found
myself writing stereotyped code. The function I keep repeating looks
like this:
def rpc (request):
# Get contents of POST body
data = ...
# Parse arguments
params, method = xmlrpclib.loads(data)
try:
result = rpc_process(method, params)
except:
# report exception back to client
...
else:
return xmlrpclib.dumps(result)
rpc_process() then looks like this:
def rpc_process (method, params):
if method == 'get_authtoken':
user, password, device = params
user_db = base.get_user_database()
elif ...
All this scaffolding is needed so the actual methods which do the work
can be simple:
def get_ticket (user, service):
return "ticket"
So what would a natural Quixote-like interface for this would be?
Here's a suggestion:
from quixote import xmlrpc
_q_exports = ['RPC2'] # or whatever...
def get_ticket (arg1, arg2):
...
def check (rule):
...
xmlrpc_funcs = ['get_ticket', 'check', ...]
RPC2 = xmlrpc.XMLRPCExporter(xmlrpc_funcs)
The exporter would need to do some fairly evil work, peeking in the
calling frame's locals to get the functions corresponding to each
name. Alternatively you could pass a list of (string, function) tuples
containing the method name and the function to call.
Thoughts?
--amk (www.amk.ca)
Logopolis is a cold place, a cold high place overlooking the universe.
-- The Master, in "Logopolis"