From f075da740998231e3c9f4916d45bcd33039799fb Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 01:16:40 +0100 Subject: [PATCH 1/9] Update README.md --- README.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 32f33c1..1c03c44 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,29 @@ -# MyWaifuWrapper -MyWaifuWrapper - An Asynchronous Java API Wrapper for MyWaifuList +

MyWaifuWrapper | An Asynchronous Java API Wrapper for MyWaifuList

+ +

+ + + + +

+

+ + + + + +

+ +# Summary + +This is an Asynchronous API Wrapper for [MyWaifuList](https://mywaifulist.moe/dash) + +# Download + +TODO + +# Usage + +TODO + + From 6717c2cd8c85d4f0ef42ff5a8ed72d72154fcea4 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:19:47 +0100 Subject: [PATCH 2/9] Add CustomException Class APIResponseException.java Throwing one single exception when catching many different ones within the sendRequest() method of MyWaifuWrapper.java --- .../goudham/me/exception/APIResponseException.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/org/goudham/me/exception/APIResponseException.java diff --git a/src/main/java/org/goudham/me/exception/APIResponseException.java b/src/main/java/org/goudham/me/exception/APIResponseException.java new file mode 100644 index 0000000..b7017ae --- /dev/null +++ b/src/main/java/org/goudham/me/exception/APIResponseException.java @@ -0,0 +1,14 @@ +package org.goudham.me.exception; + + +import org.goudham.me.MyWaifuWrapper; + +/** + * Thrown when {@link MyWaifuWrapper} + * + */ +public class APIResponseException extends Throwable { + public APIResponseException(String errorMessage, Throwable error) { + super(errorMessage, error); + } +} From 4fa583201e63732be02fcb410014dfe1fc54ef9a Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:22:41 +0100 Subject: [PATCH 3/9] Update documentation --- .../java/org/goudham/me/exception/APIResponseException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/goudham/me/exception/APIResponseException.java b/src/main/java/org/goudham/me/exception/APIResponseException.java index b7017ae..bfecdaf 100644 --- a/src/main/java/org/goudham/me/exception/APIResponseException.java +++ b/src/main/java/org/goudham/me/exception/APIResponseException.java @@ -4,7 +4,7 @@ package org.goudham.me.exception; import org.goudham.me.MyWaifuWrapper; /** - * Thrown when {@link MyWaifuWrapper} + * Thrown when {@link MyWaifuWrapper} fails to return API information * */ public class APIResponseException extends Throwable { From 6192b342048d3e2171676bf1fc5386cf7ff0af56 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:23:18 +0100 Subject: [PATCH 4/9] Update documentation --- src/main/java/org/goudham/me/api/entity/series/Series.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/goudham/me/api/entity/series/Series.java b/src/main/java/org/goudham/me/api/entity/series/Series.java index 38dc544..f35546e 100644 --- a/src/main/java/org/goudham/me/api/entity/series/Series.java +++ b/src/main/java/org/goudham/me/api/entity/series/Series.java @@ -138,8 +138,7 @@ public class Series { /** * {@link Studio} - *

- * Contains information on a given animation or game development studio + *

Contains information on a given animation or game development studio

