From 32068c4c554b0ede54d30862bb04cab743cde3cb Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 8 Jun 2021 02:45:33 +0100 Subject: [PATCH] Refactor deserialize & deserializeToList() methods Making methods more readable through type signatures and how the method is being called within APIWrapper.java --- src/main/java/org/goudham/me/APIMapper.java | 19 +++----------- src/main/java/org/goudham/me/APIWrapper.java | 26 ++++++++++++++++---- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/goudham/me/APIMapper.java b/src/main/java/org/goudham/me/APIMapper.java index b398f76..1eea24a 100644 --- a/src/main/java/org/goudham/me/APIMapper.java +++ b/src/main/java/org/goudham/me/APIMapper.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.type.TypeFactory; import org.goudham.me.api.entity.series.FilteredSeries; import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.waifu.Waifu; @@ -23,18 +22,6 @@ class APIMapper { objectMapper = new ObjectMapper(); } - /** - * Honestly I don't really know how this works - * - * @param entity The actual class of the given entity. E.g {@link Waifu#getClass()} - * @param The type of entity to be returned. E.g {@link Waifu} or {@link Series} - * @return {@link JavaType} - * - */ - private JavaType listOf(Class entity) { - return TypeFactory.defaultInstance().constructCollectionType(List.class, entity); - } - /** * Using the given {@code entity}, {@link ObjectMapper} deserializes the given Json * into a Java POJO @@ -67,13 +54,13 @@ class APIMapper { * Using the given {@code entity}, {@link ObjectMapper} deserializes the given Json * into a Java POJO. This method enables support for retrieving {@link List} of entities * + * @param List of entities to be returned. E.g {@link List} of {@link FilteredSeries} * @param result The result of the previous API response * @param entity The actual class of the given entity. E.g {@link Waifu#getClass()} - * @param List of entities to be returned. E.g {@link List} of {@link FilteredSeries} * @return {@link Response} * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly */ - Response> deserializeToList(Result result, Class entity) throws APIMapperException { + Response> deserialize(Result result, JavaType entity) throws APIMapperException { Integer statusCode = result.getStatusCode(); String body = result.getBody(); List listOfEntity = null; @@ -81,7 +68,7 @@ class APIMapper { if (statusCode == 200) { try { String data = getJsonTree(body); - listOfEntity = objectMapper.readValue(data, listOf(entity)); + listOfEntity = objectMapper.readValue(data, entity); } catch (JsonProcessingException jpe) { throwAPIMapperException(jpe); } diff --git a/src/main/java/org/goudham/me/APIWrapper.java b/src/main/java/org/goudham/me/APIWrapper.java index 8838ffe..5d59f2a 100644 --- a/src/main/java/org/goudham/me/APIWrapper.java +++ b/src/main/java/org/goudham/me/APIWrapper.java @@ -1,5 +1,7 @@ package org.goudham.me; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.type.TypeFactory; import org.goudham.me.api.entity.series.FilteredSeries; import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.waifu.Waifu; @@ -25,6 +27,7 @@ public class APIWrapper { private final String apiKey; private final APIMapper apiMapper; + private final HttpClient httpClient; /** * Instantiates an instance of {@link APIWrapper} to retrieve API Information. @@ -32,13 +35,26 @@ public class APIWrapper { * Java objects * * @param apiKey API Key to authorise API request + * @param httpClient The underlying {@link HttpClient} to use for HttpRequests * */ - APIWrapper(String apiKey) { + APIWrapper(String apiKey, HttpClient httpClient) { this.apiKey = apiKey; + this.httpClient = httpClient; apiMapper = new APIMapper(); } + /** + * Honestly I don't really know how this works + * + * @param entity The actual class of the given entity. E.g {@link Waifu#getClass()} + * @param The type of entity to be returned. E.g {@link Waifu} or {@link Series} + * @return {@link JavaType} + * + */ + private JavaType listOf(Class entity) { + return TypeFactory.defaultInstance().constructCollectionType(List.class, entity); + } /** * Handles sending a request to the API asynchronously using {@link HttpRequest} @@ -74,18 +90,18 @@ public class APIWrapper { return new Result(responseCode, responseBody); } - Response getWaifu(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { + Response getWaifu(String param) throws APIResponseException, APIMapperException { Result waifuResult = sendRequest(httpClient, "waifu/" + param); return apiMapper.deserialize(waifuResult, Waifu.class); } - Response getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { + Response getSeries(String param) throws APIResponseException, APIMapperException { Result seriesResult = sendRequest(httpClient, "series/" + param); return apiMapper.deserialize(seriesResult, Series.class); } - Response> getAiringAnime(HttpClient httpClient) throws APIResponseException, APIMapperException { + Response> getAiringAnime() throws APIResponseException, APIMapperException { Result seriesResult = sendRequest(httpClient, "airing"); - return apiMapper.deserializeToList(seriesResult, FilteredSeries.class); + return apiMapper.deserialize(seriesResult, listOf(FilteredSeries.class)); } }