Refactor deserialize & deserializeToList() methods

Making methods more readable through type signatures and how the method is being called within APIWrapper.java
pull/9/head
Hammy 3 years ago
parent 69c0ea55f3
commit 32068c4c55

@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; 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.FilteredSeries;
import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.api.entity.waifu.Waifu;
@ -23,18 +22,6 @@ class APIMapper {
objectMapper = new ObjectMapper(); 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 <T> The type of entity to be returned. E.g {@link Waifu} or {@link Series}
* @return {@link JavaType}
*
*/
private <T> JavaType listOf(Class<T> entity) {
return TypeFactory.defaultInstance().constructCollectionType(List.class, entity);
}
/** /**
* Using the given {@code entity}, {@link ObjectMapper} deserializes the given Json * Using the given {@code entity}, {@link ObjectMapper} deserializes the given Json
* into a Java POJO * into a Java POJO
@ -67,13 +54,13 @@ class APIMapper {
* Using the given {@code entity}, {@link ObjectMapper} deserializes the given Json * 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 * into a Java POJO. This method enables support for retrieving {@link List} of entities
* *
* @param <T> List of entities to be returned. E.g {@link List} of {@link FilteredSeries}
* @param result The result of the previous API response * @param result The result of the previous API response
* @param entity The actual class of the given entity. E.g {@link Waifu#getClass()} * @param entity The actual class of the given entity. E.g {@link Waifu#getClass()}
* @param <T> List of entities to be returned. E.g {@link List} of {@link FilteredSeries}
* @return {@link Response} * @return {@link Response}
* @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly * @throws APIMapperException If {@link ObjectMapper} is not able to deserialize JSON to Java POJO properly
*/ */
<T> Response<List<T>> deserializeToList(Result result, Class<T> entity) throws APIMapperException { <T> Response<List<T>> deserialize(Result result, JavaType entity) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.getStatusCode();
String body = result.getBody(); String body = result.getBody();
List<T> listOfEntity = null; List<T> listOfEntity = null;
@ -81,7 +68,7 @@ class APIMapper {
if (statusCode == 200) { if (statusCode == 200) {
try { try {
String data = getJsonTree(body); String data = getJsonTree(body);
listOfEntity = objectMapper.readValue(data, listOf(entity)); listOfEntity = objectMapper.readValue(data, entity);
} catch (JsonProcessingException jpe) { } catch (JsonProcessingException jpe) {
throwAPIMapperException(jpe); throwAPIMapperException(jpe);
} }

@ -1,5 +1,7 @@
package org.goudham.me; 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.FilteredSeries;
import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.series.Series;
import org.goudham.me.api.entity.waifu.Waifu; import org.goudham.me.api.entity.waifu.Waifu;
@ -25,6 +27,7 @@ public class APIWrapper {
private final String apiKey; private final String apiKey;
private final APIMapper apiMapper; private final APIMapper apiMapper;
private final HttpClient httpClient;
/** /**
* Instantiates an instance of {@link APIWrapper} to retrieve API Information. * Instantiates an instance of {@link APIWrapper} to retrieve API Information.
@ -32,13 +35,26 @@ public class APIWrapper {
* Java objects * Java objects
* *
* @param apiKey API Key to authorise API request * @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.apiKey = apiKey;
this.httpClient = httpClient;
apiMapper = new APIMapper(); 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 <T> The type of entity to be returned. E.g {@link Waifu} or {@link Series}
* @return {@link JavaType}
*
*/
private <T> JavaType listOf(Class<T> entity) {
return TypeFactory.defaultInstance().constructCollectionType(List.class, entity);
}
/** /**
* Handles sending a request to the API asynchronously using {@link HttpRequest} * Handles sending a request to the API asynchronously using {@link HttpRequest}
@ -74,18 +90,18 @@ public class APIWrapper {
return new Result(responseCode, responseBody); return new Result(responseCode, responseBody);
} }
Response<Waifu> getWaifu(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { Response<Waifu> getWaifu(String param) throws APIResponseException, APIMapperException {
Result waifuResult = sendRequest(httpClient, "waifu/" + param); Result waifuResult = sendRequest(httpClient, "waifu/" + param);
return apiMapper.deserialize(waifuResult, Waifu.class); return apiMapper.deserialize(waifuResult, Waifu.class);
} }
Response<Series> getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { Response<Series> getSeries(String param) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest(httpClient, "series/" + param); Result seriesResult = sendRequest(httpClient, "series/" + param);
return apiMapper.deserialize(seriesResult, Series.class); return apiMapper.deserialize(seriesResult, Series.class);
} }
Response<List<FilteredSeries>> getAiringAnime(HttpClient httpClient) throws APIResponseException, APIMapperException { Response<List<FilteredSeries>> getAiringAnime() throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest(httpClient, "airing"); Result seriesResult = sendRequest(httpClient, "airing");
return apiMapper.deserializeToList(seriesResult, FilteredSeries.class); return apiMapper.deserialize(seriesResult, listOf(FilteredSeries.class));
} }
} }

Loading…
Cancel
Save