Allowing client to select data to return via REST interface

Posted by CMP on Programmers See other posts from Programmers or by CMP
Published on 2012-11-27T22:43:45Z Indexed on 2012/11/27 23:28 UTC
Read the original article Hit count: 158

Filed under:
|

I have a rest service that is essentially a proxy to a variety of other services. So if I call

GET /users/{id}

It will get their user profile, as well as order history, and contact info, etc... all from various services, and aggregates them into one nice object. My problem is that each call to a different service has the potential to add time to the original request, so we would rather not get ALL the data ALL of the time if a particular client does not care about all of the pieces.

A solution I have arrived at is to do something like this:

GET /users/{id}?includeOrders=true&includeX=true&includeY=true...

That works, and it allow me to do only what I need to, but it is cumbersome. We have added enough different data sources that there are too many parameters for that style to be useful. I could do something similar with a single integer and a bitmask or something, but that only makes it harder to read, and it does not feel very Restful.

I could break it down into multiple calls so they would need to call /users/{id}/orders and /users/{id}/profile separately, but that sort of defeats the purpose of an aggregating proxy, who's purpose is to make clients jobs easier.

Are there any good patterns that can help me return just enough data for each client, without making it too difficult for them to filter and select what they want?

© Programmers or respective owner

Related posts about rest

Related posts about soa