'RESTful search using external APIs

I'm trying to develop a Node RESTful service that works as a unified adapter for other APIs. Users have a 'favorites' list to which they can add books by pressing a 'star' button on Book objects. These Book objects are retrieved by client through service API endpoint:

=> GET servi.ce/api/search?text=query

To fulfil the request, service calls other APIs (libraries, bookstores, etc) and aggregates the results into an array of normalized objects with Book schema. So the response looks like this:

{
   results: [
      {
         isbn: 3245236,
         title: '1984',
         ...
      },
   ],
}
  

Aggregated results aren't saved anywhere in the service (!) and just relayed back to client. This means when users want to add a Book to their favorites list there are several options:

  1. Take the whole received object and send it to the according endpoint as POST request => POST servi.ce/api/favorites/. This will validate and save a received object into the database (with reference for user).
  2. Ignore (!) and create a 'search cache' collection on the backend to save last search queries results in the database for a few minutes and send back the objects with included cache IDs. In this case client has to send only the ID of a cached object and backend will move it to the permanent database as well as reference it for user's favorites list.
  3. /your brilliant idea here/

The second way looks way easier on bandwidth, as objects can be quite big and the backend already had them at some point. However, it also feels like a crime against RESTful principles and state-lessness of a server (or not..?).

I can't choose which tradeoff to make here. Maybe your expertise includes a better way.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source