I am implementing a REST web service and I'm wondering if there is any convention on how should information exposed by the service be atomized. So far I've been unable to find a satisfactory solution so now I'm asking to the greatest minds ever found in the Internet :P
For example, let's suppose I want to implement an API to access to some sort of library registry, so we have a resource with path /books
. By reading /books/123
we would get, for instance, the XML data below:
<?xml version="1.0" encoding="utf-8"?>
<book>
<title>Visit Catalonia</title>
<author>J. Hans</author>
<year>2000</year>
</book>
Imagine the author information were wrong and I wanted to change it thru the web service's API. I come up with two possible options, each one with advantages and drawbacks:
OPTION 1:
Issue a HTTP PUT method to /books/123
with the data below:
<?xml version="1.0" encoding="utf-8"?>
<book>
<author>G. Robinson</author>
</book>
Advantages:
- It's only necessary to implement 1 resource handler: /books/BOOKID
- We can modify as many pieces of information as XML tags are defined in the data (e.g.: adding in the code above we would update the book's year too).
Drawbacks:
- GET method always returns all data about a book; we cannot access just a single piece of information. In this example getting all data is not an issue, but what if
/book/123
also had a tag with the book's content itself in order to read it online?
OPTION 2:
Issue a HTTP PUT method to /books/123/author
sending the data:
<?xml version="1.0" encoding="utf-8"?>
<author>G. Robinson</author>
Advantages:
- Information can be accessed in an arbitrary level of depth: the API client can just download the piece of information he/she needs.
Drawbacks:
- We can't get or modify several pieces of information with a single HTTP request.
- Implementation is tough: as many subresources as information fields.
So, summarizing, my question is if there's any commonly accepted convention to expose information in a web service. Of course, in simple web services that manage little amount of data this is not an issue, but what if we are exposing a real world database?
Thank you very much in advance.