Using WSO2 ESB Connectors in the Mediation Flow
WSO2 ESB supports a rich set of Connectors which enabled it to seamlessly integrate with various third party services. WSO2 API Manager can be used to manage the API of the connector which is exposed via WSO2 ESB. The following describes the high level steps to expose the Open Weather Map connector added to WSO2 ESB via the API Manager.
What is described in this page is a high level use case of using a connector in the mediation flow, using an example. For in depth details and samples in using connectors, please see the documentation on WSO2 ESB and supported Connectors.
- Download the Open Weather Map connector from WSO2 Connector Store.
- Download the start the WSO2 ESB with a port offset of 1.
- Import the Open Weather Map Connector to ESB following the documentation on Working with Connectors via the Management Console.
- Create an account in Open Weather Map service, and obtain the API key as explained in Configuring OpenWeatherMap Operations.
Create a Rest API in WSO2 ESB which will initialize the Open Weather Map connecor and invokes it. The following sample sequence invokes the getCurrentWeatherForOneLocByCityName operation:
<api xmlns="http://ws.apache.org/ns/synapse" name="OpenWeatherMapAPI" context="/openWeatherMapApi"> <resource methods="POST GET" url-mapping="/*"> <inSequence> <property name="apiUrl" expression="json-eval($.apiUrl)"/> <property name="apiKey" expression="json-eval($.apiKey)"/> <property name="apiVersion" expression="json-eval($.apiVersion)"/> <property name="cityName" expression="json-eval($.cityName)"/> <property name="countryCode" expression="json-eval($.cityCountry)"/> <openweathermap.init> <apiUrl>{$ctx:apiUrl}</apiUrl> <apiKey>{$ctx:apiKey}</apiKey> <apiVersion>{$ctx:apiVersion}</apiVersion> </openweathermap.init> <openweathermap.getCurrentWeatherForOneLocByCityName> <cityName>{$ctx:cityName}</cityName> <cityCountry>{$ctx:cityCountry}</cityCountry> </openweathermap.getCurrentWeatherForOneLocByCityName> <respond/> </inSequence> </resource> </api>
For the complete list of supported operations, please refer the documentation on Configuring OpenWeatherMap Operations.
- Expose this ESB API from API Manager, by creating an API in API Publisher and providing the ESB API as the endpoint.
Subscribe to the API created from API manager and invoke it. The apiKey should be the key obtained by creating an account in the Open Weather Map site. A sample payload is shown below:
{ "apiUrl":"http://api.openweathermap.org", "apiKey":"xxxxxxxxxxxxxxxxxxxxxxxxxxx", "apiVersion":"2.5", "cityName":"London", "cityCountry":"uk" }