Fetch and Backend RapidAPI

Below is the json file from the myStore RapidAPI.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

//RapidAPI header  https://rapidapi.com/iddogino1/api/my-store2/
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://my-store2.p.rapidapi.com/catalog/categories"))
    .header("x-rapidapi-key", "c59b416554mshfcef455148b1cd9p158ddejsn959d7b9ce755")
    .header("x-rapidapi-host", "my-store2.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());
{"categories":[{"category":""},{"category":"supplements"},{"category":"Stationery"},{"category":"Office Supplies"},{"category":"Test"},{"category":"Accessories"},{"category":"Bike"},{"category":"nill"},{"category":null},{"category":"Wallet"},{"category":"Watches"},{"category":"Travel Gear"},{"category":"Software"},{"category":"Clothing"},{"category":"asa"},{"category":"books"},{"category":"dsas"},{"category":"zx"},{"category":"25"},{"category":"appliance"},{"category":"velit elit in"},{"category":"Furniture"},{"category":"kategoria"},{"category":"  meble"},{"category":"hth"},{"category":"string"},{"category":"laptop"},{"category":"Technology"},{"category":"Baby"},{"category":"Movies"},{"category":"Automotive"},{"category":"Computers"},{"category":"Music"},{"category":"Sports"},{"category":"Beauty"},{"category":"Health"},{"category":"Electronics"},{"category":"Industrial"},{"category":"Home"},{"category":"Shoes"},{"category":"Grocery"},{"category":"Kids"},{"category":"Garden"},{"category":"Toys"},{"category":"Tools"},{"category":"Games"},{"category":"Jewelery"},{"category":"computer"},{"category":"Outdoors"},{"category":"Appliances"},{"category":"Calsado"},{"category":"Deportiva"},{"category":"Moviles"},{"category":"computers."},{"category":"Asado"},{"category":"Movil"},{"category":"computeir"},{"category":" "},{"category":" cocinca"},{"category":"shirt"},{"category":"Snacks"},{"category":"Computers    "},{"category":"Smart TV"},{"category":"Bebidas"},{"category":"Creator Expert"},{"category":"argentina"},{"category":"China"},{"category":"Gaming"},{"category":"PC"},{"category":"Tecnologie"},{"category":"{{category}}"},{"category":"string1"},{"category":"COMPUTERS PRUEBA"},{"category":"SEDAN2"},{"category":"Electronicos"},{"category":"SEDAN"},{"category":"PRUEBA3"},{"category":"tecnology"},{"category":"Accesorio"},{"category":"computappliancesers"},{"category":"cumputer"},{"category":"accesorios"},{"category":"electro"},{"category":"adsadadsad"},{"category":"Limpieza"},{"category":"Celulares"},{"category":"Juguetes"},{"category":"calzado"},{"category":"sombrero"},{"category":"Laptot"},{"category":"omputers"},{"category":"suplements"},{"category":"Accesories"},{"category":"X"},{"category":"leche"},{"category":"Electrodomesticos"},{"category":"accesoros"},{"category":"teste"},{"category":"Viveres"},{"category":"videogames"},{"category":"sport"},{"category":"iphone"},{"category":"tecnilogy"},{"category":"Computadora"},{"category":"Wiper blade"},{"category":"Pen"},{"category":"autos"},{"category":"Stationary items"},{"category":"Udemy"},{"category":"tv"},{"category":"Audio"},{"category":"harum"},{"category":"voluptatem"},{"category":"maiores"},{"category":"molestiae"},{"category":"fugiat"},{"category":"Electrodomestico"},{"category":"vitae"},{"category":"distinctio"},{"category":"Tecnología"},{"category":"Automatizacion"},{"category":"taom"},{"category":"Laptop dell intel i7 8gb ram"},{"category":"computadoras"},{"category":"TV de alta calidad"},{"category":"products"},{"category":"Categoria 1"},{"category":"CarnePre"},{"category":"perro"},{"category":"mi_category"},{"category":"Frituras"},{"category":"telefono"},{"category":"proteina"},{"category":"refresco"},{"category":"Juguete"},{"category":"papeleria"},{"category":"Alimentos"},{"category":"console"},{"category":"hombres"},{"category":"Verduras"},{"category":"karateDSL"},{"category":"dolore"},{"category":"Verduras_pichincha"},{"category":"cars"},{"category":"clothes"},{"category":"comida"},{"category":"hogar"},{"category":"Venta"},{"category":"Baja676"},{"category":"Abc"},{"category":" Accessories"},{"category":"<category>"},{"category":"insumo"},{"category":"zapatos"},{"category":"<categoria>"},{"category":"Categoria1"},{"category":"ROCA"},{"category":"Categoria 2"},{"category":"Tomates_pichincha"},{"category":"Pruebas"},{"category":"cocina"},{"category":"ropa"},{"category":"perrito"},{"category":"deportes"},{"category":"manifestaciones"},{"category":"tacones"},{"category":"Prueba"},{"category":"shirts"},{"category":"tecnlogia"},{"category":"insumo para el hogar"},{"category":"Apliances"},{"category":"ELectronica"},{"category":"phone"},{"category":"Sistemas"},{"category":"Electric guitar"},{"category":"a"},{"category":"RPOA"},{"category":"Applianes"},{"category":"Bike2"},{"category":"zapatillas"},{"category":"xxx"},{"category":"Person"},{"category":"Electro2"},{"category":" Juego"},{"category":"hombre"},{"category":"49 pulgadas"},{"category":"Telefono Movil"},{"category":"Telefono Movil 2T"},{"category":"Movil 2T"},{"category":"categoria"},{"category":"Sex"},{"category":"gff"},{"category":"Cel"},{"category":"action figures"},{"category":"Informatica"},{"category":"Casa"},{"category":"Vehicles"},{"category":"Televisores"},{"category":"comúters"},{"category":"ppa"},{"category":"consolas"},{"category":"COMPUTADOR"},{"category":"Televisor"},{"category":"Running"},{"category":"IT"},{"category":"automovil"},{"category":"Videojuegos"},{"category":"Consola"},{"category":"lindo"},{"category":"CellPhones"},{"category":"Streamer"},{"category":"lacteo"},{"category":"string123"},{"category":"cursos"},{"category":"pasta"},{"category":"Pie"},{"category":"Value Meal 1"},{"category":"Value Meal 2"},{"category":"Value Meal 3"},{"category":"Food"},{"category":"App"},{"category":"Smartphones"},{"category":"Ancient Technology"},{"category":"Appliences"},{"category":"Mobile Phone"},{"category":"c1"},{"category":"Dawa"},{"category":"sdfsfd"},{"category":"N/A"},{"category":"NA"},{"category":"AntiAge"},{"category":"ciencia"},{"category":"terror"},{"category":"cupidatat pariatur laborum occaecat"},{"category":"informatico"},{"category":"2"},{"category":"09"},{"category":"category_test"},{"category":"{{$randomDepartment}}"},{"category":"asd"},{"category":"Cmoputación"},{"category":"jabon"},{"category":"Ripley"},{"category":"Toyota"},{"category":"APPLE"},{"category":"card"},{"category":"Mobiles"},{"category":"abrigos"},{"category":"alimentos organicos"},{"category":"minim nisi commodo laborum"},{"category":"camaras"},{"category":"TShirt"},{"category":"adult"},{"category":"TEJIDO"},{"category":"Bazzar"},{"category":"video juegos"},{"category":"capunters"},{"category":"Colombia"},{"category":"Appilances"},{"category":"Armadura"},{"category":"Mouse"},{"category":"Arma blanca"},{"category":"TRANSPORTE"},{"category":"ARTESANIA"},{"category":"ROPAS"},{"category":"cosmos"},{"category":"tech"},{"category":"Celular"},{"category":"rico"},{"category":"hola"},{"category":"fotomoso"},{"category":"congas"},{"category":"reprehenderit consectetur nostrud in"},{"category":"spc"},{"category":"smart"},{"category":"fruta"},{"category":"notebook"},{"category":"succesfull"},{"category":"Tinta"},{"category":"Hardware"},{"category":"MONITOR"},{"category":"ullamco Excepteur aliqua"},{"category":"Interior"},{"category":"Animalitos"},{"category":"Animales"},{"category":"animal"},{"category":"comida rapida"},{"category":"postres helados"},{"category":"perritos"},{"category":"Smart Man"},{"category":"portatiles"},{"category":"chocolates"},{"category":"cookies"},{"category":"coputers"},{"category":"Category"},{"category":"laptos"},{"category":"Lenovo"}]}

Questions

  1. Benefit of using and API?
    • APIs allow different applications to communicate
    • Able to access and use data
  2. Benefit(s) of backend implementation?
    • can use data from other applications for own projects including our team project in AP CSA
package com.nighthawk.spring_java_project.mvc.store;

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/store")  //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("/categories")   //added to end of prefix as endpoint
    public ResponseEntity<JSONObject> getStore() {

        //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://my-store2.p.rapidapi.com/catalog/products"))
                    .header("x-rapidapi-key", "c59b416554mshfcef455148b1cd9p158ddejsn959d7b9ce755")
                    .header("x-rapidapi-host", "my-store2.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);
    }
}
|   package com.nighthawk.spring_java_project.mvc.store;
illegal start of expression