anonymvs anonymvs - 11 months ago 62
reST (reStructuredText) Question

Business logic and restful API design

Let's assume we have a simple API allowing clients to fetch a list of items of a specific type:

GET /items/foo
GET /items/bar
GET /items/blah

A response is a list of items of the requested type, each entry has an unique ID.
The client will usually display these items in table/grid/etc.

Now in the client we must implement a pinning feature so another API allows pinning/unpinning items based on their ID & their type. So I was discussing with my colleagues possibilities to inform the client about which items are pinned or not.

An option was to have another API
GET /pinning/{type}
to return the list of all the pinned items of a specified type.

Another solution was to use a similar API
GET /pinning/{type}
to return the list of the IDs of all the pinned items. Let the client sort it out.

The first solution was accepted. Their argument was that the backend is responsible for business logic and that the client shouldn't be involved in business logic so the client should just display data it receives from the server. This argument didn't sell it for me. I'm thinking the server should in this case provide the data that allows the client to perform additional presentation logic.

Which solution is better? Or what other solutions are possible?

Answer Source

If the server would only return ItemIds at GET /pinning/{type}, the client would have to repeatedly call something like GET /items/{itemId} in order to obtain data it can display on the UI, right? This in turn would just increase the load on the server. If the id would be enough, you can probably get away with the proposed solution. Since both the client and the server seem to be under the same umbrella (as in your company is also the API consumer), you have enough information to make a decision.

Even if it were a Public API with lots of clients I would still go down the route of returning items instead of just itemIds - probably in a paged manner, for performance reasons.