191 lines
4.9 KiB
Java
Raw Normal View History

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;
2023-12-18 09:25:08 +01:00
import java.nio.file.WatchEvent;
import Logik.Kind;
import com.google.gson.Gson;
2023-12-18 08:50:46 +01:00
/**
* 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;
2023-12-20 09:28:32 +01:00
private final Gson gson;
public RestApiClient(){
client = HttpClient.newHttpClient();
2023-12-20 09:28:32 +01:00
gson = new Gson();
}
public static void main(String[] args){
2023-12-20 09:28:32 +01:00
Kind kind = new Kind("Klein", "Kevin", 2);
String json = new Gson().toJson(kind);
System.out.println(json);
//new RestApiClient().post("Kind", json);
}
/**
* @param controllerName
*/
@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());
2023-12-18 09:25:08 +01:00
Kind test = gson.fromJson(httpResponse.body(), Kind.class);
// Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode());
2023-12-18 09:25:08 +01:00
System.out.println("Response Body: " + test.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param controllerName
* @param id
*/
@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());
2023-12-20 09:28:32 +01:00
Kind[] test = gson.fromJson(httpResponse.body(), Kind[].class);
// Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode());
2023-12-20 09:28:32 +01:00
for(Kind i : test){
System.out.println(i.getVorname());
}
//System.out.println("Response Body: " + test);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param controllerName
* @param id
* @param bezahlt
*/
@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();
}
}
/**
* @param controllerName
* @param id
*/
@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();
}
}
/**
* @param controllerName
*/
@Override
public void post(String controllerName, String jsonData) {
2023-12-20 09:28:32 +01:00
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();
2023-12-20 09:28:32 +01:00
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();
}
}
/**
* @param controllerName
* @param id
*/
@Override
public void delete(String controllerName, int id) {
}
}