import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; public class Client { public CompletableFuture get(String urlStr) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(urlStr)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture post(String url, String body, String... token) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .header("Authorization", "Bearer " + token[0]) .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture put(String url, String body) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .PUT(HttpRequest.BodyPublishers.ofString(body)) .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } public CompletableFuture delete(String url) { HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .DELETE() .build(); return client.sendAsync(req, BodyHandlers.ofString()) .thenApply(HttpResponse::body); } }