Hypermedia APIs on Rails
blog.stateless.co
blog.stateless.co
Even better, when you hit the API with a browser, you can click around and explore the links easily:
http://restframework.herokuapp.com/users/
The docs talk about it a bit here:
http://django-rest-framework.org/tutorial/5-relationships-an...
So,
…
{
'id': 123,
'url': '/resource/123'
},
…
instead of …
'/resource/123',
'/resource/124',
…
or worse …
123,
124,
…
Also, Django REST Framework is easily my favorite REST API tool for Django. It's very straightforward to use just as much or just as little of it as necessary. In fact, it's powering the main views of the upcoming second draft of http://marquee.by (the entire site is effectively a browsable API).Adding hypermedia reflection to JSON does amazing things to APIs. We've been doing this via oData for awhile, and it makes things so much easier on the client. Anyone building a hypermedia/REST API with JSON (or XML) would make good of using standard formats that incorporate this functionality and attitude.
Also, why tie this idea to Rails at all? Why not just make your own plugin or Sinatra app or frameowrk or whatever?
Rails has API defaults which should include sensible linking defaults we can build tooling around.
TL;DR - there's no value in discoverable (hypermedia) APIs short of some generic API browser that end users don't really want.
> I think the original idea behind discoverability was that you could have a “RESTful client” that could in theory work with any REST service that implemented discoverability.
Your analysis of that conclusion makes sense (a generic API browser is not very useful), but why did you think that's "the original idea behind discoverability"?
The BitNative article you link to gives the reason that is usually cited for using hypermedia:
> This allows the API to be highly evolvable because it avoids creating a coupling between the client and the server.
Even more important IMO is that it makes it easier for the many different parties to serve the same API.
Think RSS: clients and servers are decoupled.
* It's self-documenting. Client developers can find all the endpoints just by clicking around (instead of reading mountains of docs).
* Client apps don't need to keep a list of hard-coded urls for random access, removing one of the most brittle parts of client apps (they should know about rels of course, but those end up being easier to keep track of).
Once you actually use an API like this, other APIs feel like they're in the stone age and how to do things with them seems like a continual guessing game. And it's still simple as hell -- remember it's just json with links. It's not like that requires a lot of extra effort.
Not sure about other languages, but most of the client apps I make don't use hardcoded urls - they use a base API url, and then modifications for specific resources. The RestSharp library for C# is a great example of how this works, and even in javascript it's not hard to refactor things so they use a base url and append path & parameters based on the models you're working with.
I'm not arguing the simplicity of it, although it would be more simple to implement if we agreed on a standard like in the original article. I'm just arguing the value.
The only reason I'm aware of is for compatibility with JSONP. Github's API normally uses Link headers, but adds a "meta" object for JSONP requests: http://developer.github.com/v3/#json-p-callbacks
DHH has simply never taken REST/Hypermedia seriously as a design philosophy. He, and Rails, have been content to move simple JSON serializations over the wire and leave it at that. This was good enough a few years ago, but is getting to the end of the runway for the present and future.