1999 called, they want their CGI scripts back.
I think that pretty much sums up people’s response to the horror that is the SimpleDB API. By which, I mean the few of us who can tell what’s wrong with it. Which sadly is not that many people.
But offending it is, and much like MySpace, I can’t bring myself to use it. So I decided to do something about it. Subbu came up with an interesting proposal for a RESTful API for SimpleDB:
In this exercise, my starting point was Amazon’s definition of the REST API, which I refactored into a RESTful version without breaking the usage pattern. Several variations of this approach are possible, but the key point to make is that (a) it is important to identify what the resources are, and (b) then think of mapping various operations into known HTTP verbs for the API to be RESTful, without losing focus on the net benefits of building an API over HTTP. This is not hard.
Not hard at all, Subbu. I took that idea and ran with it, creating DeHorrible.
DeHorrible is a Rails proxy that RESTifies SimpleDB. Or if you insist, GETStifies your resources to use SimpleDB. Either way, it will keep your sensibilities intact. I ended up with a slightly different resource mapping:
- POST /domains, using either name query parameter or name in body, returning 201 with URL in Location.
- GET /domains, with optional limit and token query parameters, which brings you back a list of URLs, with which you can …
- DELETE /domains/[name].
- GET /domains/[name], which takes the optional query parameters query, limit and token, and brings you back a list of URLs, one for each found item.
- POST /domains/[name] to create a new item, returning 201 with the URL in Location. Requires item name and attributes, either URL-encoded, JSON hash, or XML document.
- GET /domains/[name]/items/[name] retrieves all attributes associated with that item.
- PUT /domains/[name]/items/[name] updates (replaces) attributes with new values.
- POST /domains/[name]/items/[name] adds new attribute values.
- DELETE /domains/[name]/items/[name] does what you think it will.
Once you get into attributes (attributes/[name]) you can do a few more funky things like retrieve, replace and append individual attribute values, and delete an attribute of all its value, or a given value (attributes/[name]/[value]).
It uses HTTP Basic authentication, so plug your AWS ID as login name, secret key as password, and rock:
curl http://your_id:key@localhost:3000/domains -X POST -d HelloDomain curl http://your_id:key@localhost:3000/domains/HelloDomain
So there you go. SimpleDB de-horrified. Now we can all sleep better.