From 69c0ea55f38a603a51b05be003266b7a0c508549 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 8 Jun 2021 02:44:34 +0100 Subject: [PATCH] Remove HttpClient from MyWaifuClient.java MyWaifuClient should not have to pass in HttpClient into the APIWrapper, the httpClient is now managed by the APIWrapper --- .../java/org/goudham/me/MyWaifuClient.java | 45 +++++++------------ 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/goudham/me/MyWaifuClient.java b/src/main/java/org/goudham/me/MyWaifuClient.java index ce5ce2d..7aaabf4 100644 --- a/src/main/java/org/goudham/me/MyWaifuClient.java +++ b/src/main/java/org/goudham/me/MyWaifuClient.java @@ -25,16 +25,17 @@ import java.util.concurrent.Executor; */ public class MyWaifuClient { private final APIWrapper APIWrapper; - private HttpClient httpClient; /** * Creates an instance of {@link MyWaifuClient} * *

See MyWaifuList for obtaining an API Key

* @param apiKey API Key to authorise API request + * @param httpClient The underlying {@link HttpClient} to use for HttpRequests + * */ - MyWaifuClient(@NotNull String apiKey) { - APIWrapper = new APIWrapper(apiKey); + MyWaifuClient(@NotNull String apiKey, HttpClient httpClient) { + APIWrapper = new APIWrapper(apiKey, httpClient); } /** @@ -44,39 +45,29 @@ public class MyWaifuClient { * @return {@link MyWaifuClient} */ public static MyWaifuClient createDefault(@NotNull String apiKey) { - MyWaifuClient myWaifuClient = new MyWaifuClient(apiKey); - myWaifuClient.setHttpClient(HttpClient.newBuilder() + HttpClient httpClient = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_2) .followRedirects(HttpClient.Redirect.NORMAL) .connectTimeout(Duration.ofSeconds(20)) - .build()); + .build(); - return myWaifuClient; + return new MyWaifuClient(apiKey, httpClient); } - public Response getWaifu(String slug) throws APIResponseException, APIMapperException { - return APIWrapper.getWaifu(httpClient, slug); + public Response getWaifu(@NotNull String slug) throws APIResponseException, APIMapperException { + return APIWrapper.getWaifu(slug); } - public Response getWaifu(Integer id) throws APIResponseException, APIMapperException { - return APIWrapper.getWaifu(httpClient, String.valueOf(id)); + public Response getWaifu(@NotNull Integer id) throws APIResponseException, APIMapperException { + return APIWrapper.getWaifu(String.valueOf(id)); } - public Response getSeries(Integer id) throws APIMapperException, APIResponseException { - return APIWrapper.getSeries(httpClient, String.valueOf(id)); + public Response getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException { + return APIWrapper.getSeries(String.valueOf(id)); } public Response> getAiringAnime() throws APIMapperException, APIResponseException { - return APIWrapper.getAiringAnime(httpClient); - } - - /** - * Sets an instance of HttpClient - * - * @param httpClient HttpClient for executing API requests - */ - void setHttpClient(HttpClient httpClient) { - this.httpClient = httpClient; + return APIWrapper.getAiringAnime(); } /** @@ -84,12 +75,10 @@ public class MyWaifuClient { */ public static class Builder { private final String apiKey; - private final APIWrapper APIWrapper; - private HttpClient.Builder httpClientBuilder; + private final HttpClient.Builder httpClientBuilder = HttpClient.newBuilder(); public Builder(String apiKey) { this.apiKey = apiKey; - APIWrapper = new APIWrapper(apiKey); } public Builder withCookieHandler(CookieHandler cookieHandler) { @@ -138,9 +127,7 @@ public class MyWaifuClient { } public MyWaifuClient build() { - MyWaifuClient myWaifuClient = new MyWaifuClient(apiKey); - myWaifuClient.setHttpClient(httpClientBuilder.build()); - return myWaifuClient; + return new MyWaifuClient(apiKey, httpClientBuilder.build()); } } }