From 209406a5e0976fb2831a4d095034eb679fb6f245 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Sat, 18 Nov 2023 23:15:40 +0100 Subject: [PATCH] Allow CC Requests to accept custom HTTP client --- .../de/florianmichael/classic4j/ClassiCubeHandler.java | 9 +++++---- .../classicube/auth/CCAuthenticationLoginRequest.java | 5 +++-- .../classicube/auth/CCAuthenticationTokenRequest.java | 5 +++-- .../request/classicube/server/CCServerInfoRequest.java | 5 +++-- .../request/classicube/server/CCServerListRequest.java | 5 +++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/florianmichael/classic4j/ClassiCubeHandler.java b/src/main/java/de/florianmichael/classic4j/ClassiCubeHandler.java index 5988d51..9ff23e6 100644 --- a/src/main/java/de/florianmichael/classic4j/ClassiCubeHandler.java +++ b/src/main/java/de/florianmichael/classic4j/ClassiCubeHandler.java @@ -26,6 +26,7 @@ import de.florianmichael.classic4j.request.classicube.server.CCServerListRequest; import de.florianmichael.classic4j.model.classicube.server.CCServerList; import de.florianmichael.classic4j.model.classicube.account.CCAccount; +import de.florianmichael.classic4j.util.WebUtils; import javax.security.auth.login.LoginException; import java.net.URI; @@ -64,7 +65,7 @@ public static void requestServerList(final CCAccount account, final Consumer complete, final Consumer throwableConsumer) { - CCServerListRequest.send(account).whenComplete((ccServerList, throwable) -> { + CCServerListRequest.send(WebUtils.HTTP_CLIENT, account).whenComplete((ccServerList, throwable) -> { if (throwable != null) { throwableConsumer.accept(throwable); return; @@ -114,7 +115,7 @@ public static void requestServerInfo(final CCAccount account, final List * @param throwableConsumer The consumer that will be called when an error occurs. */ public static void requestServerInfo(final CCAccount account, final List serverHashes, final Consumer complete, final Consumer throwableConsumer) { - CCServerInfoRequest.send(account, serverHashes).whenComplete((ccServerList, throwable) -> { + CCServerInfoRequest.send(WebUtils.HTTP_CLIENT, account, serverHashes).whenComplete((ccServerList, throwable) -> { if (throwable != null) { throwableConsumer.accept(throwable); return; @@ -136,7 +137,7 @@ public static void requestServerInfo(final CCAccount account, final List * @param processHandler The handler to use for the login process. */ public static void requestAuthentication(final CCAccount account, final String loginCode, final LoginProcessHandler processHandler) { - CCAuthenticationTokenRequest.send(account).whenComplete((initialTokenResponse, throwable) -> { + CCAuthenticationTokenRequest.send(WebUtils.HTTP_CLIENT, account).whenComplete((initialTokenResponse, throwable) -> { if (throwable != null) { processHandler.handleException(throwable); return; @@ -149,7 +150,7 @@ public static void requestAuthentication(final CCAccount account, final String l } account.token = initialTokenResponse.token; - CCAuthenticationLoginRequest.send(account, initialTokenResponse, loginCode).whenComplete((loginResponse, throwable1) -> { + CCAuthenticationLoginRequest.send(WebUtils.HTTP_CLIENT, account, initialTokenResponse, loginCode).whenComplete((loginResponse, throwable1) -> { if (throwable1 != null) { processHandler.handleException(throwable1); return; diff --git a/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationLoginRequest.java b/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationLoginRequest.java index 94075fa..38819f3 100644 --- a/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationLoginRequest.java +++ b/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationLoginRequest.java @@ -24,6 +24,7 @@ import de.florianmichael.classic4j.util.model.Parameter; import de.florianmichael.classic4j.util.WebUtils; +import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.concurrent.CompletableFuture; @@ -40,7 +41,7 @@ public class CCAuthenticationLoginRequest { * @param loginCode The login code. * @return The response. */ - public static CompletableFuture send(final CCAccount account, final CCAuthenticationResponse previousResponse, final String loginCode) { + public static CompletableFuture send(final HttpClient client, final CCAccount account, final CCAuthenticationResponse previousResponse, final String loginCode) { return CompletableFuture.supplyAsync(() -> { final CCAuthenticationData authenticationData = new CCAuthenticationData(account.username(), account.password(), previousResponse.token, loginCode); @@ -56,7 +57,7 @@ public static CompletableFuture send(final CCAccount a .uri(ClassiCubeHandler.AUTHENTICATION_URI) .header("content-type", "application/x-www-form-urlencoded")); - final HttpResponse response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); + final HttpResponse response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); WebUtils.updateCookies(account.cookieStore, response); diff --git a/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationTokenRequest.java b/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationTokenRequest.java index 0f59deb..85bf5b9 100644 --- a/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationTokenRequest.java +++ b/src/main/java/de/florianmichael/classic4j/request/classicube/auth/CCAuthenticationTokenRequest.java @@ -22,6 +22,7 @@ import de.florianmichael.classic4j.model.classicube.account.CCAccount; import de.florianmichael.classic4j.util.WebUtils; +import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.concurrent.CompletableFuture; @@ -36,11 +37,11 @@ public class CCAuthenticationTokenRequest { * @param account The account to get the token for. * @return The response. */ - public static CompletableFuture send(final CCAccount account) { + public static CompletableFuture send(final HttpClient client, final CCAccount account) { return CompletableFuture.supplyAsync(() -> { final HttpRequest request = HttpRequest.newBuilder().GET().uri(ClassiCubeHandler.AUTHENTICATION_URI).build(); - final HttpResponse response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); + final HttpResponse response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); WebUtils.updateCookies(account.cookieStore, response); final String responseBody = response.body(); diff --git a/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerInfoRequest.java b/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerInfoRequest.java index 135911f..ce8a8c9 100644 --- a/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerInfoRequest.java +++ b/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerInfoRequest.java @@ -23,6 +23,7 @@ import de.florianmichael.classic4j.util.WebUtils; import java.net.URI; +import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.List; @@ -45,12 +46,12 @@ private static URI generateUri(final List serverHashes) { * @param serverHashes The hashes of the servers to get information about. * @return A CompletableFuture containing the response. */ - public static CompletableFuture send(final CCAccount account, final List serverHashes) { + public static CompletableFuture send(final HttpClient client, final CCAccount account, final List serverHashes) { return CompletableFuture.supplyAsync(() -> { final URI uri = generateUri(serverHashes); final HttpRequest request = WebUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder().GET().uri(uri)); - final HttpResponse response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); + final HttpResponse response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); WebUtils.updateCookies(account.cookieStore, response); diff --git a/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerListRequest.java b/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerListRequest.java index d7b8f11..bf4df1f 100644 --- a/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerListRequest.java +++ b/src/main/java/de/florianmichael/classic4j/request/classicube/server/CCServerListRequest.java @@ -22,17 +22,18 @@ import de.florianmichael.classic4j.model.classicube.account.CCAccount; import de.florianmichael.classic4j.util.WebUtils; +import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.concurrent.CompletableFuture; public class CCServerListRequest { - public static CompletableFuture send(final CCAccount account) { + public static CompletableFuture send(final HttpClient client, final CCAccount account) { return CompletableFuture.supplyAsync(() -> { final HttpRequest request = WebUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder().GET().uri(ClassiCubeHandler.SERVER_LIST_INFO_URI)); - final HttpResponse response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); + final HttpResponse response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join(); WebUtils.updateCookies(account.cookieStore, response);