From 256341a88d30914017de1da21491d7590a5bfc52 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 6 Jun 2021 22:23:05 +0100 Subject: [PATCH] Add support for retrieving series by ID --- .../java/org/goudham/me/MyWaifuClient.java | 10 +++- .../java/org/goudham/me/MyWaifuWrapper.java | 48 ++++++++++++------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index 2068320..fbcff31 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -1,6 +1,8 @@ package org.goudham.me; +import org.goudham.me.api.entity.series.Series; import org.goudham.me.api.entity.waifu.Waifu; +import org.goudham.me.exception.APIMapperException; import org.goudham.me.exception.APIResponseException; import org.jetbrains.annotations.NotNull; @@ -50,14 +52,18 @@ public class MyWaifuClient { return myWaifuClient; } - public Response getWaifu(String slug) throws APIResponseException { + public Response getWaifu(String slug) throws APIResponseException, APIMapperException { return myWaifuWrapper.getWaifu(httpClient, slug); } - public Response getWaifu(Integer id) throws APIResponseException { + public Response getWaifu(Integer id) throws APIResponseException, APIMapperException { return myWaifuWrapper.getWaifu(httpClient, String.valueOf(id)); } + public Response getSeries(Integer id) throws APIMapperException, APIResponseException { + return myWaifuWrapper.getSeries(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 662da1c..aa71979 100644 --- a/src/main/java/org/goudham/me/MyWaifuWrapper.java +++ b/src/main/java/org/goudham/me/MyWaifuWrapper.java @@ -3,7 +3,9 @@ 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.Series; import org.goudham.me.api.entity.waifu.Waifu; +import org.goudham.me.exception.APIMapperException; import org.goudham.me.exception.APIResponseException; import java.net.URI; @@ -33,19 +35,24 @@ public class MyWaifuWrapper { this.apiKey = apiKey; } + /** - * + * Handles sending a request to the API asynchronously using {@link HttpRequest} + * and the underlying {@link HttpClient} * * @param httpClient The {@link HttpClient} to use for sending {@link HttpRequest}'s * @param param The end of the endpoint appended onto the host + * @return {@link Result} + * @throws APIResponseException If the {@link CompletableFuture Response} + * cannot be decoded or the thread was interrupted while waiting to receive the data + * */ private Result sendRequest(HttpClient httpClient, String param) throws APIResponseException { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(host + param)) .version(httpClient.version()) - .timeout(Duration.ofSeconds(30)) - .header("Content-Type", "application/json") - .header("apikey", apiKey) + .timeout(Duration.ofSeconds(20)) + .headers("Content-Type", "application/json", "apikey", apiKey) .GET() .build(); @@ -63,26 +70,35 @@ public class MyWaifuWrapper { return new Result(responseCode, responseBody); } - 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; + 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 waifuData = parent.get("data").toString(); - waifu = objectMapper.readValue(waifuData, Waifu.class); + String data = parent.get("data").toString(); + newEntity = objectMapper.readValue(data, entity); } catch (JsonProcessingException jpe) { - jpe.printStackTrace(); + 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(); - 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}"; + throw new APIMapperException(exceptionMessage, jpe); } } - return new Response<>(statusCode, body, waifu); + 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); + } + + Response getSeries(HttpClient httpClient, String param) throws APIResponseException, APIMapperException { + Result seriesResult = sendRequest(httpClient, "series/" + param); + return getPopulatedResponse(seriesResult, Series.class); } }