API Aggregation Using Azure API Management

 

API Aggregation Using Azure API Management  

 

 

There are cases where we need to call multiple APIs from the client at the same time in order to proceed further with the processing from the client application. Now as oppose to calling those APIs individually i.e., making separate HTTP requests for all those multiple APIs what if we can make one HTTP request and get all the responses aggregated in one response as opposed to multiple response isn't it going to be super cool. 

 

 

So, in this post we will try to understand how we can aggregate/ merge multiple API responses from the backend and provide one single request and response from the Azure API Management (APIM) to the calling client application. 

 

 

To do this first of all we need to understand our APIs request body which we want to merge using APIM, so let say we want to call 2 APIs and their URI are like this. 

 

 

 

 

 

 

So as we can see here the request for the APIs are userId. 

 

 

Now we will have to create a variable named as say "userId" which we will use to pass the input parameter required by the APIs. We can do so by adding like this in the Inbound Processing >> Code View 

 

 

<set-variable name="userIdvalue="@(context.Request.Url.Query["userId"].Last())" />   

 

 

Now that we have the input variable available next step is to create the send-request policy for both our APIs which we need to call internally. send-request policy has few important attributes which we will have to understand. 

 

 

  • response-variable-name : Temporary variable to store the API response. 

  • set-url : API endpoint which needs to be called to fetch the data. 

  • set-method : Provide the Http Verb like GET, POST for the calling API endpoint. 

  • set-header : Provided there are any header values required by the API endpoints, the same can be provided over here. 

So here is what we will have for the send-request policy in order to call both the APIs and store them in respective temporary response variables. 

 

 

<send-request mode="new" response-variable-name="userProfile" timeout="20" ignore-error="true">   

<set-method>GET</set-method>   

<set-header name="Content-Type" exists-action="override">   

<value>application/x-www-form-urlencoded</value>   

</set-header>>   

</send-request>   

 

 

 

<send-request mode="new" response-variable-name="pendingTasks" timeout="20" ignore-error="true">   

<set-method>GET</set-method>   

<set-header name="Content-Type" exists-action="override">   

<value>application/x-www-form-urlencoded</value>   

</set-header>   

</send-request>  

 

 

Now, finally we need to collect the data response from both the APIs and send it back to the actual APIM Http request. To do so we will need to have a return-response policy. And the policy will look something like following: 

 

 

<return-response>   

            <set-status code="200" reason="OK" />   

            <set-header name="Content-Type" exists-action="override">   

                <value>application/json</value>   

            </set-header>   

            <set-body>@(new JObject(new JProperty("userProfile",((IResponse)context.Variables["userProfile"]).Body.As<JObject>()),   

                            new JProperty("pendingTasks",((IResponse)context.Variables["pendingTasks"]).Body.As<JObject>())   

                            ).ToString())</set-body>   

        </return-response>   

 

 

 

 

Now that we have completed all the necessary steps required, we can start consuming the API using APIM and we should get 2 sections on the API response named userProfile and pendingTasks with the data from respective APIs. 

  

 

Hope you liked the post, do provide inputs in the comment, also if you think the article will be helpful for any of your friends and relatives then please do share the article with them. 

  

And don't forget to subscribe for the new post available so that you get notified on your mailbox. 

  

Happy Learning. 📚 

 
 

 

Comments

Popular posts from this blog

Disaster Recovery of Azure API Management (APIM)

What is Cloud Computing?