Integrating external APIs
In the last tutorial, you learned how to access all REST-Endpoints provided by the ApiOmat. But sometimes you have to use REST-Endpoints provided by other servers. As an example, we will use the free pokémon-Api at http://pokeapi.co/ . The page provides a REST-Endpoint for querying pokémon data, like data about pokémon, items, regions etc. For example, if you want to have informations about the item "poké-ball", you can adress the "poké-ball"-resource via the URL http://pokeapi.co/api/v2/item/poke-ball . The information about the poké-ball is then returned as a JSON.
Let's suppose the following use-case, keeping in mind the shopping list we have been building in this toturial. Every-time someone enters the a product with the name "poké-ball", the Module should automatically connect to the poké-api to request the price of the poké-ball.
-
First, change the "name"-attribute of groceries to "mandatory" so that the user is forced to enter a name when a new grocery is created. You can do that easily via the dashboard.
-
Your module-code should already be downloaded and integrated into eclipse. Go to the "GroceryHooksNonTransient"-class .
-
Inside the class, enter the following method:
Codepublic
String downloadJson( String urlString )
throws
IOException
{
URL url =
new
URL( urlString );
HttpURLConnection con = ( HttpURLConnection ) url.openConnection( );
con.addRequestProperty(
"User-Agent"
,
"Cheese"
);
System.out.println( con.getResponseMessage( ) );
if
( con.getResponseCode( ) == HttpURLConnection.HTTP_OK )
{
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader( con.getInputStream( ) ) );
// transform the response into a single string
String line;
String content =
""
;
while
( ( line = reader.readLine( ) ) !=
null
)
{
content += line;
}
// extract attributes
return
content;
}
return
null
;
}
This method can be used to download the data inside the body of a html-response.
-
Now, alter the "beforePost"-Hook, so that everytime a new grocery with the name "poké-ball" is created, the resource "http://pokeapi.co/api/v2/item/poke-ball" should be requested in order to access the price of the poké-ball.
Code@Override
public
void
beforePost( Grocery obj, Request r )
{
// enter default price (from previous tutorial)
Object defaultPrice = TutorialModule.APP_CONFIG_PROXY.getConfigValue( TutorialModule.DEFAULT_PRICE,
r.getApplicationName( ), r.getSystem( ) );
obj.setPrice( Double.valueOf( String.valueOf( defaultPrice ) ) );
//if the name of the newly created grocery is "poke-ball", download the information about the poke-ball from the poke-api.
if
( obj.getName( ).equals(
"poke-ball"
) )
{
try
{
String pokeBallJson = downloadJson(
"http://pokeapi.co/api/v2/item/poke-ball"
);
}
catch
( IOException e )
{
e.printStackTrace( );
}
catch
( ParseException e )
}
}
-
If you enter " http://pokeapi.co/api/v2/item/poke-ball " into your browser, you access the very JSON that will now be contained inside the pokeBallJson- String. The price of the poké-ball can be accessed through the "cost" attribute. To parse the JSON with java, you need an external library, like the JSON.simple - library. You can download the jar right here: json-simple-1.1.1.jar
-
Copy this jar into the lib-folder of your Module. Then right click on it, and choose [ build-path > add to build path ].
-
Now, you can access the methods provided by the JSON.simple-library to parse the JSON provided by the api.
Code@Override
public
void
beforePost( Grocery obj, Request r )
{
// enter default price (from previous tutorial)
Object defaultPrice = TutorialModule.APP_CONFIG_PROXY.getConfigValue( TutorialModule.DEFAULT_PRICE,
r.getApplicationName( ), r.getSystem( ) );
obj.setPrice( Double.valueOf( String.valueOf( defaultPrice ) ) );
//if the name of the newly created grocery is "poke-ball", download the information about the poke-ball from the poke-api.
if
( obj.getName( ).equals(
"poke-ball"
) )
{
try
{
String pokeBallJson = downloadJson(
"http://pokeapi.co/api/v2/item/poke-ball"
);
//parse the JSON-String, extract the cost and enter the value into the price of the grocery
JSONParser parser =
new
JSONParser( );
JSONObject pokeObject = ( JSONObject ) parser.parse( pokeBallJson );
Long cost = ( Long ) pokeObject.get(
"cost"
);
obj.setPrice( cost.doubleValue() );
}
catch
( IOException e )
{
e.printStackTrace( );
}
catch
( ParseException e )
{
e.printStackTrace( );
}
}
}
-
You're almost finished! Upload the module-code. And try it out! Now, when you enter a new item with the name "poké-ball", the price should autmatically be set by the price provided by the external API, which is 200!