242 lines
7.1 KiB
Java
242 lines
7.1 KiB
Java
package RestAPISchnittstelle;
|
|
|
|
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import Logik.ElternAccount;
|
|
import Logik.Kind;
|
|
import com.google.gson.*;
|
|
|
|
/**
|
|
* noch nicht getestet
|
|
* TODO FERTIG MACHEN
|
|
* @author Samuel Wolff
|
|
*/
|
|
public class RestApiClient implements IRestAPI{
|
|
|
|
private final String urlBase = "https://pbg2h22awo.web.pb.bib.de/VPR_Schnittstelle/VPR_Schnittstelle/restAPI.php";
|
|
|
|
private final HttpClient client;
|
|
|
|
private final Gson gson;
|
|
|
|
public RestApiClient(){
|
|
client = HttpClient.newHttpClient();
|
|
gson = new Gson();
|
|
}
|
|
|
|
public static void main(String[] args){
|
|
|
|
RestApiClient client1 = new RestApiClient();
|
|
|
|
|
|
System.out.println(client1.nextId("Benutzer"));
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Get-Aufruf. Ruft alle Elemente einer Tabelle auf.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers
|
|
*/
|
|
@Override
|
|
public void get(String controllerName) {
|
|
URI apiUri = URI.create(String.format("%s/%s", urlBase, controllerName));
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.GET()
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Get-Aufruf. Ruft ein spezifisches Element auf.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers
|
|
* @param id Id der Aufzurufenden Zeile
|
|
*/
|
|
@Override
|
|
public void get(String controllerName, int id) {
|
|
URI apiUri = URI.create(String.format("%s/%s/%s", urlBase, controllerName, id));
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.GET()
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode() + httpResponse.body());
|
|
|
|
//System.out.println("Response Body: " + test);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Get-Aufruf. Ruft ein spezielles Element auf.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers
|
|
* @param id Id der Aufzurufenden Zeile
|
|
* @param bezahlt TODO Warum ist das hier?
|
|
*/
|
|
@Override
|
|
public void get(String controllerName, int id, boolean bezahlt) {
|
|
URI apiUri = URI.create(String.format("%s/%s?%s&%s", urlBase, controllerName, id, bezahlt));
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.GET()
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Put-Aufruf. Aktualisiert einen Eintrag.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers.
|
|
* @param id Id des zu änderenden Eintrags.
|
|
* @param jsonData JsonString mit den neuen Daten.
|
|
*/
|
|
@Override
|
|
public void put(String controllerName, int id, String jsonData) {
|
|
|
|
URI apiUri = URI.create(String.format("%s/%s/%s", urlBase,controllerName, id));
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.header("Content-Type", "application/json")
|
|
.PUT(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8))
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Post-Aufruf. Fügt einen Eintrag in eine Datenbank hinzu.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers.
|
|
* @param jsonData JsonString mit den Daten des Eintrags.
|
|
*/
|
|
@Override
|
|
public void post(String controllerName, String jsonData) {
|
|
URI apiUri = URI.create(String.format("%s/%s", urlBase,controllerName));
|
|
|
|
System.out.println(apiUri);
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.header("Content-Type", "application/json")
|
|
.POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8))
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Methode für einen Delete-Aufruf. Löscht einen Eintrag mit einer Id.
|
|
*
|
|
* @param controllerName Name des aufzurufenden Controllers
|
|
* @param id Id des zu löschenden Eintrags.
|
|
*/
|
|
@Override
|
|
public void delete(String controllerName, int id) {
|
|
URI apiUri = URI.create(String.format("%s/%s/%d", urlBase,controllerName, id));
|
|
|
|
System.out.println(apiUri);
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.header("Content-Type", "application/json")
|
|
.DELETE()
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
public int nextId(String controllerName){
|
|
URI apiUri = URI.create(String.format("%s/%s/nextId", urlBase, controllerName));
|
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
|
.uri(apiUri)
|
|
.GET()
|
|
.build();
|
|
|
|
try {
|
|
// Send the request and get the response
|
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
// Print the response status code and body
|
|
System.out.println("Status Code: " + httpResponse.statusCode());
|
|
System.out.println("Response Body: " + httpResponse.body());
|
|
|
|
JsonElement jsonElement = JsonParser.parseString(httpResponse.body());
|
|
JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
|
JsonObject json = jsonArray.get(0).getAsJsonObject();
|
|
|
|
return json.get("auto_increment").getAsInt();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
}
|