In this post i'm going to discuss an API interconnecting scenario with aid of an simple use case. Here the use case is as follows. We have two apis 1. Which gives the city name when longitude, latitude coordinates are given. 2. An API which gives temperature of a city (when city name is given). Here my objective is to combine (chain) both apis and get the temperature when lon-lat is given.
First we will create the mock APIs for Map API and Weather API. To create APIs I used WSO2 Developer Studio [1]. Using DevStudio I created a JAX-RS project and deploy it on WSO2 AS[2]
Source code for Map service and Weather service is as follows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.test.map; | |
import javax.ws.rs.*; | |
@Path("/") | |
public class SimpleMap { | |
@GET | |
@Path("/getcity/") | |
@Produces("application/json") | |
public String getcity(@QueryParam("x") int x, @QueryParam("y") int y) { | |
if (x==6 && y==79) { | |
return "{\"city\":\"Colombo\"}"; | |
} | |
else if (x==7 && y==80) { | |
return "{\"city\":\"Kandy\"}"; | |
} | |
else if (x==6 && y==81) { | |
return "{\"city\":\"Galle\"}"; | |
} | |
else if (x==9 && y==80) { | |
return "{\"city\":\"Jaffna\"}"; | |
} | |
else { | |
return "{\"city\":\"Not found\"}"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.test.map; | |
import javax.ws.rs.*; | |
@Path("/") | |
public class WeatherInfo { | |
@GET | |
@Path("/gettemperature/") | |
@Produces("application/json") | |
public double gettemperature(@QueryParam("city") String city) { | |
if ("Colombo".equalsIgnoreCase(city)) { | |
return 26.25; | |
} | |
if ("Kandy".equalsIgnoreCase(city)) { | |
return 24.5; | |
} | |
if ("Galle".equalsIgnoreCase(city)) { | |
return 25.00; | |
} | |
if ("Jafna".equalsIgnoreCase(city)) { | |
return 27.25; | |
} | |
else { | |
return -200; | |
} | |
} | |
} |
You can download the war file from here. Once deployed the war file on WSO2 AS, you would be able to access the API's as follows. Note that i have set portOffset of WSO2 AS as 2 since i run both servers in same machine.
http://localhost:9765/SimpleMap_1.0.0/services/weather/gettemperature?city=Kandy
First one will give {"city":"kandy"} as the output and second will give the 24.5 as the temperature output.
Now it is the time to play with WSO2 ESB to chain the above two apis. Please have a look at ESB config below, As code says what it does i'm not going to explain in detail. Figure below show the DevStudio view of the API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<api xmlns="http://ws.apache.org/ns/synapse" name="MapService" context="/MapService"> | |
<resource methods="GET" uri-template="/gettemperature?x={lon}&y={lat}"> | |
<inSequence> | |
<call> | |
<endpoint> | |
<http method="get" uri-template="http://localhost:9765/SimpleMap_1.0.0/services/map/getcity?x={uri.var.lon}&y={uri.var.lat}"></http> | |
</endpoint> | |
</call> | |
<property name="uri.var.city" expression="json-eval($.city)"></property> | |
<log level="full"> | |
<property name="locationLOG" expression="get-property(uri.var.city)"></property> | |
</log> | |
<send> | |
<endpoint> | |
<http method="get" uri-template="http://localhost:9765/SimpleMap_1.0.0/services/weather/gettemperature?city={uri.var.city}"></http> | |
</endpoint> | |
</send> | |
</inSequence> | |
<outSequence> | |
<send></send> | |
</outSequence> | |
</resource> | |
</api> |
http://localhost:8280/MapService/gettemperature?x=7&y=80
References
[1] - http://wso2.com/products/developer-studio/
[2] - http://wso2.com/products/application-server/
[3] - http://wso2.com/products/enterprise-service-bus/
1 comment:
The term 'api chaining' was originally created and copyrighted by Owen Rubel. Please cease and desist usage without proper implementation and acceptable acknowledgement of copyright ownership.
Post a Comment