Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "lazy load" in a RESTful manner?

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.

like image 760
Marcus Leon Avatar asked Dec 03 '22 14:12

Marcus Leon


2 Answers

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>
like image 145
Darrel Miller Avatar answered Dec 25 '22 17:12

Darrel Miller


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.

like image 39
Hank Gay Avatar answered Dec 25 '22 18:12

Hank Gay