Add support for retrieving Waifu Images in sets of 10

pull/9/head
Hammy 4 years ago
parent 761b651fd7
commit 115710353f

@ -2,15 +2,17 @@ package me.goudham;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import me.goudham.domain.user.UserList;
import me.goudham.util.Season;
import me.goudham.domain.pagination.PaginationData;
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 java.net.URI;
import java.net.http.HttpClient;
@ -61,18 +63,21 @@ public class APIWrapper {
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}
* 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 {
private Result sendRequest(String param) throws APIResponseException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(host + param))
.version(httpClient.version())
@ -96,67 +101,72 @@ public class APIWrapper {
}
Response<Waifu> getWaifu(String waifuId) throws APIResponseException, APIMapperException {
Result waifuResult = sendRequest(httpClient, "waifu/" + waifuId);
Result waifuResult = sendRequest("waifu/" + waifuId);
return apiMapper.deserialize(waifuResult, Waifu.class);
}
Response<PaginationData<WaifuImage>> getWaifuImages(String waifuId, String pageNum) throws APIResponseException, APIMapperException {
Result waifuImagesResult = sendRequest("waifu/" + waifuId + "/images?page=" + pageNum);
return apiMapper.deserializeToPaginationData(waifuImagesResult, paginationData(WaifuImage.class));
}
Response<FilteredWaifu> getDailyWaifu() throws APIResponseException, APIMapperException {
Result dailyWaifuResult = sendRequest(httpClient, "meta/daily");
Result dailyWaifuResult = sendRequest("meta/daily");
return apiMapper.deserialize(dailyWaifuResult, FilteredWaifu.class);
}
Response<FilteredWaifu> getRandomWaifu() throws APIResponseException, APIMapperException {
Result randomWaifuResult = sendRequest(httpClient, "meta/random");
Result randomWaifuResult = sendRequest("meta/random");
return apiMapper.deserialize(randomWaifuResult, FilteredWaifu.class);
}
Response<Series> getSeries(String seriesId) throws APIResponseException, APIMapperException {
Result seriesResult = sendRequest(httpClient, "series/" + seriesId);
Result seriesResult = sendRequest("series/" + seriesId);
return apiMapper.deserialize(seriesResult, Series.class);
}
Response<List<FilteredSeries>> getSeasonalAnime() throws APIResponseException, APIMapperException {
Result seasonalAnimeResult = sendRequest(httpClient, "airing");
return apiMapper.deserialize(seasonalAnimeResult, listOf(FilteredSeries.class));
Result seasonalAnimeResult = sendRequest("airing");
return apiMapper.deserializeToList(seasonalAnimeResult, listOf(FilteredSeries.class));
}
Response<List<FilteredWaifu>> getBestWaifus() throws APIResponseException, APIMapperException {
Result waifuResults = sendRequest(httpClient, "airing/best");
return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class));
Result waifuResults = sendRequest("airing/best");
return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class));
}
Response<List<FilteredWaifu>> getPopularWaifus() throws APIResponseException, APIMapperException {
Result waifuResults = sendRequest(httpClient, "airing/popular");
return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class));
Result waifuResults = sendRequest("airing/popular");
return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class));
}
Response<List<FilteredWaifu>> getTrashWaifus() throws APIResponseException, APIMapperException {
Result waifuResults = sendRequest(httpClient, "airing/trash");
return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class));
Result waifuResults = sendRequest("airing/trash");
return apiMapper.deserializeToList(waifuResults, listOf(FilteredWaifu.class));
}
Response<List<FilteredSeries>> getAllSeries(Season season, Integer year) throws APIResponseException, APIMapperException {
Result allSeriesResult = sendRequest(httpClient, "airing/" + season.getSeason() + "/" + year);
return apiMapper.deserialize(allSeriesResult, listOf(FilteredSeries.class));
Result allSeriesResult = sendRequest("airing/" + season.getSeason() + "/" + year);
return apiMapper.deserializeToList(allSeriesResult, listOf(FilteredSeries.class));
}
Response<List<FilteredWaifu>> getSeriesWaifus(String seriesId) throws APIResponseException, APIMapperException {
Result allWaifusFromSeriesResults = sendRequest(httpClient, "series/" + seriesId + "/waifus");
return apiMapper.deserialize(allWaifusFromSeriesResults, listOf(FilteredWaifu.class));
Result allWaifusFromSeriesResults = sendRequest("series/" + seriesId + "/waifus");
return apiMapper.deserializeToList(allWaifusFromSeriesResults, listOf(FilteredWaifu.class));
}
Response<User> getUserProfile(String userId) throws APIResponseException, APIMapperException {
Result userProfileResult = sendRequest(httpClient, "user/" + userId);
Result userProfileResult = sendRequest("user/" + userId);
return apiMapper.deserialize(userProfileResult, User.class);
}
Response<List<UserList>> getUserLists(String userId) throws APIResponseException, APIMapperException {
Result userProfileResult = sendRequest(httpClient, "user/" + userId + "/lists");
return apiMapper.deserialize(userProfileResult, listOf(UserList.class));
Result userProfileResult = sendRequest("user/" + userId + "/lists");
return apiMapper.deserializeToList(userProfileResult, listOf(UserList.class));
}
Response<UserList> getUserList(String userId, String listId) throws APIResponseException, APIMapperException {
Result userProfileResult = sendRequest(httpClient, "user/" + userId + "/lists/" + listId);
Result userProfileResult = sendRequest("user/" + userId + "/lists/" + listId);
return apiMapper.deserialize(userProfileResult, UserList.class);
}
}

@ -1,6 +1,8 @@
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;
@ -84,6 +86,10 @@ public class MyWaifuClient {
return APIWrapper.getWaifu(String.valueOf(id));
}
public Response<PaginationData<WaifuImage>> getWaifuImages(@NotNull Integer id, @NotNull Integer pageNum) throws APIResponseException, APIMapperException {
return APIWrapper.getWaifuImages(String.valueOf(id), String.valueOf(pageNum));
}
public Response<FilteredWaifu> getDailyWaifu() throws APIResponseException, APIMapperException {
return APIWrapper.getDailyWaifu();
}

Loading…
Cancel
Save