Skip to content

Commit

Permalink
Allow CC Requests to accept custom HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Nov 18, 2023
1 parent 4fc9e00 commit 209406a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static void requestServerList(final CCAccount account, final Consumer<CCS
* @param throwableConsumer The consumer that will be called when an error occurs.
*/
public static void requestServerList(final CCAccount account, final Consumer<CCServerList> complete, final Consumer<Throwable> throwableConsumer) {
CCServerListRequest.send(account).whenComplete((ccServerList, throwable) -> {
CCServerListRequest.send(WebUtils.HTTP_CLIENT, account).whenComplete((ccServerList, throwable) -> {
if (throwable != null) {
throwableConsumer.accept(throwable);
return;
Expand Down Expand Up @@ -114,7 +115,7 @@ public static void requestServerInfo(final CCAccount account, final List<String>
* @param throwableConsumer The consumer that will be called when an error occurs.
*/
public static void requestServerInfo(final CCAccount account, final List<String> serverHashes, final Consumer<CCServerList> complete, final Consumer<Throwable> 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;
Expand All @@ -136,7 +137,7 @@ public static void requestServerInfo(final CCAccount account, final List<String>
* @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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +41,7 @@ public class CCAuthenticationLoginRequest {
* @param loginCode The login code.
* @return The response.
*/
public static CompletableFuture<CCAuthenticationResponse> send(final CCAccount account, final CCAuthenticationResponse previousResponse, final String loginCode) {
public static CompletableFuture<CCAuthenticationResponse> 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);

Expand All @@ -56,7 +57,7 @@ public static CompletableFuture<CCAuthenticationResponse> send(final CCAccount a
.uri(ClassiCubeHandler.AUTHENTICATION_URI)
.header("content-type", "application/x-www-form-urlencoded"));

final HttpResponse<String> response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();
final HttpResponse<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();

WebUtils.updateCookies(account.cookieStore, response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,11 +37,11 @@ public class CCAuthenticationTokenRequest {
* @param account The account to get the token for.
* @return The response.
*/
public static CompletableFuture<CCAuthenticationResponse> send(final CCAccount account) {
public static CompletableFuture<CCAuthenticationResponse> send(final HttpClient client, final CCAccount account) {
return CompletableFuture.supplyAsync(() -> {
final HttpRequest request = HttpRequest.newBuilder().GET().uri(ClassiCubeHandler.AUTHENTICATION_URI).build();

final HttpResponse<String> response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();
final HttpResponse<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();

WebUtils.updateCookies(account.cookieStore, response);
final String responseBody = response.body();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,12 +46,12 @@ private static URI generateUri(final List<String> serverHashes) {
* @param serverHashes The hashes of the servers to get information about.
* @return A CompletableFuture containing the response.
*/
public static CompletableFuture<CCServerList> send(final CCAccount account, final List<String> serverHashes) {
public static CompletableFuture<CCServerList> send(final HttpClient client, final CCAccount account, final List<String> serverHashes) {
return CompletableFuture.supplyAsync(() -> {
final URI uri = generateUri(serverHashes);

final HttpRequest request = WebUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder().GET().uri(uri));
final HttpResponse<String> response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();
final HttpResponse<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();

WebUtils.updateCookies(account.cookieStore, response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CCServerList> send(final CCAccount account) {
public static CompletableFuture<CCServerList> 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<String> response = WebUtils.HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();
final HttpResponse<String> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).join();

WebUtils.updateCookies(account.cookieStore, response);

Expand Down

0 comments on commit 209406a

Please sign in to comment.