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

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...

Exposing a SOAP service as a REST API

In this post i will be explaining how we can transform a SOAP based backend to receive requests in a restful manner through the WSO2 API Cloud. Steps. First log into the WSO2 Cloud and navigate to the API Cloud. In the API cloud select the option to add a new API. We will be creating an API to demonstrate an invocation to the backend soap service ws.cdyne.com/phoneverify/phoneverify.asmx?wsdl Give a name, context and version to the API and add a resource name with a GET Method. The resource name can be anything which you like since we will invoke the actual service usiing a custom sequence. Mention the URI template as indicated in the below screenshot. Next go to the implement tab. And select the endpoint type as HTTP/SOAP Endpoint and specify the endpoint as http://ws.cdyne.com/phoneverify/phoneverify.asmx. There is an important step we need to do here. We need to set the SOAP version for this request. In order to do that we need to select the advanced option ...

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...