Skip to main content

An example of proxy transforming a JSON response and merging multiple responses into single response.

The API Gateway has a default mediation flow for the API invocation requests that it receives. You can extend this default mediation flow to do additional custom mediation for the messages in the API Gateway. An extension is provided as a synapse mediation sequence. You design all sequences using a tool like WSO2 Developer Studio and then add the sequences to the API.


Prerequisite

Download WSO2 Developer Studio (version 3.7.1 is used here) from http://wso2.com/products/developer-studio/ and open it by double clicking the Eclipse.app file inside the downloaded folder.

Need to have an existent account with the WSO2 API Cloud.

Steps:

  • In the normal manner create the API providing a name and context. (You can follow the tutorial here [1]) 
  • Before specifying the resource select the ‘Implement’ button. You will be asked whether to include a wild card resource select yes and proceed to the next step. 
  • Here there will be a button which asks whether you will need to include a custom sequence. Before this you will need to create the below sequences after which you can upload them in this page. 
  • Go to the Developer Studio option in the title bar and select open dashboard. 
  • Select the ESB Config Project and create a new project 
  • Right click on the project and add two sequence names . I have created the in sequence as IteratingSample and the out sequence as AggregateMediator.
  • What we will be doing in this will be to iterate the post request made by the customer id and make individual requests to the service.
  • In the In sequence It will iterate through the list. The xml file corresponding to the sample can be found here. Below is the sequence used for the sample. The expression to iterate is chosen as “//customerIds/customerId “as that is the format of the json payload we passed into the POST method. More details about the mediator can be found here
iteratingSample.png

For the out sequence we need use an aggregate mediator which will wait until and the responses are received made by the in sequence and then we aggregate all the results and return as a single response. The following is the sequence used for the out sequence. The sequence can be found here. More details about the aggregate mediator can be found here.

aggregate.png
  • Now we have completed creating the sequences, 

  • Next we need to add these two sequences to our API through the publisher 

  • In the API's implement tab you will need to add it as mentioned in this tutorials step 16. Add the IteratingSample.xml as the in sequence and the AggregateMediator.xml as the out sequence.


  • Next Manage and select the tiers. 
  • Save and Publish the API. Then go to the store and subscribe to this API and generate the access token. 
  • Go to the API Console of the API. 
  • Expand the POST method and give the payload as follows and Try this. What we have done here is sent a list of ids for which we need to retrieve the information about. 

{ "customerIds": { "customerId": [ "123", "124", "125" ] }}

You will be able to see a json response which has merged multiple responses into one response. This is what you will be able to see. The requests have been processed parallel.




Special Notes:

The endpoint [3] by default only has the payload for the customer id "123" if you need to add more entries for the respective ids you can post the values to the endpoint using the following payload format.

{ "Customer": { "name": "Sera" }}

References:

Comments

Popular posts from this blog

Adding error sequence to a custom sequence

In my previous posi i explained how to call an endpoint using the call mediator. Usually when errors are thrown from the call mediator we can handle them using a custom sequence. When call mediators fails the execution stops hence handling errors is very essential. For this post I will be modifying the same sequence which i created in the blog post [1] The change which we need to do is we need to modify the sequence definition to map with the error sequence as shown below. You can find the modified sequence here . <sequence xmlns="http://ws.apache.org/ns/synapse" name="countrySequenceWithError" onError="countryErrorSequence"> <property name="uri.var.countryCode" expression="$url:countryCode"/> <property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>     <call blocking="true">         <endpoint>             <http method="get" uri-temp...

WSO2 Cloud

Introduction WSO2 Cloud consists of three commercial cloud offerings namely the API cloud, Integration Cloud and the Identity Cloud. WSO2 Integration Cloud WSO2 Integration Cloud is a public hosted version of the powerful, 100% open-source WSO2 Enterprise Integrator. It allows you to host your cloud-to-cloud, cloud-to-on-premise integrations and API backends on a scalable cloud platform WSO2 API Cloud WSO2 API Cloud is an enterprise-ready, self-service platform that enables you to expose business capabilities as managed APIs and leverage off-the-shelf potential for advertising and selling business APIs. The API Cloud supports all aspects of an API’s lifecycle, from its creation to publication and retirement. The API Cloud is based on WSO2 API Manager , WSO2’s complete solution for designing and publishing APIs and managing a developer community. [2] WSO2 Identity Cloud WSO2 Identity Cloud leverages the functionality of WSO2 Identity Server’s identity and ac...

Invoking external endpoints using the call mediator in wso2 api manager

Introduction In API Manager if you need to do any service chaining use cases the call mediator comes in handy. If you need to use the response received by invoking one endpoint and then use it to invoke another endpoint you can use the call mediator in API Manager. The call mediator behaves in a synchronous manner. Hence, mediation pauses after the service invocation and resumes from the next mediator in the sequence when the response is received. You can read more about the call mediator in the wso2 esb documentation [1] . In api manager 1.10.0 the call mediator works in the blocking mode. Prerequisite Before we can use the call mediator in API Manager 1.10.0 we need to make the following changes to some configs. We need to comment the jms transport sender in axis2_blocking_client.xml found in the location APIM_HOME/repository/conf/axis2. This will resolve the jms sender initialization issues.   <!--transportSender name="jms"        ...