REST design for API accessing multiple resources -
imagine api returns json data tv listings app zap2it tv listings.
it's list of tv channels , each channel shows on , beyond. currently, have api returns channels get /channels. however, there need add show on each channel in data. thinking of adding new api, get /channels/on_now, differentiate current api. i want clear new api, don't want make individual call each channel, show-on-now data needs returned channels. rest api design?
current get /channels json data
[ "channel": { "channelname": "kron4", }, "channel": { "channelname": "ktov5", }, ... ] expected json data new api get /channels/on_now below
[ { "channel": { "channelname": "kron4", }, "on_now": { "starttime": "2012-06-04t11:30:00", "endtime": "2012-06-04t12:00:00", "shortdescription": "latest local, statewide & national news events, along sports & weather.", "shorttitle": "4:30am newscast" } }, { "channel": { "channelname": "ktov5", }, "on_now": { "starttime": "2012-06-04t11:30:00", "endtime": "2012-06-04t12:30:00", "shortdescription": "local morning news , weather report", "shorttitle": "morning newscast" } }, ...next channel... ]
i advice concentrate on content, not on urls.
example: you've got entry point, '/'. url in api. on return st like
{ "channels" : { "href" : "path.to/channels" }, "programs" : { "href" : "path.to/programs" } } to retrieve list of channels, on corresponding url - don't need know before - , obtain, example:
[ { "name" : "bbc", "id" : 452, "href" : "path.to/channels/452" }, { "name" : "foo", "id" : 112, "href" : "path.to/channels/112" } ] for detailled information bbc, on provided url:
{ "name" : "bbc", "id" : 452, "self" : "path.to/channels/452", "live_url" : "link.to.bbc.cast", "whatever" : "bar", "current" : "path.to/channels/452/current", "program" : "path.to/channels/452/program" } and on. urls discovered on fly; free modify them anytime. makes api content: have agree clients returned (fields, types, ...). call "current" url above obtain information current program.
read here more: http://kellabyte.com/2011/09/04/clarifying-rest/
edit after op-comment:
you introduce 'embed' parameter limit amount of requests:
get path.to/channels/452?embed=current would return:
{ "name" : "bbc", "id" : 452, "self" : "path.to/channels/452", "live_url" : "link.to.bbc.cast", "whatever" : "bar", "current" : { "self" : "path.to/channels/452/current", "name" : "morning show", "start_time" : "(datetime here)", "end_time" : "(datetime here)", "next" : "whatever.comes.ne/xt" }, "program" : "path.to/channels/452/program" }
Comments
Post a Comment