From 7b0d3411832dbc29602d9c1f56cdcb7ec452171a Mon Sep 17 00:00:00 2001 From: Hammy Date: Wed, 9 Jun 2021 00:07:53 +0100 Subject: [PATCH] Add many new endpoints Coverage of the API is sitting around 40%/50% --- src/main/java/me/goudham/APIWrapper.java | 48 +++++++++++++-- src/main/java/me/goudham/MyWaifuClient.java | 65 ++++++++++++++++----- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/main/java/me/goudham/APIWrapper.java b/src/main/java/me/goudham/APIWrapper.java index d982453..e8a9d5b 100644 --- a/src/main/java/me/goudham/APIWrapper.java +++ b/src/main/java/me/goudham/APIWrapper.java @@ -2,8 +2,10 @@ package me.goudham; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.type.TypeFactory; +import me.goudham.util.Season; import me.goudham.api.entity.series.FilteredSeries; import me.goudham.api.entity.series.Series; +import me.goudham.api.entity.user.User; import me.goudham.api.entity.waifu.FilteredWaifu; import me.goudham.api.entity.waifu.Waifu; import me.goudham.exception.APIMapperException; @@ -21,6 +23,7 @@ import java.util.concurrent.ExecutionException; /** * Returns API information to {@link MyWaifuClient} + * */ public class APIWrapper { private final String version = "1.0"; @@ -97,8 +100,13 @@ public class APIWrapper { } Response getDailyWaifu() throws APIResponseException, APIMapperException { - Result waifuResult = sendRequest(httpClient, "meta/daily"); - return apiMapper.deserialize(waifuResult, FilteredWaifu.class); + Result dailyWaifuResult = sendRequest(httpClient, "meta/daily"); + return apiMapper.deserialize(dailyWaifuResult, FilteredWaifu.class); + } + + Response getRandomWaifu() throws APIResponseException, APIMapperException { + Result randomWaifuResult = sendRequest(httpClient, "meta/random"); + return apiMapper.deserialize(randomWaifuResult, FilteredWaifu.class); } Response getSeries(String param) throws APIResponseException, APIMapperException { @@ -106,8 +114,38 @@ public class APIWrapper { return apiMapper.deserialize(seriesResult, Series.class); } - Response> getAiringAnime() throws APIResponseException, APIMapperException { - Result seriesResult = sendRequest(httpClient, "airing"); - return apiMapper.deserialize(seriesResult, listOf(FilteredSeries.class)); + Response> getSeasonalAnime() throws APIResponseException, APIMapperException { + Result seasonalAnimeResult = sendRequest(httpClient, "airing"); + return apiMapper.deserialize(seasonalAnimeResult, listOf(FilteredSeries.class)); + } + + Response> getBestWaifus() throws APIResponseException, APIMapperException { + Result waifuResults = sendRequest(httpClient, "airing/best"); + return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class)); + } + + Response> getPopularWaifus() throws APIResponseException, APIMapperException { + Result waifuResults = sendRequest(httpClient, "airing/popular"); + return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class)); + } + + Response> getTrashWaifus() throws APIResponseException, APIMapperException { + Result waifuResults = sendRequest(httpClient, "airing/trash"); + return apiMapper.deserialize(waifuResults, listOf(FilteredWaifu.class)); + } + + Response> getAllSeries(Season season, Integer year) throws APIResponseException, APIMapperException { + Result allSeriesResult = sendRequest(httpClient, "airing/" + season.getSeason() + "/" + year); + return apiMapper.deserialize(allSeriesResult, listOf(FilteredSeries.class)); + } + + Response> getSeriesWaifus(String param) throws APIResponseException, APIMapperException { + Result allWaifusFromSeriesResults = sendRequest(httpClient, "series/" + param + "/waifus"); + return apiMapper.deserialize(allWaifusFromSeriesResults, listOf(FilteredWaifu.class)); + } + + Response getUserProfile(String param) throws APIResponseException, APIMapperException { + Result userProfileResult = sendRequest(httpClient, "user/" + param); + return apiMapper.deserialize(userProfileResult, User.class); } } diff --git a/src/main/java/me/goudham/MyWaifuClient.java b/src/main/java/me/goudham/MyWaifuClient.java index cc8f197..ae94994 100644 --- a/src/main/java/me/goudham/MyWaifuClient.java +++ b/src/main/java/me/goudham/MyWaifuClient.java @@ -1,7 +1,9 @@ package me.goudham; +import me.goudham.util.Season; import me.goudham.api.entity.series.FilteredSeries; import me.goudham.api.entity.series.Series; +import me.goudham.api.entity.user.User; import me.goudham.api.entity.waifu.FilteredWaifu; import me.goudham.api.entity.waifu.Waifu; import me.goudham.exception.APIMapperException; @@ -35,7 +37,7 @@ public class MyWaifuClient { * @param httpClient The underlying {@link HttpClient} to use for HttpRequests * */ - MyWaifuClient(@NotNull String apiKey, HttpClient httpClient) { + MyWaifuClient(@NotNull String apiKey, @NotNull HttpClient httpClient) { APIWrapper = new APIWrapper(apiKey, httpClient); } @@ -59,7 +61,7 @@ public class MyWaifuClient { return APIWrapper.getWaifu(slug); } - public Response getWaifu(@NotNull Integer id) throws APIResponseException, APIMapperException { + Response getWaifu(@NotNull Integer id) throws APIResponseException, APIMapperException { return APIWrapper.getWaifu(String.valueOf(id)); } @@ -67,22 +69,59 @@ public class MyWaifuClient { return APIWrapper.getDailyWaifu(); } + public Response getRandomWaifu() throws APIResponseException, APIMapperException { + return APIWrapper.getRandomWaifu(); + } + + public Response getSeries(@NotNull String param) throws APIMapperException, APIResponseException { + return APIWrapper.getSeries(param); + } + public Response getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException { return APIWrapper.getSeries(String.valueOf(id)); } - public Response> getAiringAnime() throws APIMapperException, APIResponseException { - return APIWrapper.getAiringAnime(); + public Response> getSeasonalAnime() throws APIMapperException, APIResponseException { + return APIWrapper.getSeasonalAnime(); + } + + public Response> getBestWaifus() throws APIMapperException, APIResponseException { + return APIWrapper.getBestWaifus(); + } + + public Response> getPopularWaifus() throws APIMapperException, APIResponseException { + return APIWrapper.getPopularWaifus(); + } + + public Response> getTrashWaifus() throws APIMapperException, APIResponseException { + return APIWrapper.getTrashWaifus(); + } + + public Response> getAllSeries(@NotNull Season season, @NotNull Integer year) throws APIResponseException, APIMapperException { + return APIWrapper.getAllSeries(season, year); + } + + public Response> getSeriesWaifus(@NotNull String slug) throws APIMapperException, APIResponseException { + return APIWrapper.getSeriesWaifus(slug); + } + + public Response> getSeriesWaifus(@NotNull Integer id) throws APIMapperException, APIResponseException { + return APIWrapper.getSeriesWaifus(String.valueOf(id)); + } + + public Response getUserProfile(@NotNull Integer id) throws APIMapperException, APIResponseException { + return APIWrapper.getUserProfile(String.valueOf(id)); } /** - * Builder for MyWaifuClient + * Builder for {@link MyWaifuClient} + * */ public static class Builder { private final String apiKey; private final HttpClient.Builder httpClientBuilder = HttpClient.newBuilder(); - public Builder(String apiKey) { + public Builder(@NotNull String apiKey) { this.apiKey = apiKey; } @@ -91,27 +130,27 @@ public class MyWaifuClient { return this; } - public Builder withConnectTimeout(Duration duration) { + public Builder withConnectTimeout(@NotNull Duration duration) { httpClientBuilder.connectTimeout(duration); return this; } - public Builder withSslParameters(SSLParameters sslParameters) { + public Builder withSslParameters(@NotNull SSLParameters sslParameters) { httpClientBuilder.sslParameters(sslParameters); return this; } - public Builder withExecutor(Executor executor) { + public Builder withExecutor(@NotNull Executor executor) { httpClientBuilder.executor(executor); return this; } - public Builder withFollowRedirects(HttpClient.Redirect policy) { + public Builder withFollowRedirects(@NotNull HttpClient.Redirect policy) { httpClientBuilder.followRedirects(policy); return this; } - public Builder withVersion(HttpClient.Version version) { + public Builder withVersion(@NotNull HttpClient.Version version) { httpClientBuilder.version(version); return this; } @@ -121,12 +160,12 @@ public class MyWaifuClient { return this; } - public Builder withProxy(ProxySelector proxySelector) { + public Builder withProxy(@NotNull ProxySelector proxySelector) { httpClientBuilder.proxy(proxySelector); return this; } - public Builder withAuthenticator(Authenticator authenticator) { + public Builder withAuthenticator(@NotNull Authenticator authenticator) { httpClientBuilder.authenticator(authenticator); return this; }