From d734e76db85fc5140ad16b6e4f34c79b149dc85d Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 5 Jun 2021 23:29:31 +0100 Subject: [PATCH] (Unfinished) Testing API Retrieval --- .../java/org/goudham/me/MyWaifuClient.java | 10 +++- .../java/org/goudham/me/MyWaifuWrapper.java | 55 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index bc174e3..9fde196 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -26,7 +26,7 @@ public class MyWaifuClient { *

See MyWaifuList for obtaining an API Key

* @param apiKey API Key to authorise API request */ - public MyWaifuClient(@NotNull String apiKey) { + MyWaifuClient(@NotNull String apiKey) { myWaifuWrapper = new MyWaifuWrapper(apiKey); } @@ -47,6 +47,14 @@ public class MyWaifuClient { return myWaifuClient; } + public void getWaifu(String slug) { + myWaifuWrapper.getWaifu(httpClient, slug); + } + + public void getWaifu(Integer id) { + myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); + } + /** * Sets an instance of HttpClient * diff --git a/src/main/java/org/goudham/me/MyWaifuWrapper.java b/src/main/java/org/goudham/me/MyWaifuWrapper.java index f116755..15dfa4c 100644 --- a/src/main/java/org/goudham/me/MyWaifuWrapper.java +++ b/src/main/java/org/goudham/me/MyWaifuWrapper.java @@ -1,13 +1,25 @@ package org.goudham.me; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; + + /** * Returns API information to {@link MyWaifuClient} */ class MyWaifuWrapper { private final String version = "1.0"; - private final String host = "https://mywaifulist.moe/api/v1/"; + private static final String host = "https://mywaifulist.moe/api/v1/"; private final String apiKey; + private final ObjectMapper objectMapper = new ObjectMapper(); + /** * Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information * @param apiKey API Key to authorise API request @@ -15,4 +27,45 @@ class MyWaifuWrapper { MyWaifuWrapper(String apiKey) { this.apiKey = apiKey; } + + private void sendRequest(HttpClient httpClient, String param) { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(host + param)) + .version(httpClient.version()) + .timeout(Duration.ofSeconds(30)) + .header("Content-Type", "application/json") + .header("apikey", apiKey) + .GET() + .build(); + + CompletableFuture> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); + + System.out.println(response); + +// String resultBody = ""; +// int resultStatusCode = 0; +// try { +// resultBody = response.thenApply(HttpResponse::body).get(); +// resultStatusCode = response.thenApply(HttpResponse::statusCode).get(); +// +// } catch (InterruptedException | ExecutionException e) { +// e.printStackTrace(); +// } +// +// System.out.println(resultStatusCode); +// System.out.println(resultBody); +// +// try { +// JsonNode parent = objectMapper.readTree(resultBody); +// String waifuData = parent.get("data").toString(); +// Waifu waifu = objectMapper.readValue(waifuData, Waifu.class); +// System.out.println(waifu); +// } catch (JsonProcessingException e) { +// e.printStackTrace(); +// } + } + + void getWaifu(HttpClient httpClient, String param) { + sendRequest(httpClient, "waifu/" + param); + } }