dynamic method calls

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")

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.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s