From 1f3ea2fc0c8568fb73876d778b0cb40f9e8db29d Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 7 Jun 2021 02:38:21 +0100 Subject: [PATCH] Rename MyWaifuWrapper.java to APIWrapper.java --- .../{MyWaifuWrapper.java => APIWrapper.java} | 44 ++++++------------- .../java/org/goudham/me/MyWaifuClient.java | 20 +++++---- .../me/exception/APIMapperException.java | 5 +-- .../me/exception/APIResponseException.java | 5 +-- 4 files changed, 28 insertions(+), 46 deletions(-) rename src/main/java/org/goudham/me/{MyWaifuWrapper.java => APIWrapper.java} (64%) diff --git a/src/main/java/org/goudham/me/MyWaifuWrapper.java b/src/main/java/org/goudham/me/APIWrapper.java similarity index 64% rename from src/main/java/org/goudham/me/MyWaifuWrapper.java rename to src/main/java/org/goudham/me/APIWrapper.java index aa71979..6c4b1b8 100644 --- a/src/main/java/org/goudham/me/MyWaifuWrapper.java +++ b/src/main/java/org/goudham/me/APIWrapper.java @@ -1,8 +1,6 @@ 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.series.FilteredSeries; import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.exception.APIMapperException; @@ -13,6 +11,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -20,19 +19,24 @@ import java.util.concurrent.ExecutionException; /** * Returns API information to {@link MyWaifuClient} */ -public class MyWaifuWrapper { +public class APIWrapper { private final String version = "1.0"; private static final String host = "https://mywaifulist.moe/api/v1/"; private final String apiKey; - private final ObjectMapper objectMapper = new ObjectMapper(); + private final APIMapper apiMapper; /** - * Instantiates an instance of {@link MyWaifuWrapper} to retrieve API Information + * Instantiates an instance of {@link APIWrapper} to retrieve API Information. + * An instance of {@link APIMapper} is created to be able to {@link APIMapper#deserialize(Result, Class)} JSON to + * Java objects + * * @param apiKey API Key to authorise API request + * */ - MyWaifuWrapper(String apiKey) { + APIWrapper(String apiKey) { this.apiKey = apiKey; + apiMapper = new APIMapper(); } @@ -70,35 +74,13 @@ public class MyWaifuWrapper { return new Result(responseCode, responseBody); } - private Response getPopulatedResponse(Result result, Class entity) throws APIMapperException { - Integer statusCode = result.getStatusCode(); - String body = result.getBody(); - T newEntity = null; - - if (statusCode == 200) { - try { - JsonNode parent = objectMapper.readTree(body); - String data = parent.get("data").toString(); - newEntity = objectMapper.readValue(data, entity); - } catch (JsonProcessingException jpe) { - String customExceptionMessage = "If you are seeing this message, this is more than likely a fault in my logic. " + - "Please raise an issue including the printed stacktrace :D"; - String exceptionMessage = "\n\n" + customExceptionMessage + "\n\n" + jpe.getMessage(); - - throw new APIMapperException(exceptionMessage, jpe); - } - } - - return new Response<>(statusCode, body, newEntity); - } - Response getWaifu(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { Result waifuResult = sendRequest(httpClient, "waifu/" + param); - return getPopulatedResponse(waifuResult, Waifu.class); + return apiMapper.deserialize(waifuResult, Waifu.class); } Response getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { Result seriesResult = sendRequest(httpClient, "series/" + param); - return getPopulatedResponse(seriesResult, Series.class); + return apiMapper.deserialize(seriesResult, Series.class); } } diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index fbcff31..31337d9 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -1,5 +1,6 @@ package org.goudham.me; +import org.goudham.me.api.entity.series.FilteredSeries; import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.exception.APIMapperException; @@ -12,6 +13,7 @@ import java.net.CookieHandler; import java.net.ProxySelector; import java.net.http.HttpClient; import java.time.Duration; +import java.util.List; import java.util.concurrent.Executor; /** @@ -19,20 +21,20 @@ import java.util.concurrent.Executor; * * *

Main entry point for retrieving information from MyWaifuList.

- *

{@link MyWaifuWrapper} is utilised to make the API requests

+ *

{@link APIWrapper} is utilised to make the API requests

*/ public class MyWaifuClient { - private final MyWaifuWrapper myWaifuWrapper; + private final APIWrapper APIWrapper; private HttpClient httpClient; /** - * Creates an instance of MyWaifuClient + * Creates an instance of {@link MyWaifuClient} * *

See MyWaifuList for obtaining an API Key

* @param apiKey API Key to authorise API request */ MyWaifuClient(@NotNull String apiKey) { - myWaifuWrapper = new MyWaifuWrapper(apiKey); + APIWrapper = new APIWrapper(apiKey); } /** @@ -53,15 +55,15 @@ public class MyWaifuClient { } public Response getWaifu(String slug) throws APIResponseException, APIMapperException { - return myWaifuWrapper.getWaifu(httpClient, slug); + return APIWrapper.getWaifu(httpClient, slug); } public Response getWaifu(Integer id) throws APIResponseException, APIMapperException { - return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); + return APIWrapper.getWaifu(httpClient, String.valueOf(id)); } public Response getSeries(Integer id) throws APIMapperException, APIResponseException { - return myWaifuWrapper.getSeries(httpClient, String.valueOf(id)); + return APIWrapper.getSeries(httpClient, String.valueOf(id)); } /** @@ -78,12 +80,12 @@ public class MyWaifuClient { */ public static class Builder { private final String apiKey; - private final MyWaifuWrapper myWaifuWrapper; + private final APIWrapper APIWrapper; private HttpClient.Builder httpClientBuilder; public Builder(String apiKey) { this.apiKey = apiKey; - myWaifuWrapper = new MyWaifuWrapper(apiKey); + APIWrapper = new APIWrapper(apiKey); } public Builder withCookieHandler(CookieHandler cookieHandler) { diff --git a/src/main/java/org/goudham/me/exception/APIMapperException.java b/src/main/java/org/goudham/me/exception/APIMapperException.java index c016e9f..79bad38 100644 --- a/src/main/java/org/goudham/me/exception/APIMapperException.java +++ b/src/main/java/org/goudham/me/exception/APIMapperException.java @@ -1,11 +1,10 @@ package org.goudham.me.exception; - -import org.goudham.me.MyWaifuWrapper; +import org.goudham.me.APIWrapper; import org.goudham.me.Response; /** - * Thrown when {@link MyWaifuWrapper} fails to unmarshal json into Java POJO's ({@link Response#getEntity()}) + * Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#getEntity()}) * */ public class APIMapperException extends Throwable { diff --git a/src/main/java/org/goudham/me/exception/APIResponseException.java b/src/main/java/org/goudham/me/exception/APIResponseException.java index bfecdaf..53259d2 100644 --- a/src/main/java/org/goudham/me/exception/APIResponseException.java +++ b/src/main/java/org/goudham/me/exception/APIResponseException.java @@ -1,10 +1,9 @@ package org.goudham.me.exception; - -import org.goudham.me.MyWaifuWrapper; +import org.goudham.me.APIWrapper; /** - * Thrown when {@link MyWaifuWrapper} fails to return API information + * Thrown when {@link APIWrapper} fails to return API information * */ public class APIResponseException extends Throwable {