is good software. It's a Python library for the 68 methods in the Flickr API in 48 lines of code. Such parsimony! Here's the nut of it:
def __getattr__(self, method):
  def method(_self=self, _method=method, **params):
    _method = _method.replace("_", ".")
    url = HOST + PATH + "?method=%s&%s&api_key=%s" %
          (_method, urlencode(params), self.api_key)
    try: rsp = xmltramp.load(url)
    except: return None
    return _self._parseResponse(rsp)
If you're not familiar with Python, what's going on here is a bit of metaprogramming genius. The __getattr__ makes it so that automatically means "invoke the Flickr API call foo", for any API call. The method() code builds a URL out of the Python method name foo and the parameters the method was invoked with, then does a GET to Flickr. Because of the consistent naming of the Flickr REST API this all works like you'd expect.

Parsing the response data is done by passing the Flickr XML to xmltramp, a quick and dirty Python XML binding that does what you want. The _parseResponse() method (not shown) does error handling in four lines.

I love how simple and Pythonic this is. Data is loosely typed. API calls are built dynamically. Everything is so abstract that the library code doesn't even have to be updated when new API methods or datatypes are added to Flickr. Clean, simple, perfect for scripting. It's about the exact opposite of SOAP and WSDL where you typically use thousands of lines of generated code to call the service. The WSDL approach gives you strong typing, which can be helpful, but can also be a dual edged sword.

  2005-05-15 19:59 Z