views:

277

answers:

2

consider the following http request:

GET /defects?group-by=priority

I would like the returned collection(feed) of defects to be grouped by their priority. i.e. the returned feed consists of defects(resources) and group infromation.

I thought about something that will return the groups' titles and count before returning the collection, for example:

<content>
<Group val="High" count="567"/>
<Group val="Medium" count="437"/> 
<Group val="Low" count="19"/> 
<Defect ,,,,>
<Defect ,,,,>
<Defect ,,,,>
</content>

The problem with such representation is that the queried resource (URL) is defect so the client expects collection of Defects and not the Group element.

I guess one option for solving this problem would be to define a separate groups resource for defects i.e.:

 defects/groups?group1=priority

that will return collection of groups and their count, and then the client can query the defects resource for the data itself. But this design is cumbersome and requires extra round trips, not to mention possible consistency problems when defects was added\removed between the call to the group resource and the defects resource.

Bottom line, what is the restful way to return a collection of elements grouped by an attribute?

EDIT I first thought of that this problem should be addressed by the ATOM publishing standard. But even if ATOM had addressed it, I still need to support other representations (XML, JSON) so I am looking for a pattern more inherent in the RESTful approach.

+3  A: 

I don't think what you are asking for is possible.

If the client is expecting a collection of defects, then you can order by priority, but

a group of defects != a defect

So you can't return a collection of groups.

You could add a nasty hack to add attributes to the defect resource for priority and priority-group-size, but that seems bad to me.

I think the correct Restful ways to do it are:

  • Design the service to return groups of defects

or

  • Return defects ordered by priority, and let the client do the grouping and counting of the resources in each group.
DanSingerman
You are correct that is exactly my problem.Your second suggestion is not applicable for me as the service has to support paging as well, so I don't want the client to "collect" thousands of defects and then group them locally.I am looking for the restful way to design this service.
LiorH
In that case I think you need to use my first option, and change what the service is designed to return. You could have one groups service which returns the group size for each priority, and then the defects service would return the defects with a given priority.
DanSingerman
Grouping seems like a presentational effect to me, so I'd suggest the client should be responsible, but I see no harm in providing metadata to assist this
annakata
+1  A: 

Based on a comment to this answer, it seems like this needs to support paging. In that case, I recommend you check out AtomPub, and specifically how it handles paging. Perhaps the same general approach will work for you; if you're really lucky, you can leverage the entire standard.

Hank Gay