The great Linode is preparing an api for customers to manipulate their account and twiddle their dns zone settings etc. Apparently I was supposed to write the Python bindings, the reality is somewhere between volunteer and victim I suspect. So I spent a little bit time working up an example how they might work.
A little background on the API, it is generously simple, you can submit via GET or POST, send your api_key in HTTP auth or in the GET/POST, and you can get results in JSON/WDDX/Human readable formats. It’s still a bit in flux as far as what methods are available etc. The bindings are deliciously simple (if I do say so myself), however they rely on a certain amount of evil to the casual python fan. When designing the python bindings I wanted a way to quickly add and remove api methods (re: flux), but also minimize the copy and paste effort required. The flow for a request boils down to create an http request, determine the action (the api method), append necessary state vars, submit, read response, return json decoded result. The system I’ve devised uses the powerful python decorators to achieve this.
Here’s an example of how a method is defined in the api:
Couple of notes about the above method definition:
The privateness of the decorator relates to another hack utilized elsewhere to determine the list of available api methods quickly (the reason for this will be in the follow up post) Because there’s no other real instructions to this method the doc string declaration is important so as not to annoy the python block definitions (stupid whitespace idiocy [I’m more of a ruby lover at heart]) You may ask, how is it exactly that you achieve such simple definitions of methods? Python decorators of course! Decorators are a way of manipulating a function/method before the actual function/method is invoked. Let’s look at the definition of __api_request
You’ll notice that the input to the method __api_request is the method we’re decorating, and what’s returned is a separate callable method that we’ve defined dynamically. Oh the wonder of it all! There’s more than just your normal bit of trickery going on here, if you wanted to determine the number of arguments that were passed to domainList() you’d change the parameters for “def decorator”, and the number and types of parameters don’t even have to match what we defined as domainList() anyway. In fact, we defined domainList initially to take self and request, however we called it with no parameters. That’s why my decorator function uses
**__kw, it’s a heavy handed var_args for python.
The rest of the decorator function relates to how the internals of generating an api request work, it translates
func.__name__ into the action to be performed, takes any named parameters
[method(ParamName=foo)] adds them the dictionary (it also looks for 1 dictionary argument that may or may not be present and adds any of those parameters to the request, the reason for this will be discussed in a later blog). The decorator then calls the original method definition, checks to see if anything was returned, and then calls the base
__send_message and returns that result to the callee. Phew!
The reason the decorator calls the original method is to allow for some input sanitization and other sanity checks before allowing the request to go out. You could add default values or check for more complex relationships between required parameters. Speaking of required parameters, you can also use another decorator to achieve that:
Checks the named parameter list (and the odd regular argument) for the required parameters and raises an exception if they’re not found, pretty snazzy. To use it:
If you’re interested in seeing the rest of the code you can visit http://github.com/tjfontaine/linode-python or clone it yourself with
git clone git://github.com/tjfontaine/linode-python.git