Saturday, September 08, 2012

7Digital API wrapper in Python

I've been looking at the developer API from 7Digital, the digital media delivery company, and realized that I needed a lightweight, python-friendly wrapper for the API requests and responses.

And so I wrote one. It's called py7D, and it's here: https://github.com/jasonrubenstein/python-7Digital
and the README is here: https://github.com/jasonrubenstein/python-7Digital#readme


Why
I wanted a very thin layer of code that wasn't dependent on ORM-like structures. I don't believe that abstracting out each and every API method/function call to a method on an object makes it "easier for the programmer". If anything, it makes it a pain in the ass to maintain the library every time 7Digital comes out with a new API method/function. I also wanted to keep the separation between function and data cleaner, more obvious, and clearly intended.

Ok, enough with the negative comments. Lets get positive. What I wanted to do: create a library of small, clean modules that do the absolute minimum to get the data from the API; provide the responses in a python-friendly form like a dictionary or a list (rather than in XML, which is what the API returns); make no assumptions about how the data is to be consumed, and therefore just pass it on in as much of an unmodified form as possible; separate data from function and separate disparate functions from each other (for example, relocating API calls from the OAuth module to the API module).  I also wanted to do away with the use of classes where I think it's not necessary. There really wasn't any state being kept here, and the settings that could imply state weren't enough of a deciding factor to go with a class.  Also, if the requirements of a project require the maintenance of state, the modules are written such that wrapping them with a class would be trivial.

I also wanted to update the oauth library used in the existing, legacy library. I upgraded my version to use oauth2 and implemented its convenient Client class for making oauth signed requests.


What
My plan for this thing is to create a service around it. The service would handle an incoming request for information, call the API, and then convert the response from the API into some consumable form (probably a JSON string or an edited dict or list of dicts). It would also handle routing certain responses to a cache in redis.


Notes
The page for 7Digital's API is here,  and a direct link to the docs is here.