implemented repository design pattern
This commit is contained in:
parent
31581fc643
commit
86677c942f
@ -1,6 +1,6 @@
|
|||||||
package eu.mhsl.craftattack.spawn;
|
package eu.mhsl.craftattack.spawn;
|
||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.api.HttpServer;
|
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.api.client;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class HttpRepository extends Repository {
|
||||||
|
Consumer<URIBuilder> baseUriBuilder;
|
||||||
|
public HttpRepository(URI basePath, Consumer<URIBuilder> baseUriBuilder) {
|
||||||
|
super(basePath);
|
||||||
|
this.baseUriBuilder = baseUriBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public record RequestResponse<TData>(int status, TData data) {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <TInput, TOutput> RequestResponse<TOutput> post(String command, TInput data, Class<TOutput> clazz) {
|
||||||
|
return this.post(command, uriBuilder -> baseUriBuilder.accept(uriBuilder), data, clazz);
|
||||||
|
}
|
||||||
|
protected <TInput, TOutput> RequestResponse<TOutput> post(String command, Consumer<URIBuilder> parameters, TInput data, Class<TOutput> clazz) {
|
||||||
|
HttpRequest request = this.getRequestBuilder(this.getUri(command))
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(data)))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
RequestResponse<String> rawResponse = this.runRequest(request);
|
||||||
|
return new RequestResponse<>(rawResponse.status, new Gson().fromJson(rawResponse.data, clazz));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <TOutput> RequestResponse<TOutput> get(String command, Class<TOutput> clazz) {
|
||||||
|
HttpRequest request = this.getRequestBuilder(this.getUri(command))
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
RequestResponse<String> rawResponse = this.runRequest(request);
|
||||||
|
return new RequestResponse<>(rawResponse.status, new Gson().fromJson(rawResponse.data, clazz));
|
||||||
|
}
|
||||||
|
|
||||||
|
private URI getUri(String command) {
|
||||||
|
try {
|
||||||
|
URIBuilder builder = new URIBuilder(this.basePath + "/" + command);
|
||||||
|
this.baseUriBuilder.accept(builder);
|
||||||
|
return builder.build();
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequest.Builder getRequestBuilder(URI endpoint) {
|
||||||
|
return HttpRequest.newBuilder()
|
||||||
|
.uri(endpoint)
|
||||||
|
.header("Content-Type", "application/json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestResponse<String> runRequest(HttpRequest request) {
|
||||||
|
try(HttpClient client = HttpClient.newHttpClient()) {
|
||||||
|
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
return new RequestResponse<>(httpResponse.statusCode(), httpResponse.body());
|
||||||
|
} catch(IOException | InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.api.client;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class Repository {
|
||||||
|
protected URI basePath;
|
||||||
|
|
||||||
|
public Repository(URI basePath) {
|
||||||
|
this.basePath = basePath;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.api.client.repositories;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.api.client.HttpRepository;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class ReportRepository extends HttpRepository {
|
||||||
|
public ReportRepository() throws URISyntaxException {
|
||||||
|
super(new URI("asdasdas"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public record SendReportResponse(UUID user) {}
|
||||||
|
public record ReportInput(UUID user) {}
|
||||||
|
|
||||||
|
public RequestResponse<SendReportResponse> sendReport(ReportInput input) {
|
||||||
|
return this.post("reports", (builder) -> builder.addParameter("token", "asd"), input, SendReportResponse.class);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package eu.mhsl.craftattack.spawn.api;
|
package eu.mhsl.craftattack.spawn.api.server;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
@ -1,7 +1,7 @@
|
|||||||
package eu.mhsl.craftattack.spawn.appliance;
|
package eu.mhsl.craftattack.spawn.appliance;
|
||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
import eu.mhsl.craftattack.spawn.api.HttpServer;
|
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
|
@ -2,7 +2,7 @@ package eu.mhsl.craftattack.spawn.appliances.event;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
import eu.mhsl.craftattack.spawn.api.HttpServer;
|
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.customAdvancements.Advancements;
|
import eu.mhsl.craftattack.spawn.appliances.customAdvancements.Advancements;
|
||||||
|
@ -4,7 +4,7 @@ import com.google.gson.Gson;
|
|||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
|
||||||
import eu.mhsl.craftattack.spawn.util.server.ApiUtil;
|
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.ComponentBuilder;
|
import net.kyori.adventure.text.ComponentBuilder;
|
||||||
import net.kyori.adventure.text.TextComponent;
|
import net.kyori.adventure.text.TextComponent;
|
||||||
|
@ -2,10 +2,10 @@ package eu.mhsl.craftattack.spawn.appliances.whitelist;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import eu.mhsl.craftattack.spawn.Main;
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
import eu.mhsl.craftattack.spawn.api.HttpServer;
|
import eu.mhsl.craftattack.spawn.api.server.HttpServer;
|
||||||
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
||||||
import eu.mhsl.craftattack.spawn.util.server.ApiUtil;
|
import eu.mhsl.craftattack.spawn.util.api.ApiUtil;
|
||||||
import eu.mhsl.craftattack.spawn.util.server.Floodgate;
|
import eu.mhsl.craftattack.spawn.util.server.Floodgate;
|
||||||
import eu.mhsl.craftattack.spawn.util.text.DisconnectInfo;
|
import eu.mhsl.craftattack.spawn.util.text.DisconnectInfo;
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
@ -98,7 +98,7 @@ public class Whitelist extends Appliance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private UserData fetchUserData(UUID uuid) throws DisconnectInfo.Throwable {
|
private UserData fetchUserData(UUID uuid) throws DisconnectInfo.Throwable {
|
||||||
URIBuilder uriBuilder = new URIBuilder(apiEndpoint);
|
URIBuilder uriBuilder = new URIBuilder(this.apiEndpoint);
|
||||||
uriBuilder.addParameter("secret", this.apiSecret);
|
uriBuilder.addParameter("secret", this.apiSecret);
|
||||||
uriBuilder.addParameter("uuid", uuid.toString());
|
uriBuilder.addParameter("uuid", uuid.toString());
|
||||||
|
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.util.api;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.whitelist.Whitelist;
|
||||||
|
import eu.mhsl.craftattack.spawn.config.Configuration;
|
||||||
|
import eu.mhsl.craftattack.spawn.util.text.DisconnectInfo;
|
||||||
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class ApiUtil {
|
||||||
|
private static URI whitelistEndpoint;
|
||||||
|
private static URI reportEndpoint;
|
||||||
|
private static String baseUrl;
|
||||||
|
private static String apiSecret;
|
||||||
|
|
||||||
|
public static String getApiSecret() {
|
||||||
|
if(apiSecret == null) {
|
||||||
|
apiSecret = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("secret");
|
||||||
|
}
|
||||||
|
return apiSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getBaseUrl() {
|
||||||
|
if(baseUrl == null) {
|
||||||
|
baseUrl = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("baseurl");
|
||||||
|
}
|
||||||
|
return baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static URI getWhitelistEndpoint() {
|
||||||
|
if(whitelistEndpoint == null) {
|
||||||
|
String configValue = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("whitelist");
|
||||||
|
whitelistEndpoint = URI.create(getBaseUrl() + Objects.requireNonNull(configValue));
|
||||||
|
}
|
||||||
|
return whitelistEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static URI getReportEndpoint() {
|
||||||
|
if(reportEndpoint == null) {
|
||||||
|
String configValue = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("report");
|
||||||
|
reportEndpoint = URI.create(getBaseUrl() + Objects.requireNonNull(configValue));
|
||||||
|
}
|
||||||
|
return reportEndpoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static HttpResponse<String> getHttpResponse(URI endpoint, ) {
|
||||||
|
URIBuilder uriBuilder = new URIBuilder(endpoint);
|
||||||
|
uriBuilder.addParameter("secret", apiSecret);
|
||||||
|
uriBuilder.addParameter("uuid", uuid.toString());
|
||||||
|
|
||||||
|
try(HttpClient client = HttpClient.newHttpClient()) {
|
||||||
|
HttpRequest httpRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(uriBuilder.build())
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if(httpResponse.statusCode() == 404)
|
||||||
|
throw new DisconnectInfo.Throwable(
|
||||||
|
"Nicht angemeldet",
|
||||||
|
"Du bist derzeit nicht als Teilnehmer des CraftAttack-Projektes registriert!",
|
||||||
|
"Melde Dich bei einem Admin für eine nachträgliche Anmeldung.",
|
||||||
|
uuid
|
||||||
|
);
|
||||||
|
|
||||||
|
return new Gson().fromJson(httpResponse.body(), Whitelist.UserData.class);
|
||||||
|
|
||||||
|
} catch(IOException | InterruptedException | URISyntaxException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,43 +0,0 @@
|
|||||||
package eu.mhsl.craftattack.spawn.util.server;
|
|
||||||
|
|
||||||
import eu.mhsl.craftattack.spawn.config.Configuration;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class ApiUtil {
|
|
||||||
private static URI whitelistEndpoint;
|
|
||||||
private static URI reportEndpoint;
|
|
||||||
private static String baseUrl;
|
|
||||||
private static String apiSecret;
|
|
||||||
|
|
||||||
public static String getApiSecret() {
|
|
||||||
if(apiSecret == null) {
|
|
||||||
apiSecret = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("secret");
|
|
||||||
}
|
|
||||||
return apiSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getBaseUrl() {
|
|
||||||
if(baseUrl == null) {
|
|
||||||
baseUrl = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("baseurl");
|
|
||||||
}
|
|
||||||
return baseUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static URI getWhitelistEndpoint() {
|
|
||||||
if(whitelistEndpoint == null) {
|
|
||||||
String configValue = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("whitelist");
|
|
||||||
whitelistEndpoint = URI.create(getBaseUrl() + Objects.requireNonNull(configValue));
|
|
||||||
}
|
|
||||||
return whitelistEndpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static URI getReportEndpoint() {
|
|
||||||
if(reportEndpoint == null) {
|
|
||||||
String configValue = Objects.requireNonNull(Configuration.cfg.getConfigurationSection("api")).getString("report");
|
|
||||||
reportEndpoint = URI.create(getBaseUrl() + Objects.requireNonNull(configValue));
|
|
||||||
}
|
|
||||||
return reportEndpoint;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user