API design question – group response entities on server or push logic to client?

Hey folks,

I came across this question while at work today. Let's say you have an API /animals which will return animals with their types (mammal, reptile etc.).

Sample query: GET /animals
Sample response:

[ { 'name': 'fifo', 'type': 'mammal', // other attributes }, { 'name': 'polly', 'type': 'reptile', // other attributes }, // etc. ] 

Is it a good idea to instead return a sample response grouped by the type of animal? Something like:

[ 'mammal': [{'name':'fifo', // other attributes}, {// some other mammal}], 'reptile': [{'name': 'polly', // other attributes}, {// some other reptile}] ] 

According to my thinking, the former response makes more sense since in that case we are not restricting the format of the API response – the client can slice and dice it however it wants to. The second response looks like a special case that a few clients might want, but that responsibility should lie with the client instead.

One other idea I had was to support something like /animals?group=true to support both kinds of responses, but that will make the API bloated, and I don't think that's a good idea.

Do you folks have any thoughts/suggestions in both the above cases? Thanks in advance!

PS: This isn't exactly the type of data I have but its similar πŸ˜€

submitted by /u/Nrawal
[link] [comments]

from Software Development – methodologies, techniques, and tools. Covering Agile, RUP, Waterfall + more! http://bit.ly/2Wsc7b0

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close