Java RapidAPI
APIs can be found all over the internet. A great consolidator of many APIs is RapidAPI. In this example we will use a site to consolidates API stats. Learning a few lines of code and you can start extracting lots of data from the internet via APIs.
Java and RapidAPI use case
APIs and tooling like Jupyter docs allows many opportunities in fields like Data Science. As more and more developers use APIs, they build standards in how you setup a client, send requests and receive information...
- An HttpClient can be used to send requests and retrieve their responses
- An HttpRequest sets up header and body to API provider
- An HttpResponse is returned as a result of sending an HttpRequest
- Typically a response body will be received, if all goes well
- Next step, is to format the response body according to your data science needs
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
//RapidAPI header https://rapidapi.com/spamakashrajtech/api/corona-virus-world-and-india-data
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://corona-virus-world-and-india-data.p.rapidapi.com/api"))
.header("x-rapidapi-key", "dec069b877msh0d9d0827664078cp1a18fajsn2afac35ae063")
.header("x-rapidapi-host", "corona-virus-world-and-india-data.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
//RapidAPI request and response
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
//RapidAPI Body
System.out.println(response.body());
Build your own Web Service
To build a Web Service in the backend allows you to control information and errors between your app and the user. A proper Web Service requires more code:RESTful interface, error handling, and in this case reducing the calls to the service according to update frequency.- There are benefits of building service in Java vs JavaScript:
- Firewall will not be blocked as backend server on AWS is outside the firewall - Selecting frontend service https://nighthawkcodingsociety.com that has been cleared through IT. - Many APIs have usage costs or usage limits, backend can serve while limiting direct API access.
package com.nighthawk.spring_portfolio.mvc.covid;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.HashMap;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController // annotation to create a RESTful web services
@RequestMapping("/api/covid") //prefix of API
public class CovidApiController {
private JSONObject body; //last run result
private HttpStatus status; //last run status
String last_run = null; //last run day of month
// GET Covid 19 Stats
@GetMapping("/daily") //added to end of prefix as endpoint
public ResponseEntity<JSONObject> getCovid() {
//calls API once a day, sets body and status properties
String today = new Date().toString().substring(0,10);
if (last_run == null || !today.equals(last_run))
{
try { //APIs can fail (ie Internet or Service down)
//RapidAPI header
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://corona-virus-world-and-india-data.p.rapidapi.com/api"))
.header("x-rapidapi-key", "dec069b877msh0d9d0827664078cp1a18fajsn2afac35ae063")
.header("x-rapidapi-host", "corona-virus-world-and-india-data.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
//RapidAPI request and response
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
//JSONParser extracts text body and parses to JSONObject
this.body = (JSONObject) new JSONParser().parse(response.body());
this.status = HttpStatus.OK; //200 success
this.last_run = today;
}
catch (Exception e) { //capture failure info
HashMap<String, String> status = new HashMap<>();
status.put("status", "RapidApi failure: " + e);
//Setup object for error
this.body = (JSONObject) status;
this.status = HttpStatus.INTERNAL_SERVER_ERROR; //500 error
this.last_run = null;
}
}
//return JSONObject in RESTful style
return new ResponseEntity<>(body, status);
}
}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://ultimate-tennis1.p.rapidapi.com/live_leaderboard/50"))
.header("X-RapidAPI-Key", "af654d789amshce4b35d071f3bd2p1c0cc8jsn8db3aa6a8acc")
.header("X-RapidAPI-Host", "ultimate-tennis1.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());