Web User
Web User

Reputation: 7736

Combining results of multiple API calls

I am invoking an API operation to fetch a list of objects and then, for each object in that list, I am invoking another API operation to fetch additional details of that object and adding those details into the object. The goal is to return a list of objects with all the properties (or details) that I need. In the example below, the /allObjects call will get a list of all objects with 2 properties - id and k1. But my application needs 3 properties, id, k1 and k4. So I invoke another API method to get detailed information for each object, which includes the k4 property. I copy that detailed property into the original result-set's object and return the "enriched" result-set. But the latency cost of this can be high. What is the best way of achieving this without slowing down the application too much? In the real world, I am dealing with 500-2000 objects.

GET /allObjects yields JSON results =>
{
  "data" : [
    {
      "id" : "123",
      "k1" : "v1"
    }, {
      "id" : "456",
      "k1" : "v1"
    }
  ]
}

for (obj in results.data) {
  GET /object/{obj.id} yields JSON result =>
  {
    "data" : {
      "id" : "123",
      "k1" : "v1",
      "k2" : "v2",
      "k3" : "v3",
      "k4" : "v4"
    }
  }
  // Add k4 property to original result-set object, and assign
  // it the value of the k4 property in the individual result object.
  obj.k4 = result.data.k4;
}

return results.data;

Upvotes: 1

Views: 1987

Answers (1)

Dumidu Handakumbura
Dumidu Handakumbura

Reputation: 426

Your requirement is such that, you have no other option but to go for a mash-up(unless you can convince the API developers to combine the two API).

You could, however, opt to make a mashup service with a low latency cost to stand in between your application to abstract out the mashup logic, conversely, you could opt to use a language that is custom made for this kind of work to program your application. Both of these options can be accommodated with Ballerina, I've written a post here showing how easy it is to do that using it.

Upvotes: 2

Related Questions