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:
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.
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.
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.
- In the ‘Implement’ section provide the endpoint as http://appserver.dev.cloud.wso2.com/t/backstage/webapps/customerservice-default-SNAPSHOT/services/customers/customerservice
- 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.
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.
- 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:
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
Post a Comment