dynamic method calls

13 03 2007

As you may know I’m working on updating sm-photo-tool from XMLPRC to REST. I wanted similar functionality with the REST implementation as with XMLRPC. That is I wanted to be able to do things like this:
sm = Smugmug()
sm.login.withPassword(”foo@foo.com”, “password”)

I couldn’t find a REST library I liked probably because it’s trivial as all you need is a properly formatted url. I knew it was possible to do the above with XMLRPC in python:
sm = ServerProxy("url")
sm.login.withPassword(…)

I looked through the xmlrpclib.py and stole the _Method implementation from it (with a slight modification):
class _Method:
# some magic to bind an XML-RPC method to an RPC server.
# supports “nested” methods (e.g. examples.getStateName)
def __init__(self, send, name):
self.__send = send
self.__name = name
def __getattr__(self, name):
return _Method(self.__send, “%s.%s” % (self.__name, name))
def __call__(self, **args):
print “__name: %s” % self.__name
print “args: ” + str(args)
return self.__send(self.__name, args)

Now I can make calls to Smugmug’s REST api.
sm = Smugmug()
sm.smugmug.login.withPassword(EmailAddress=”foo@foo.com”, Password=”foo”)

Next stop is to get the parsing of the XML response.


Actions

Information

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>