• 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
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://flight-radar1.p.rapidapi.com/aircrafts/list"))
    .header("x-rapidapi-key", "9e3f40a0cemshcced0bdaa85f0c0p1e3309jsnbb4748bd1d8c")
    .header("x-rapidapi-host", "flight-radar1.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());
{"version":1549273446,"rows":[{"description":"Airbus A220 family","models":[{"Name":"Airbus A220-100","Code":"BCS1"},{"Name":"Airbus A220-300","Code":"BCS3"}]},{"description":"Airbus A300 family","models":[{"Name":"Airbus A300","Code":"A30B"},{"Name":"Airbus A300-600","Code":"A306"}]},{"description":"Airbus A310 family","models":[{"Name":"Airbus A310","Code":"A310"}]},{"description":"Airbus A320 family","models":[{"Name":"Airbus A318","Code":"A318"},{"Name":"Airbus A319","Code":"A319"},{"Name":"Airbus A319neo","Code":"A19N"},{"Name":"Airbus A320","Code":"A320"},{"Name":"Airbus A320neo","Code":"A20N"},{"Name":"Airbus A321","Code":"A321"},{"Name":"Airbus A321neo","Code":"A21N"}]},{"description":"Airbus A330 family","models":[{"Name":"Airbus A330-200","Code":"A332"},{"Name":"Airbus A330-300","Code":"A333"},{"Name":"Airbus A330-900","Code":"A339"}]},{"description":"Airbus A340 family","models":[{"Name":"Airbus A340-200","Code":"A342"},{"Name":"Airbus A340-300","Code":"A343"},{"Name":"Airbus A340-500","Code":"A345"},{"Name":"Airbus A340-600","Code":"A346"}]},{"description":"Airbus A350 family","models":[{"Name":"Airbus A350-1000","Code":"A35K"},{"Name":"Airbus A350-900","Code":"A359"}]},{"description":"Airbus A380-800","models":[{"Name":"Airbus A380-800","Code":"A388"}]},{"description":"ATR 42 family","models":[{"Name":"ATR 42-300","Code":"AT43"},{"Name":"ATR 42-500","Code":"AT45"},{"Name":"ATR 42-600","Code":"AT46"}]},{"description":"ATR 72 family","models":[{"Name":"ATR 72-200","Code":"AT72"},{"Name":"ATR 72-500","Code":"AT75"},{"Name":"ATR 72-600","Code":"AT76"}]},{"description":"Avro RJ family","models":[{"Name":"Avro RJ100","Code":"RJ1H"},{"Name":"Avro RJ70","Code":"RJ70"},{"Name":"Avro RJ85","Code":"RJ85"}]},{"description":"Boeing 737 family","models":[{"Name":"Boeing 737 MAX 7","Code":"B37M"},{"Name":"Boeing 737 MAX 8","Code":"B38M"},{"Name":"Boeing 737 MAX 9","Code":"B39M"},{"Name":"Boeing 737-200","Code":"B732"},{"Name":"Boeing 737-300","Code":"B733"},{"Name":"Boeing 737-400","Code":"B734"},{"Name":"Boeing 737-500","Code":"B735"},{"Name":"Boeing 737-600","Code":"B736"},{"Name":"Boeing 737-700","Code":"B737"},{"Name":"Boeing 737-800","Code":"B738"},{"Name":"Boeing 737-900","Code":"B739"}]},{"description":"Boeing 747 family","models":[{"Name":"Boeing 747-200","Code":"B742"},{"Name":"Boeing 747-300","Code":"B743"},{"Name":"Boeing 747-400","Code":"B744"},{"Name":"Boeing 747-8","Code":"B748"},{"Name":"Boeing 747SP","Code":"B74S"}]},{"description":"Boeing 757 family","models":[{"Name":"Boeing 757-200","Code":"B752"},{"Name":"Boeing 757-300","Code":"B753"}]},{"description":"Boeing 767 family","models":[{"Name":"Boeing 767-200","Code":"B762"},{"Name":"Boeing 767-300","Code":"B763"},{"Name":"Boeing 767-400","Code":"B764"}]},{"description":"Boeing 777 family","models":[{"Name":"Boeing 777-200","Code":"B772"},{"Name":"Boeing 777-200LR","Code":"B77L"},{"Name":"Boeing 777-300","Code":"B773"},{"Name":"Boeing 777-300ER","Code":"B77W"}]},{"description":"Boeing 787 family","models":[{"Name":"Boeing 787-10","Code":"B78X"},{"Name":"Boeing 787-8","Code":"B788"},{"Name":"Boeing 787-9","Code":"B789"}]},{"description":"Bombardier CRJ family","models":[{"Name":"Bombardier CRJ-100","Code":"CRJ1"},{"Name":"Bombardier CRJ-1000","Code":"CRJX"},{"Name":"Bombardier CRJ-200","Code":"CRJ2"},{"Name":"Bombardier CRJ-700","Code":"CRJ7"},{"Name":"Bombardier CRJ-900","Code":"CRJ9"}]},{"description":"Bombardier Dash 8 family","models":[{"Name":"Bombardier Dash 8-100","Code":"DH8A"},{"Name":"Bombardier Dash 8-200","Code":"DH8B"},{"Name":"Bombardier Dash 8-300","Code":"DH8C"},{"Name":"Bombardier Dash 8-400","Code":"DH8D"}]},{"description":"Embraer E-Jet family","models":[{"Name":"Embraer E170","Code":"E170"},{"Name":"Embraer E175 (long)","Code":"E75L"},{"Name":"Embraer E175 (short)","Code":"E75S"},{"Name":"Embraer E190","Code":"E190"},{"Name":"Embraer E195","Code":"E195"}]},{"description":"Embraer RJ135","models":[{"Name":"Embraer RJ135","Code":"E135"}]},{"description":"Embraer RJ145","models":[{"Name":"Embraer RJ145","Code":"E145"}]},{"description":"Fokker family","models":[{"Name":"Fokker 100","Code":"F100"},{"Name":"Fokker 50","Code":"F50"},{"Name":"Fokker 70","Code":"F70"}]},{"description":"McDonnell Douglas DC-10","models":[{"Name":"McDonnell Douglas DC-10","Code":"DC10"}]},{"description":"McDonnell Douglas MD-11","models":[{"Name":"McDonnell Douglas MD-11","Code":"MD11"}]},{"description":"McDonnell Douglas MD-80 family","models":[{"Name":"McDonnell Douglas MD-81","Code":"MD81"},{"Name":"McDonnell Douglas MD-82","Code":"MD82"},{"Name":"McDonnell Douglas MD-83","Code":"MD83"},{"Name":"McDonnell Douglas MD-87","Code":"MD87"},{"Name":"McDonnell Douglas MD-88","Code":"MD88"}]},{"description":"McDonnell Douglas MD-90","models":[{"Name":"McDonnell Douglas MD-90","Code":"MD90"}]},{"description":"Saab 2000","models":[{"Name":"Saab 2000","Code":"SB20"}]},{"description":"Saab 340","models":[{"Name":"Saab 340","Code":"SF34"}]},{"description":"Sukhoi Superjet 100","models":[{"Name":"Sukhoi Superjet 100","Code":"SU95"}]}]}
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/flights")  //prefix of API
public class flights {
    private JSONObject body; //last run result
    private HttpStatus status; //last run status
    String last_run = null; //last run day of month

    @GetMapping("/monthly")   //added to end of prefix as endpoint
    public ResponseEntity<JSONObject> flights() {

        //calls API once a day, sets body and status properties
        String today = new Date().toString().substring(0,500); 
        if (last_run == null || !today.equals(last_run))
        {
            try { 

                HttpRequest request = HttpRequest.newBuilder()
                 .uri(URI.create("https://flight-radar1.p.rapidapi.com/aircrafts/list"))
                 .header("x-rapidapi-key", "9e3f40a0cemshcced0bdaa85f0c0p1e3309jsnbb4748bd1d8c")
                 .header("x-rapidapi-host", "flight-radar1.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 org.json.simple.JSONObject;
package org.json.simple does not exist

Benefit of using and API?

  • APIs can be found all over the internet. We can just use a few lines of code to get lots of data from the internet, so it is easier, more efficient, and less time consuming.

Benefit(s) of backend implementation?

  • Can set limit on how many times an api could run in a day or a month so that it would not exceed the maximun amount; for exmaple, the flights api (free subscription) only allows the api to be run 500 times per month