A quick demonstration of the described_routes discovery protocol is in order. It’s all done with link headers, one per resource.
Starting with some arbitrary resource in the application:
$ curl --silent --head http://localhost:3000/users/dojo | grep Link Link: ; rel="describedby"; meta="ResourceTemplate" $ curl --silent --head http://localhost:3000/described_routes/user?user_id=dojo | grep Link Link: ; rel="index"; meta="ResourceTemplates"
So after the first HEAD request we have the location of the resource’s ResourceTemplate metadata; after the second we have the location of the site ResourceTemplates (plural) metadata.
A HEAD request to the site metadata location merely confirms (via the meta tag) what we already knew:
$ curl --silent --head http://localhost:3000/described_routes | grep Link Link: ; rel="self"; meta="ResourceTemplates"
On the root resource, a single HEAD request links us to the site metadata location directly:
$ curl --silent --head http://localhost:3000 | grep Link Link: ; rel="describedby"; meta="ResourceTemplates"
After that, it’s up to the client.
When you create a path-to client thus:
app = PathTo::DescribedRoutes::Application.discover("http://localhost:3000/users/dojo")
path-to discovers the site metadata and GETs it, specifying JSON as the acceptable format. The JSON response is converted to basic Ruby objects and used to initialize the Application object app. The client now knows the application’s resource structure:
app.users['dojo'].uri => "http://localhost:3000/users/dojo"
and we’re back where we started!
Tags: described_routes, link_header, path-to
[...] http://positiveincline.com/?p=480 [...]
[...] Positive Incline Mike Burrows (asplake) moving on up « One link to rule them all [...]
[...] and by PathTo clients in order to discover and interact with application resources. All of this works in Ruby already of course; my current personal project is to replicate it in Python with support [...]