It occurred to me today that there are two important ideas in designing a REST API.
The more familiar of the two is the hypermedia constraint. The client uses a bookmark to load a starting point, and from there navigates the application protocol by examining links provided by the server, and choosing which link to follow.
That gives the server a lot of control, as it can choose which possible state transitions to share with the client. Links can be revealed or hidden as required; the client is programmed to choose from among the available links. Because the server controls the links, the server can choose the appropriate request target. The client can just "follow its nose"; which is to say, the client follows a link by using a request target selected by the server.
The second idea is that the request targets proposed by the server are not arbitrary. HTTP defines cache invalidation semantics in RFC 7234. Successful unsafe requests invalidate the clients locally cached representation of the request target.
A classic example that takes advantage of this is collections: if we POST a new item to a collection resource, then the cached representation of the collection is automatically invalidated if the server's response indicates that the change was successful.
Alas, there is an asymmetry: when we delete a resource, we don't have a good way to invalidate the collections that it may have belonged to.
No comments:
Post a Comment