tags:

views:

294

answers:

3

How would one model resources that are hierarchical? Assume for example that a person has a "message board" and that a "message" is a resource. Let's say that the "message" can have replies so that it forms a discussion thread. How does one model the notion of a thread?

Does the "message" include it's children? Is the "thread" it's own resource? Finally, what kind of REST URI would work in this instance?

A: 

How about:

# View message with all replies
GET messages/id/

# Create a new reply to a message with form data
POST /messages/id/replies/

# View a reply, possibly in the context of its original message
GET /replies/id/

# Delete comment spam
DELETE replies/id/

and so on...

Modelling a situation in which every reply can be replied to is more complicated, but the idea would be the same.

There's also Railscasts # 139, which shows one way to flatten nested resources. It's Rails-specific, but the basic approach can be used anywhere.

Rich Apodaca
This is RPC, not REST, since you are defining URIs out-of-band.
Wahnfrieden
Interesting. Do you have a reference?
Rich Apodaca
Yes, Roy Fielding: "A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server). Servers must have the freedom to control their own namespace. Instead, allow servers to instruct clients on how to construct appropriate URIs, such as is done in HTML forms and URI templates, by defining those instructions within media types and link relations. [Failure here implies that clients are assuming a resource structure due to out-of band information, such as a domain-specific standard, which is the data-oriented equivalent to RPC's functional coupling]."
Wahnfrieden
How would you fix the example I give to conform to REST based on your interpretation (feel free to post as a separate answer to the original question).
Rich Apodaca
... Alternatively, which existing Web API(s) would you cite as conforming to your interpretation of REST?
Rich Apodaca
Unfortunately it's been hard for me to find examples of real REST APIs. An obvious one is AtomPub though. Also, this isn't really just my interpretation, since the Fielding quote agrees and he's the one that has specified the REST architecture.
Wahnfrieden
+2  A: 

If you consider that all replies to a message are also messages I would give an id to every one of them and use these URIs:

#message {id}  (only the message, no replies)
/messages/{id}

#replies to the {id} message (a list of the id's of the replies)
/messages/{id}/replies

To create a new message do a post to the uri

 /messages

To create a reply to a message {id} do a post to

/messages/{id}/replies

Update

I will modify here my previous answer. Hopefully this time in a correct Restful style.

You have an entry point uri, let's call it {messages}.

GET {messages} -> list of uris of all the messages, {message1}, {message2}, etc.

GET {message1} -> responds with the message1 document, for example in xml it could be:

<message responses="{link to message1 responses}">
    <date>...</date>
    <body>...</body>
</message>

{link to message1 responses} is the link that the client has to follow to get the list of uris of the responses.

If a message is a response to another it will include that in his content, for example

<message responses="{link to message1 responses}" inResponseTo="{uri}" >
    <date>...</date>
    <body>...</body>
</message>

Now to add new messages just post it to the original {messages} uri. If the message is a response to other message just include it in its content (note that this is an efective change to the inital answer where you post the replies to an special uri).

To modify some message do a PUT to its uri.

All the uris coudl follow thats of the first part of the answer, but this is not necessary.

DaniCE
I'd have /messages/{id}/replies contain links to the replies instead of reply ids -- it's better to avoid having the client build URIs where possible.
dF
You are absolutely right... that was a bad habit from the pre-Rest times...
DaniCE
This is wrong. Relying on URI naming conventions is against everything REST is about. You should only have an entry point like /messages/ which gives the entire URI to other message resources inside its response, and then each message resource should also send the client the URI of each reply message resource, etc. It needs to all be done through hypertext.
Wahnfrieden
I don't mention it but I assumed that a GET to /messages will respond with a list to all messages (or threads, thats a design decision). as the first commend says this will better contain the uris of the replies, so the client don't has no "URI naming convention" as you say
DaniCE
My response is only a suggestion on *how to organize uris in the server*. This kind of hierarchy is enforced by all the server rest libraries I know. You don't give any answer about how to do a reply to a message, so your response is incomplete
DaniCE
Fair enough. But this is a rather trivial issue and unimportant compared to how to present appropriate responses in hypertext, which it sounds like the OP does not seem to understand. He actually asked specifically about making his resources RESTful - and REST has nothing to do with URI format. Your answer is off-topic, even though he mistakenly asked about "RESTful URIs" (which is a nonexistent concept).
Wahnfrieden
I have updated my response based in your comments
DaniCE
Thanks, that looks better to me now. Just to clarify though, the only URI that can be in his published API is the entry point, in order to be RESTful.
Wahnfrieden
A: 

So far every response has not been RESTful at all. REST doesn't need to be hierarchical. Just have an entry point like /threads/ which gives full URIs of each thread resource, and each thread resource would respond with the URIs of each message in itself, or the top message plus the URIs of its replies, and so on. The way these URIs are generated is immaterial as long as they are discoverable via hypertext from an entry point.

Wahnfrieden
To clarify, you cannot define URIs as part of your API if you are going to call your API RESTful.
Wahnfrieden
Instead of just saying that everything is NOT REST, why don't you *show* what REST would look like?
pbreitenbach
What part don't you understand? If you want me to explain REST to you, I suggest you read Fielding's dissertation.
Wahnfrieden
How's about provide an example instead?
Daniel T.
If you want an intro to REST, there are several questions on stackoverflow already which address it.
Wahnfrieden