Multiple api calls vs more complex apis

jsonmobileswift-languageweb-api

I have been providing a Web API (.NET MVC 5) of an Oracle Database to people who are making a mobile app. I have to admit that I am not very good at it. I have found that I can easily produce Data Transfer Objects to make very simple JSON displays.

My question is whether it is feasible to ask the mobile developers to make multiple calls to the API to get the data they need. This would be in opposition to me providing a more complex model that provides all the info in ONE call.

As a pure example… if we were dealing with animals, in my API they would need to call both the category (api/categories) and the animal name (api/animals) and link the 2 together depending on their needs:

…..call 1 to category_table

[{
CATEGORY_ID: 1,
CATEGORY_NAME: "Dog"
},
{
CATEGORY_ID: 2,
CATEGORY_NAME: "Cat"
}]

…..call 2 to animal_table

[{
ANIMAL_ID: 1,
CATEGORY_ID: 2,
NAME: Tabby
},
{
ANIMAL_ID: 2,
CATEGORY_ID: 2,
NAME: Lion
},
{
...etc,etc,
}
]

The other option would be to use more complex JSON utilizing arrays so that they only make one call. That I am having some difficulty with. At the moment the APIs are very simple so they may have to call a few APIs. I tried directly using the model that I had created from the DB but ended up with humongous amounts of data being produced (arrays inside arrays inside arrays… etc, etc,). I had to use DTOs but I am having difficulty using arrays.

The question is whether it is feasible to have those developers make multiple calls instead of a more complex JSON response. They will be programming for IOS mobile devices using swift.

In other words I could place an array of each animal into the categories call (it was maybe a simplistic example). I am not talking about putting the category_name in the api/animals… that would be easy! I mean some form of associative arrays.

Best Answer

It's a matter of satisfying DRY as much as possible. You say your service only has one client, an iOS app, but what happens when someone decides that an Android app or web app should be written? How much of the logic is that second app going to have to duplicate because of how you wrote the service?

Write your service in such a way as to reduce duplicated logic as much as possible. That means that if your API requires all clients to call categories before they can call animals, then you are forcing all clients to duplicate logic.

Yes, this means more work for you, the beleaguered service provider, but it's better to have one programmer in charge of one piece of logic built in one place than have multiple programmers each in charge of handling that one piece of logic in several different places.

Related Topic