* */ @JsonProperty("studio") From e6baaef299b290a0ffbea29084b3b3235b7647e7 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:24:43 +0100 Subject: [PATCH 5/9] Add new constructor with @JsonCreator annotation As the API is inconsistent, the Studio object within the Waifu endpoint was returning a String, this implementation allows Jackson to instantiate the Studio class even if a String was returned --- src/main/java/org/goudham/me/api/entity/Studio.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/goudham/me/api/entity/Studio.java b/src/main/java/org/goudham/me/api/entity/Studio.java index dbe47fa..29894bd 100644 --- a/src/main/java/org/goudham/me/api/entity/Studio.java +++ b/src/main/java/org/goudham/me/api/entity/Studio.java @@ -2,10 +2,7 @@ package org.goudham.me.api.entity; import javax.annotation.processing.Generated; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyDescription; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.*; import java.util.Objects; @@ -29,6 +26,12 @@ import java.util.Objects; }) @Generated("jsonschema2pojo") public class Studio { + + @JsonCreator + Studio(String name) { + this.name = name; + } + /** * {@link Studio} ID */ From 8471d7a5e3c8bec242ae71ef17694e0be78b2630 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:26:19 +0100 Subject: [PATCH 6/9] Add POJO for Response A Response<> object is what users will receive when interacting with MyWaifuClient.java --- src/main/java/org/goudham/me/Response.java | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/org/goudham/me/Response.java diff --git a/src/main/java/org/goudham/me/Response.java b/src/main/java/org/goudham/me/Response.java new file mode 100644 index 0000000..c9e2e8e --- /dev/null +++ b/src/main/java/org/goudham/me/Response.java @@ -0,0 +1,30 @@ +package org.goudham.me; + +public class Response { + private T entity; + private final Integer responseCode; + private final String responseBody; + + Response(Integer responseCode, String responseBody, T entity) { + this.responseCode = responseCode; + this.responseBody = responseBody; + this.entity = entity; + } + + Response(Integer responseCode, String responseBody) { + this.responseCode = responseCode; + this.responseBody = responseBody; + } + + public T getEntity() { + return entity; + } + + public Integer getResponseCode() { + return responseCode; + } + + public String getResponseBody() { + return responseBody; + } +} From fc9bf53620bec5c7801e7baefe7b0a13317d69e3 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:26:56 +0100 Subject: [PATCH 7/9] Add POJO for Result A Result object is what the sendRequest() method will return to the helper methods within MyWaifuWrapper.java --- src/main/java/org/goudham/me/Result.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/org/goudham/me/Result.java diff --git a/src/main/java/org/goudham/me/Result.java b/src/main/java/org/goudham/me/Result.java new file mode 100644 index 0000000..483e752 --- /dev/null +++ b/src/main/java/org/goudham/me/Result.java @@ -0,0 +1,19 @@ +package org.goudham.me; + +class Result { + private final Integer statusCode; + private final String body; + + Result(Integer statusCode, String body) { + this.statusCode = statusCode; + this.body = body; + } + + Integer getStatusCode() { + return statusCode; + } + + String getBody() { + return body; + } +} From e3d187d142e36d87544b9ca535c1863224ee88fd Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:28:49 +0100 Subject: [PATCH 8/9] Implement basic functionality for Waifu endpoint --- .../java/org/goudham/me/MyWaifuClient.java | 12 ++-- .../java/org/goudham/me/MyWaifuWrapper.java | 69 ++++++++++++------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index 9fde196..ba2fb2f 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -1,5 +1,7 @@ package org.goudham.me; +import org.goudham.me.api.entity.waifu.Waifu; +import org.goudham.me.exception.APIResponseException; import org.jetbrains.annotations.NotNull; import javax.net.ssl.SSLParameters; @@ -47,12 +49,12 @@ public class MyWaifuClient { return myWaifuClient; } - public void getWaifu(String slug) { - myWaifuWrapper.getWaifu(httpClient, slug); + public Response getWaifu(String slug) throws APIResponseException { + return myWaifuWrapper.getWaifu(httpClient, slug); } - public void getWaifu(Integer id) { - myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); + public Response getWaifu(Integer id) throws APIResponseException { + return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); } /** @@ -60,7 +62,7 @@ public class MyWaifuClient { * * @param httpClient HttpClient for executing API requests */ - public void setHttpClient(HttpClient httpClient) { + void setHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } diff --git a/src/main/java/org/goudham/me/MyWaifuWrapper.java b/src/main/java/org/goudham/me/MyWaifuWrapper.java index 15dfa4c..662da1c 100644 --- a/src/main/java/org/goudham/me/MyWaifuWrapper.java +++ b/src/main/java/org/goudham/me/MyWaifuWrapper.java @@ -1,6 +1,10 @@ package org.goudham.me; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.goudham.me.api.entity.waifu.Waifu; +import org.goudham.me.exception.APIResponseException; import java.net.URI; import java.net.http.HttpClient; @@ -8,12 +12,13 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; /** * Returns API information to {@link MyWaifuClient} */ -class MyWaifuWrapper { +public class MyWaifuWrapper { private final String version = "1.0"; private static final String host = "https://mywaifulist.moe/api/v1/"; private final String apiKey; @@ -28,7 +33,13 @@ class MyWaifuWrapper { this.apiKey = apiKey; } - private void sendRequest(HttpClient httpClient, String param) { + /** + * + * + * @param httpClient The {@link HttpClient} to use for sending {@link HttpRequest}'s + * @param param The end of the endpoint appended onto the host + */ + private Result sendRequest(HttpClient httpClient, String param) throws APIResponseException { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(host + param)) .version(httpClient.version()) @@ -40,32 +51,38 @@ class MyWaifuWrapper { CompletableFuture> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); - System.out.println(response); + int responseCode; + String responseBody; + try { + responseCode = response.thenApply(HttpResponse::statusCode).get(); + responseBody = response.thenApply(HttpResponse::body).get(); + } catch (InterruptedException | ExecutionException exp) { + throw new APIResponseException(exp.getMessage(), exp); + } -// 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(); -// } + return new Result(responseCode, responseBody); } - void getWaifu(HttpClient httpClient, String param) { - sendRequest(httpClient, "waifu/" + param); + Response getWaifu(HttpClient httpClient, String param) throws APIResponseException { + Result waifuResult = sendRequest(httpClient, "waifu/" + param); + Integer statusCode = waifuResult.getStatusCode(); + String body = waifuResult.getBody(); + Waifu waifu = null; + + if (statusCode == 200) { + try { + JsonNode parent = objectMapper.readTree(body); + String waifuData = parent.get("data").toString(); + waifu = objectMapper.readValue(waifuData, Waifu.class); + } catch (JsonProcessingException jpe) { + jpe.printStackTrace(); + + statusCode = 100; + body = "{\"message\":\"If you are seeing this message, this is more than likely a fault in my logic. " + + "Please raise an issue with the printed stacktrace :D\",\"code\":Custom 100}"; + } + } + + return new Response<>(statusCode, body, waifu); } } From b8a1fec7f6f40a10c96bcd493a057666e18970a9 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 04:30:35 +0100 Subject: [PATCH 9/9] Update version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9349c21..aceb3d2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.goudham.me MyWaifuWrapper - 0.1 + 0.1.2 MyWaifuWrapper