Add support for retrieving series by page

pull/9/head
Hammy 3 years ago
parent 2b9d47cc8d
commit eb43a8baf9

@ -1,7 +1,5 @@
package me.goudham; 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.pagination.PaginationData;
import me.goudham.domain.series.FilteredSeries; import me.goudham.domain.series.FilteredSeries;
import me.goudham.domain.series.Series; import me.goudham.domain.series.Series;
@ -14,6 +12,7 @@ import me.goudham.exception.APIMapperException;
import me.goudham.exception.APIResponseException; import me.goudham.exception.APIResponseException;
import me.goudham.util.Season; import me.goudham.util.Season;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
@ -22,6 +21,11 @@ import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; 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 APIMapper apiMapper;
private final HttpClient httpClient; private final HttpClient httpClient;
private final Executor executor = Executors.newFixedThreadPool(10);
/** /**
* Instantiates an instance of {@link APIWrapper} 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 * An instance of {@link APIMapper} is created to be able to {@code deserialize} JSON to
* Java objects * Java objects
* *
* @param apiKey API Key to authorise API request * @param apiKey API Key to authorise API request
@ -51,22 +56,6 @@ public class APIWrapper {
apiMapper = new APIMapper(); 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 <T> The type of model to be returned. E.g {@link Waifu} or {@link Series}
* @return {@link JavaType}
*
*/
private <T> JavaType listOf(Class<T> model) {
return TypeFactory.defaultInstance().constructCollectionType(List.class, model);
}
private <T> JavaType paginationData(Class<T> model) {
return TypeFactory.defaultInstance().constructParametricType(PaginationData.class, model);
}
/** /**
* Handles sending a request to the API asynchronously using {@link HttpRequest} * Handles sending a request to the API asynchronously using {@link HttpRequest}
* and the underlying {@link HttpClient} * and the underlying {@link HttpClient}
@ -78,26 +67,28 @@ public class APIWrapper {
* *
*/ */
private Result sendRequest(String param) throws APIResponseException { private Result sendRequest(String param) throws APIResponseException {
HttpRequest request = HttpRequest.newBuilder() CompletableFuture<Result> futureResult = CompletableFuture.supplyAsync(() -> {
.uri(URI.create(host + param)) HttpRequest request = HttpRequest.newBuilder()
.version(httpClient.version()) .uri(URI.create(host + param))
.timeout(Duration.ofSeconds(20)) .version(httpClient.version())
.headers("Content-Type", "application/json", "apikey", apiKey) .timeout(Duration.ofSeconds(20))
.GET() .headers("Content-Type", "application/json", "apikey", apiKey)
.build(); .GET()
.build();
CompletableFuture<HttpResponse<String>> response = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
try {
int responseCode; return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
String responseBody; } catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
return null;
}, executor).thenApply(httpResponse -> new Result(httpResponse.statusCode(), httpResponse.body()));
try { try {
responseCode = response.thenApply(HttpResponse::statusCode).get(); return futureResult.get();
responseBody = response.thenApply(HttpResponse::body).get();
} catch (InterruptedException | ExecutionException exp) { } catch (InterruptedException | ExecutionException exp) {
throw new APIResponseException(exp.getMessage(), exp); throw new APIResponseException(exp.getMessage(), exp);
} }
return new Result(responseCode, responseBody);
} }
Response<Waifu> getWaifu(String waifuId) throws APIResponseException, APIMapperException { Response<Waifu> getWaifu(String waifuId) throws APIResponseException, APIMapperException {
@ -120,11 +111,6 @@ public class APIWrapper {
return apiMapper.deserialize(randomWaifuResult, FilteredWaifu.class); return apiMapper.deserialize(randomWaifuResult, FilteredWaifu.class);
} }
Response<Series> getSeries(String seriesId) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest("series/" + seriesId);
return apiMapper.deserialize(seriesResult, Series.class);
}
Response<List<FilteredSeries>> getSeasonalAnime() throws APIResponseException, APIMapperException { Response<List<FilteredSeries>> getSeasonalAnime() throws APIResponseException, APIMapperException {
Result seasonalAnimeResult = sendRequest("airing"); Result seasonalAnimeResult = sendRequest("airing");
return apiMapper.deserializeToList(seasonalAnimeResult, listOf(FilteredSeries.class)); return apiMapper.deserializeToList(seasonalAnimeResult, listOf(FilteredSeries.class));
@ -145,6 +131,16 @@ public class APIWrapper {
return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class)); return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class));
} }
Response<Series> getSeries(String seriesId) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest("series/" + seriesId);
return apiMapper.deserialize(seriesResult, Series.class);
}
Response<PaginationData<FilteredSeries>> getSeriesByPage(String pageNum) throws APIResponseException, APIMapperException {
Result seriesPageResult = sendRequest("series?page=" + pageNum);
return apiMapper.deserializeToPaginationData(seriesPageResult, paginationData(FilteredSeries.class));
}
Response<List<FilteredSeries>> getAllSeries(Season season, Integer year) throws APIResponseException, APIMapperException { Response<List<FilteredSeries>> getAllSeries(Season season, Integer year) throws APIResponseException, APIMapperException {
Result allSeriesResult = sendRequest("airing/" + season.getSeason() + "/" + year); Result allSeriesResult = sendRequest("airing/" + season.getSeason() + "/" + year);
return apiMapper.deserializeToList(allSeriesResult, listOf(FilteredSeries.class)); return apiMapper.deserializeToList(allSeriesResult, listOf(FilteredSeries.class));

@ -1,16 +1,16 @@
package me.goudham; package me.goudham;
import me.goudham.domain.pagination.PaginationData; 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.FilteredSeries;
import me.goudham.domain.series.Series; import me.goudham.domain.series.Series;
import me.goudham.domain.user.User; import me.goudham.domain.user.User;
import me.goudham.domain.user.UserList;
import me.goudham.domain.waifu.FilteredWaifu; import me.goudham.domain.waifu.FilteredWaifu;
import me.goudham.domain.waifu.Waifu; import me.goudham.domain.waifu.Waifu;
import me.goudham.domain.waifu.WaifuImage;
import me.goudham.exception.APIMapperException; import me.goudham.exception.APIMapperException;
import me.goudham.exception.APIResponseException; import me.goudham.exception.APIResponseException;
import me.goudham.util.Season;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLParameters;
@ -98,14 +98,6 @@ public class MyWaifuClient {
return APIWrapper.getRandomWaifu(); return APIWrapper.getRandomWaifu();
} }
public Response<Series> getSeries(@NotNull String slug) throws APIMapperException, APIResponseException {
return APIWrapper.getSeries(slug);
}
public Response<Series> getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException {
return APIWrapper.getSeries(String.valueOf(id));
}
public Response<List<FilteredSeries>> getSeasonalAnime() throws APIMapperException, APIResponseException { public Response<List<FilteredSeries>> getSeasonalAnime() throws APIMapperException, APIResponseException {
return APIWrapper.getSeasonalAnime(); return APIWrapper.getSeasonalAnime();
} }
@ -122,6 +114,18 @@ public class MyWaifuClient {
return APIWrapper.getTrashWaifus(); return APIWrapper.getTrashWaifus();
} }
public Response<Series> getSeries(@NotNull String slug) throws APIMapperException, APIResponseException {
return APIWrapper.getSeries(slug);
}
public Response<Series> getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException {
return APIWrapper.getSeries(String.valueOf(id));
}
public Response<PaginationData<FilteredSeries>> getSeriesByPage(@NotNull Integer pageNum) throws APIMapperException, APIResponseException {
return APIWrapper.getSeriesByPage(String.valueOf(pageNum));
}
public Response<List<FilteredSeries>> getAllSeries(@NotNull Season season, @NotNull Integer year) throws APIResponseException, APIMapperException { public Response<List<FilteredSeries>> getAllSeries(@NotNull Season season, @NotNull Integer year) throws APIResponseException, APIMapperException {
return APIWrapper.getAllSeries(season, year); return APIWrapper.getAllSeries(season, year);
} }

Loading…
Cancel
Save