Given this service to get information about a hotel:
> GET /hotel/{id}
< HTTP/1.1 200 OK
< <hotel>
< <a>aaa</a>
< <b>aaa</b>
> <biggie>aaa....I am 300K</biggie >
< </hotel>
Problem is that biggie
is 300K and we don't want to return it with every response. What's the RESTful way to lazy load this value?
Should we set up two resources:
> GET /hotel/{id}
< HTTP/1.1 200 OK
< <hotel>
< <a>aaa</a>
< <b>aaa</b>
< </hotel>
and..
> GET /hotel/{id}/biggie
< HTTP/1.1 200 OK
< <biggie>
< <val>aaa....I am 300K</val>
< </biggie>
And you only request GET /hotel/{id}/biggie
when you really need that data?
This would work.. although there is nothing special about biggie
except that it's a large data set. I think it's nicer to keep everything at the hotel
level as all the attributes are really just attributes of the hotel
.
Don't forget, hypermedia is your friend.
GET /hotel/{id}
HTTP/1.1 200 OK
<hotel Id="99">
<a>aaa</a>
<b>aaa</b>
<biggieLink href="/Hotel/99/Biggie"/>
</hotel>
or you can even do
GET /hotel/{id}
HTTP/1.1 200 OK
<hotel Id="99">
<a>aaa</a>
<b>aaa</b>
<biggieSynopsis href="/Hotel/99/Biggie">
<title>Here is a a summary of biggie</title>
</biggieSynopsis
</hotel>
Setting it up as two resources would work, but if you don't like that, you could look into using caching; depending on the nature of the data, that might actually save you more load than splitting into multiple resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With