jeudi 22 décembre 2016

API design pattern to be integrated both by own web app and other systems

So this backend will be consumed by an ad-hoc front end application. But will also be integrated by other systems and we will expose API for them.

When designing the rest I see that there is ONE database table (we call it for table A) that can join many other tables, lets say about 10 to 20 other tables.

Now, my strategy would be to build routes in my backend that will "reason" according to the ad-hoc frontend we have.

So if there is a page in the frontend (let's call this page for page1) that requires to get rows from the table A but also fields from let's say 3 other join tables, then I want to create a route in the backend called maybe "page1" which will return rows from table A and also from the other 3 tables.

This is of course an ordinary way to build a backend. But as it will also be used by other systems then somebody could argue that these systems maybe don't have any need for the route "page1". Their frontend will maybe never build a "page1".

So according to people here, it would better to build the API more agnostically. And instead of creating the route "page1" I should build it according to "hateoas". And if I understand that principle, instead of letting my ad-hoc frontend to request the resource "page1" it would request "pageForTableA". And then, the resource "pageForTableA" should return which are the possible table to be joined.

In this case, for my frontend's page1, I would need to make 4 subsequent request to the server, instead of one like I would like to do if there was a "page1" resource in the backend.

What do you think?

I also see a thirt strategy. I don't know if there is a name for this pattern but it would be this way:

A resource in backend that returns only rows from table A. BUT, the route also takes arguments. And the argument is an array with the name of all the other tables someone want to include.

So if frontend calls:

getTableA(array('tableB', 'tableD', 'tableF'))

Then the resource would include/join the tables B, D and F. In short: API resource let's the frontend decide what it want to get delivered.

Which of these 3 strategies are best do you think? Or there is some more that could be taken in consideration?

Aucun commentaire:

Enregistrer un commentaire