Remove HttpClient from MyWaifuClient.java

MyWaifuClient should not have to pass in HttpClient into the APIWrapper, the httpClient is now managed by the APIWrapper
pull/9/head
Hammy 3 years ago
parent d931330750
commit 69c0ea55f3

@ -25,16 +25,17 @@ import java.util.concurrent.Executor;
*/ */
public class MyWaifuClient { public class MyWaifuClient {
private final APIWrapper APIWrapper; private final APIWrapper APIWrapper;
private HttpClient httpClient;
/** /**
* Creates an instance of {@link MyWaifuClient} * Creates an instance of {@link MyWaifuClient}
* *
* <p>See <a href="https://mywaifulist.docs.stoplight.io/">MyWaifuList</a> for obtaining an API Key</p> * <p>See <a href="https://mywaifulist.docs.stoplight.io/">MyWaifuList</a> for obtaining an API Key</p>
* @param apiKey API Key to authorise API request * @param apiKey API Key to authorise API request
* @param httpClient The underlying {@link HttpClient} to use for HttpRequests
*
*/ */
MyWaifuClient(@NotNull String apiKey) { MyWaifuClient(@NotNull String apiKey, HttpClient httpClient) {
APIWrapper = new APIWrapper(apiKey); APIWrapper = new APIWrapper(apiKey, httpClient);
} }
/** /**
@ -44,39 +45,29 @@ public class MyWaifuClient {
* @return {@link MyWaifuClient} * @return {@link MyWaifuClient}
*/ */
public static MyWaifuClient createDefault(@NotNull String apiKey) { public static MyWaifuClient createDefault(@NotNull String apiKey) {
MyWaifuClient myWaifuClient = new MyWaifuClient(apiKey); HttpClient httpClient = HttpClient.newBuilder()
myWaifuClient.setHttpClient(HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2) .version(HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.NORMAL) .followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20)) .connectTimeout(Duration.ofSeconds(20))
.build()); .build();
return myWaifuClient; return new MyWaifuClient(apiKey, httpClient);
} }
public Response<Waifu> getWaifu(String slug) throws APIResponseException, APIMapperException { public Response<Waifu> getWaifu(@NotNull String slug) throws APIResponseException, APIMapperException {
return APIWrapper.getWaifu(httpClient, slug); return APIWrapper.getWaifu(slug);
} }
public Response<Waifu> getWaifu(Integer id) throws APIResponseException, APIMapperException { public Response<Waifu> getWaifu(@NotNull Integer id) throws APIResponseException, APIMapperException {
return APIWrapper.getWaifu(httpClient, String.valueOf(id)); return APIWrapper.getWaifu(String.valueOf(id));
} }
public Response<Series> getSeries(Integer id) throws APIMapperException, APIResponseException { public Response<Series> getSeries(@NotNull Integer id) throws APIMapperException, APIResponseException {
return APIWrapper.getSeries(httpClient, String.valueOf(id)); return APIWrapper.getSeries(String.valueOf(id));
} }
public Response<List<FilteredSeries>> getAiringAnime() throws APIMapperException, APIResponseException { public Response<List<FilteredSeries>> getAiringAnime() throws APIMapperException, APIResponseException {
return APIWrapper.getAiringAnime(httpClient); return APIWrapper.getAiringAnime();
}
/**
* Sets an instance of HttpClient
*
* @param httpClient HttpClient for executing API requests
*/
void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
} }
/** /**
@ -84,12 +75,10 @@ public class MyWaifuClient {
*/ */
public static class Builder { public static class Builder {
private final String apiKey; private final String apiKey;
private final APIWrapper APIWrapper; private final HttpClient.Builder httpClientBuilder = HttpClient.newBuilder();
private HttpClient.Builder httpClientBuilder;
public Builder(String apiKey) { public Builder(String apiKey) {
this.apiKey = apiKey; this.apiKey = apiKey;
APIWrapper = new APIWrapper(apiKey);
} }
public Builder withCookieHandler(CookieHandler cookieHandler) { public Builder withCookieHandler(CookieHandler cookieHandler) {
@ -138,9 +127,7 @@ public class MyWaifuClient {
} }
public MyWaifuClient build() { public MyWaifuClient build() {
MyWaifuClient myWaifuClient = new MyWaifuClient(apiKey); return new MyWaifuClient(apiKey, httpClientBuilder.build());
myWaifuClient.setHttpClient(httpClientBuilder.build());
return myWaifuClient;
} }
} }
} }

Loading…
Cancel
Save