Implementing a service mashup with WSO2 API Manager

Chanaka Fernando
WSO2 Best Practices
4 min readJun 9, 2018

--

WSO2 API Manager is one of the leading open source API management platforms available in the market. According to a recent Gartner research (2018) it has been identified as the best “visionary” type vendor in the market. It comes with a support for full API lifecycle management , horizontal and vertical scalability and deployment options of on-premise, public cloud (SaaS) and managed (private) cloud. In this article, I’m going to discuss about how you can implement a service mashup (or service orchestration) with WSO2 API Manager within 10 minutes.

Let’s get started by downloading the WSO2 API Manager from the following link.

https://wso2.com/api-management/install/

Once you downloaded the product, you can install it to a desired location. Let’s consider the directory which WSO2 API Manager is intalled as “APIM_HOME”. You can start the product using the following command within the APIM_HOME directory.

$ sh bin/wso2server.sh

Before implementing the use case, let’s understand the scenario with the below image.

WSO2 API Manager service mashup

In this scenario, we have 2 backend microservices called “Service1” and “Service2” and they produce the following results.

Service1 = {“id”:200,”name”:”IBM”,”price”:234.34}

Service2 = {“name”:”IBM”,”industry”:”technology”,”CEO”:”John Doe”,”Revenue”:”23.5 billion USD”}

Now we need to mashup these two responses to produce a result similar to below.

{results: [{“id”:200,”name”:”IBM”,”price”:234.34}, {“name”:”IBM”,”industry”:”technology”,”CEO”:”John Doe”,”Revenue”:”23.5 billion USD”}]}

Let’s see how we can achieve this requirement with WSO2 API Manager. You can log in to the publisher portal and start creating an API as depicted below.

  • Create a new API by clicking on “Add API” button and then selecting the option 3 which is “Design a new REST API”. That will bring the below mentioned interface where you need to configure the API definition.
  • Once the above interface is filled with the values depicted above, click on the “Next: Implement”. This will bring you to the interface where you need to configure the API implementation logic. Here you need to configure the URL of “service1” as the endpoint URL and then select “Enable Message Mediation” option to upload the service mashup logic which is implemented as a custom mediation policy.

We are going to implement the service mashup logic within a custom mediation policy which is implemented using the “synapse” mediation language using XML. The mashup logic is shown below.

In the above sequence, we are saving the response of the first endpoint call to a property called “response1” and then call the second endpoint “service2” and save the result in another property called “response2”. After that, using a payload factory mediator, we are mashing up the 2 responses and creating the final response. Then using the <respond> mediator we are sending back the response.

You need to upload this custom sequence as the “outFlow” of this API as depicted in the above image.

  • Once the above mediation sequence is uploaded, click on “Next: Manage” button and select the “Unlimited” subscription tier and click “Save and Publish” button to publish the API as depicted in the below image.

Now the API is created and published to the API store. Now let’s go and start the backend services. These services are implemented in Ballerina programming language. Following are the source code of these 2 services.

Once these 2 service are started, they will be running on the below URLs.

http://localhost:9090/service1

http://localhost:9091/service2

  • Let’s log in to the API Store and subscribe to this API using the default application and execute the API using the generated access token.

Click on the “Applications” tab and generate access token to consume the API.

Now we have the access token to execute the API. Let’s send a CURL request to get the result.

curl -d “{\”name\”:\”WSO2\”}” -H “Content-Type: application/json” -X POST -H “Authorization: Bearer d30d7e25-bb40–3e70–86b0–714f86784cd2” http://localhost:8281/mashup/v1

You will get the below result.

{results: [result1:{“id”:200,”name”:”IBM”,”price”:234.34}, result2:{“name”:”IBM”,”industry”:”technology”,”CEO”:”John Doe”,”Revenue”:”23.5 billion USD”}]}

That’s all. You can do the mashup of the results based on your requirement by modifying the payload factory mediator.

--

--

Chanaka Fernando
WSO2 Best Practices

Writes about Microservices, APIs, and Integration. Author of “Designing Microservices Platforms with NATS” and "Solution Architecture Patterns for Enterprise"