From eb43a8baf9e0abdd450b09c15874be30d6b1679a Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 13 Jun 2021 01:23:29 +0100 Subject: [PATCH] Add support for retrieving series by page --- src/main/java/me/goudham/APIWrapper.java | 76 ++++++++++----------- src/main/java/me/goudham/MyWaifuClient.java | 26 ++++--- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/main/java/me/goudham/APIWrapper.java b/src/main/java/me/goudham/APIWrapper.java index 88c1173..95aff97 100644 --- a/src/main/java/me/goudham/APIWrapper.java +++ b/src/main/java/me/goudham/APIWrapper.java @@ -1,7 +1,5 @@ package me.goudham; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.type.TypeFactory; import me.goudham.domain.pagination.PaginationData; import me.goudham.domain.series.FilteredSeries; import me.goudham.domain.series.Series; @@ -14,6 +12,7 @@ import me.goudham.exception.APIMapperException; import me.goudham.exception.APIResponseException; import me.goudham.util.Season; +import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -22,6 +21,11 @@ import java.time.Duration; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import static me.goudham.APIUtils.listOf; +import static me.goudham.APIUtils.paginationData; /** @@ -35,10 +39,11 @@ public class APIWrapper { private final APIMapper apiMapper; private final HttpClient httpClient; + private final Executor executor = Executors.newFixedThreadPool(10); /** * 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 + * An instance of {@link APIMapper} is created to be able to {@code deserialize} JSON to * Java objects * * @param apiKey API Key to authorise API request @@ -51,22 +56,6 @@ public class APIWrapper { apiMapper = new APIMapper(); } - /** - * Honestly I don't really know how this works - * - * @param model The actual class of the given model. E.g {@link Waifu#getClass()} - * @param The type of model to be returned. E.g {@link Waifu} or {@link Series} - * @return {@link JavaType} - * - */ - private JavaType listOf(Class model) { - return TypeFactory.defaultInstance().constructCollectionType(List.class, model); - } - - private JavaType paginationData(Class model) { - return TypeFactory.defaultInstance().constructParametricType(PaginationData.class, model); - } - /** * Handles sending a request to the API asynchronously using {@link HttpRequest} * and the underlying {@link HttpClient} @@ -78,26 +67,28 @@ public class APIWrapper { * */ private Result sendRequest(String param) throws APIResponseException { - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(host + param)) - .version(httpClient.version()) - .timeout(Duration.ofSeconds(20)) - .headers("Content-Type", "application/json", "apikey", apiKey) - .GET() - .build(); - - CompletableFuture> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); - - int responseCode; - String responseBody; + CompletableFuture futureResult = CompletableFuture.supplyAsync(() -> { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(host + param)) + .version(httpClient.version()) + .timeout(Duration.ofSeconds(20)) + .headers("Content-Type", "application/json", "apikey", apiKey) + .GET() + .build(); + + try { + return httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } catch (IOException | InterruptedException exp) { + exp.printStackTrace(); + } + return null; + }, executor).thenApply(httpResponse -> new Result(httpResponse.statusCode(), httpResponse.body())); + try { - responseCode = response.thenApply(HttpResponse::statusCode).get(); - responseBody = response.thenApply(HttpResponse::body).get(); + return futureResult.get(); } catch (InterruptedException | ExecutionException exp) { throw new APIResponseException(exp.getMessage(), exp); } - - return new Result(responseCode, responseBody); } Response getWaifu(String waifuId) throws APIResponseException, APIMapperException { @@ -120,11 +111,6 @@ public class APIWrapper { return apiMapper.deserialize(randomWaifuResult, FilteredWaifu.class); } - Response getSeries(String seriesId) throws APIResponseException, APIMapperException { - Result seriesResult = sendRequest("series/" + seriesId); - return apiMapper.deserialize(seriesResult, Series.class); - } - Response> getSeasonalAnime() throws APIResponseException, APIMapperException { Result seasonalAnimeResult = sendRequest("airing"); return apiMapper.deserializeToList(seasonalAnimeResult, listOf(FilteredSeries.class)); @@ -145,6 +131,16 @@ public class APIWrapper { return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class)); } + Response getSeries(String seriesId) throws APIResponseException, APIMapperException { + Result seriesResult = sendRequest("series/" + seriesId); + return apiMapper.deserialize(seriesResult, Series.class); + } + + Response> getSeriesByPage(String pageNum) throws APIResponseException, APIMapperException { + Result seriesPageResult = sendRequest("series?page=" + pageNum); + return apiMapper.deserializeToPaginationData(seriesPageResult, paginationData(FilteredSeries.class)); + } + Response> getAllSeries(Season season, Integer year) throws APIResponseException, APIMapperException { Result allSeriesResult = sendRequest("airing/" + season.getSeason() + "/" + year); return apiMapper.deserializeToList(allSeriesResult, listOf(FilteredSeries.class)); diff --git a/src/main/java/me/goudham/MyWaifuClient.java b/src/main/java/me/goudham/MyWaifuClient.java index 7ca4f98..67bc5cb 100644 --- a/src/main/java/me/goudham/MyWaifuClient.java +++ b/src/main/java/me/goudham/MyWaifuClient.java @@ -1,16 +1,16 @@ package me.goudham; import me.goudham.domain.pagination.PaginationData; -import me.goudham.domain.user.UserList; -import me.goudham.domain.waifu.WaifuImage; -import me.goudham.util.Season; import me.goudham.domain.series.FilteredSeries; import me.goudham.domain.series.Series; import me.goudham.domain.user.User; +import me.goudham.domain.user.UserList; import me.goudham.domain.waifu.FilteredWaifu; import me.goudham.domain.waifu.Waifu; +import me.goudham.domain.waifu.WaifuImage; import me.goudham.exception.APIMapperException; import me.goudham.exception.APIResponseException; +import me.goudham.util.Season; import org.jetbrains.annotations.NotNull; import javax.net.ssl.SSLParameters; @@ -98,14 +98,6 @@ public class MyWaifuClient { return APIWrapper.getRandomWaifu(); } - public Response getSeries(@NotNull String slug) throws APIMapperException, APIResponseException { - return APIWrapper.getSeries(slug); - } - - public Response getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException { - return APIWrapper.getSeries(String.valueOf(id)); - } - public Response> getSeasonalAnime() throws APIMapperException, APIResponseException { return APIWrapper.getSeasonalAnime(); } @@ -122,6 +114,18 @@ public class MyWaifuClient { return APIWrapper.getTrashWaifus(); } + public Response getSeries(@NotNull String slug) throws APIMapperException, APIResponseException { + return APIWrapper.getSeries(slug); + } + + public Response getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException { + return APIWrapper.getSeries(String.valueOf(id)); + } + + public Response> getSeriesByPage(@NotNull Integer pageNum) throws APIMapperException, APIResponseException { + return APIWrapper.getSeriesByPage(String.valueOf(pageNum)); + } + public Response> getAllSeries(@NotNull Season season, @NotNull Integer year) throws APIResponseException, APIMapperException { return APIWrapper.getAllSeries(season, year); }