Skip to content

Commit

Permalink
Structural refactor of http utils
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 28, 2024
1 parent b8ed7bb commit 024ea43
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import de.florianmichael.classic4j.api.JoinServerInterface;
import de.florianmichael.classic4j.model.betacraft.BCServerList;
import de.florianmichael.classic4j.request.betacraft.BCServerListRequest;
import de.florianmichael.classic4j.util.WebUtils;
import de.florianmichael.classic4j.util.HttpClientUtils;

import java.io.InputStream;
import java.net.InetAddress;
Expand Down Expand Up @@ -132,7 +132,7 @@ public static void requestV1ServerList(final Consumer<BCServerList> complete, fi
* @param throwableConsumer The consumer that will be called when an error occurs.
*/
private static void requestServerList(final BCServerListRequest request, final Consumer<BCServerList> complete, final Consumer<Throwable> throwableConsumer) {
request.send(WebUtils.HTTP_CLIENT, ClassiCubeHandler.GSON).whenComplete((bcServerList, throwable) -> {
request.send(HttpClientUtils.HTTP_CLIENT, ClassiCubeHandler.GSON).whenComplete((bcServerList, throwable) -> {
if (throwable != null) {
throwableConsumer.accept(throwable);
return;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/florianmichael/classic4j/ClassiCubeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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 de.florianmichael.classic4j.util.HttpClientUtils;

import javax.security.auth.login.LoginException;
import java.net.URI;
Expand Down Expand Up @@ -66,7 +66,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(WebUtils.HTTP_CLIENT, account).whenComplete((ccServerList, throwable) -> {
CCServerListRequest.send(HttpClientUtils.HTTP_CLIENT, account).whenComplete((ccServerList, throwable) -> {
if (throwable != null) {
throwableConsumer.accept(throwable);
return;
Expand Down Expand Up @@ -116,7 +116,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(WebUtils.HTTP_CLIENT, account, serverHashes).whenComplete((ccServerList, throwable) -> {
CCServerInfoRequest.send(HttpClientUtils.HTTP_CLIENT, account, serverHashes).whenComplete((ccServerList, throwable) -> {
if (throwable != null) {
throwableConsumer.accept(throwable);
return;
Expand All @@ -138,7 +138,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(WebUtils.HTTP_CLIENT, account).whenComplete((initialTokenResponse, throwable) -> {
CCAuthenticationTokenRequest.send(HttpClientUtils.HTTP_CLIENT, account).whenComplete((initialTokenResponse, throwable) -> {
if (throwable != null) {
processHandler.handleException(throwable);
return;
Expand All @@ -151,7 +151,7 @@ public static void requestAuthentication(final CCAccount account, final String l
}
account.token = initialTokenResponse.token;

CCAuthenticationLoginRequest.send(WebUtils.HTTP_CLIENT, account, initialTokenResponse, loginCode).whenComplete((loginResponse, throwable1) -> {
CCAuthenticationLoginRequest.send(HttpClientUtils.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 @@ -18,7 +18,7 @@
package de.florianmichael.classic4j.model.classicube.account;

import com.google.gson.JsonObject;
import de.florianmichael.classic4j.util.model.CookieStore;
import de.florianmichael.classic4j.util.CookieStore;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import de.florianmichael.classic4j.model.classicube.CCAuthenticationResponse;
import de.florianmichael.classic4j.model.classicube.account.CCAccount;
import de.florianmichael.classic4j.model.classicube.account.CCAuthenticationData;
import de.florianmichael.classic4j.util.model.Parameter;
import de.florianmichael.classic4j.util.WebUtils;
import de.florianmichael.classic4j.util.Parameter;
import de.florianmichael.classic4j.util.HttpClientUtils;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -45,21 +45,21 @@ public static CompletableFuture<CCAuthenticationResponse> send(final HttpClient
return CompletableFuture.supplyAsync(() -> {
final CCAuthenticationData authenticationData = new CCAuthenticationData(account.username(), account.password(), previousResponse.token, loginCode);

final String requestBody = WebUtils.createRequestBody(
final String requestBody = HttpClientUtils.createRequestBody(
new Parameter("username", authenticationData.username()),
new Parameter("password", authenticationData.password()),
new Parameter("token", authenticationData.previousToken()),
new Parameter("login_code", authenticationData.loginCode())
);

final HttpRequest request = WebUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder()
final HttpRequest request = HttpClientUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.uri(ClassiCubeHandler.AUTHENTICATION_URI)
.header("content-type", "application/x-www-form-urlencoded"));

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

WebUtils.updateCookies(account.cookieStore, response);
HttpClientUtils.updateCookies(account.cookieStore, response);

final String responseBody = response.body();
return CCAuthenticationResponse.fromJson(responseBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import de.florianmichael.classic4j.ClassiCubeHandler;
import de.florianmichael.classic4j.model.classicube.CCAuthenticationResponse;
import de.florianmichael.classic4j.model.classicube.account.CCAccount;
import de.florianmichael.classic4j.util.WebUtils;
import de.florianmichael.classic4j.util.HttpClientUtils;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -43,7 +43,7 @@ public static CompletableFuture<CCAuthenticationResponse> send(final HttpClient

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

WebUtils.updateCookies(account.cookieStore, response);
HttpClientUtils.updateCookies(account.cookieStore, response);
final String responseBody = response.body();
return CCAuthenticationResponse.fromJson(responseBody);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import de.florianmichael.classic4j.ClassiCubeHandler;
import de.florianmichael.classic4j.model.classicube.server.CCServerList;
import de.florianmichael.classic4j.model.classicube.account.CCAccount;
import de.florianmichael.classic4j.util.WebUtils;
import de.florianmichael.classic4j.util.HttpClientUtils;

import java.net.URI;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -50,10 +50,10 @@ public static CompletableFuture<CCServerList> send(final HttpClient client, fina
return CompletableFuture.supplyAsync(() -> {
final URI uri = generateUri(serverHashes);

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

WebUtils.updateCookies(account.cookieStore, response);
HttpClientUtils.updateCookies(account.cookieStore, response);

final String body = response.body();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import de.florianmichael.classic4j.ClassiCubeHandler;
import de.florianmichael.classic4j.model.classicube.server.CCServerList;
import de.florianmichael.classic4j.model.classicube.account.CCAccount;
import de.florianmichael.classic4j.util.WebUtils;
import de.florianmichael.classic4j.util.HttpClientUtils;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -41,11 +41,11 @@ public class CCServerListRequest {
*/
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 HttpRequest request = HttpClientUtils.buildWithCookies(account.cookieStore, HttpRequest.newBuilder().GET().uri(ClassiCubeHandler.SERVER_LIST_INFO_URI));

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

WebUtils.updateCookies(account.cookieStore, response);
HttpClientUtils.updateCookies(account.cookieStore, response);

final String body = response.body();
return CCServerList.fromJson(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package de.florianmichael.classic4j.util.model;
package de.florianmichael.classic4j.util;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -65,13 +65,10 @@ public String toString() {
}

public void mergeFromResponse(HttpResponse<?> response) {
final Optional<String> setCookieHeaderOptional = response.headers()
.firstValue("set-cookie");

final Optional<String> setCookieHeaderOptional = response.headers().firstValue("set-cookie");
if (setCookieHeaderOptional.isEmpty()) {
return;
}

final String setCookieHeader = setCookieHeaderOptional.get();
final CookieStore store = CookieStore.parse(setCookieHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

package de.florianmichael.classic4j.util;

import de.florianmichael.classic4j.util.model.CookieStore;
import de.florianmichael.classic4j.util.model.Parameter;

import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

public class WebUtils {
public class HttpClientUtils {

public static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package de.florianmichael.classic4j.util.model;
package de.florianmichael.classic4j.util;

public record Parameter(String name, String value) {

Expand Down

0 comments on commit 024ea43

Please sign in to comment.