HATEOAS with WADL
Craig asked me how you would describe something like the Sun Cloud APIs with WADL and I thought others might be interested in the answer. A key feature of the cloud APIs is that they make good use of hypertext as the engine of application state or HATEOAS for (relatively) short. What this means is that rather than documenting a set of URIs and URI templates and relying on the client to construct URIs to access the resource they need, the cloud APIs only publish a single "root" URI and then document where to find additional URIs in representations that clients can use to traverse the service. This is much closer to how a browser user traverses links in web pages. In a recent blog entry Craig describes the advantages of HATEOAS.
So, how to describe this kind of Web application in WADL ? The key is to:
- Use resource types to describe each kind of resource,
- Parameterize representations to identify links embedded within them,
- Define the types of resource each embedded link identifies.
To illustrate I'll use the Sun cloud APIs and the root level representation of the VDC. In WADL we describe this top level resource as follows:
<resource path="/" type="#vdc"/>
The above declares a top level resource of type "vdc" at the URI "http://example.com/". Next we need to describe the resource type "vdc":
<parameter name="attach" path="vms.attach" style="plain">
<parameter name="detach" path="vms.detach" style="plain">
<parameter name="backup" path="vms.backup" style="plain">
The above says that a representation of resources of type "vdc" can be obtained with GET and that such representations contain links to other types of resources. I've shown three links here but in reality a VDC document contains many more. Each link is contained within a parameter that describes where in the representation the link can be found. In this case each link is to the same type of resource that essentially acts like a push button, here's the description of that resource type:
The above says that resources of type "button" only support POST and return an empty 204 response on success.
I've glossed over one important issue related to the use of JSON for the representation of a VDC. If the representation used XML then the value of the path attribute of the parameter element is an XPath and it has semantics for selecting a set of nodes. For JSON theres no such standard path language (that I'm aware of) so I've had to improvise. The "vms" property of the vdc JSON object is an array and the "" in the path attribute values is meant to signify that the path will match multiple properties of the JSON object, if anyone has a better idea then let me know.