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 pokemon-Api at http://pokeapi.co/ . The page provides a REST-Endpoint for querying pokemon data, like data about pokemon, items, regions etc. For example, if you want to have informations about the item "poke-ball", you can adress the "poke-ball"-resource via the URL http://pokeapi.co/api/v2/item/poke-ball . The informations about the poke-ball are then returned as a JSON.
Let's suppose the following use-case: Remember the shop-app we have been building in this tutorial. Every-time someone enters the a product with the name "poke-ball", the Module should automatically connect to the poke-api to request the price of the poke-ball.
-
First, change the "name"-attribut 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 integratet into eclipse. Go to the "GroceryHooksNonTransient"-class .
-
Inside the class, enter the following mehthod:
public
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 "poke-ball" is created, the resource "http://pokeapi.co/api/v2/item/poke-ball" should be requestet to access the price of the pokeball.
@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 pokeball can accessed through the "cost" attribut. To parse the JSON with java, you need an external library, like the JSON.simple - library. You can download the jar right here:
-
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 cann access the methods provided by the JSON.simple-library to parse the JSON provided by the api.
@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 "poke-ball", the price should autmatically be set the price provided by the external API, which is 200!