mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
deployment: Refactor ModrinthService to improve configuration handling and add timeout settings
This commit is contained in:
@@ -4,14 +4,17 @@ import com.google.gson.Gson;
|
|||||||
import de.oliver.deployment.modrinth.ModrinthService;
|
import de.oliver.deployment.modrinth.ModrinthService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private static final Gson GSON = new Gson();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws IOException {
|
||||||
String configFilePath = args[0];
|
String configFilePath = args[0];
|
||||||
Configuration configuration = GSON.fromJson(configFilePath, Configuration.class);
|
String configData = Files.readString(Path.of(configFilePath));
|
||||||
|
Configuration configuration = GSON.fromJson(configData, Configuration.class);
|
||||||
|
|
||||||
String modrinthApiKey = System.getenv("MODRINTH_API_KEY");
|
String modrinthApiKey = System.getenv("MODRINTH_API_KEY");
|
||||||
ModrinthService modrinthService = new ModrinthService(modrinthApiKey);
|
ModrinthService modrinthService = new ModrinthService(modrinthApiKey);
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ public record CreateVersionRequest(
|
|||||||
String name,
|
String name,
|
||||||
@SerializedName("version_number") String versionName,
|
@SerializedName("version_number") String versionName,
|
||||||
String changelog,
|
String changelog,
|
||||||
|
String[] dependencies,
|
||||||
@SerializedName("game_versions") String[] gameVersions,
|
@SerializedName("game_versions") String[] gameVersions,
|
||||||
@SerializedName("version_type") String versionType,
|
@SerializedName("version_type") String versionType,
|
||||||
String[] loaders,
|
String[] loaders,
|
||||||
boolean featured,
|
boolean featured,
|
||||||
String status,
|
String status,
|
||||||
|
@SerializedName("requested_status") String requestedStatus,
|
||||||
@SerializedName("project_id") String projectId,
|
@SerializedName("project_id") String projectId,
|
||||||
@SerializedName("file_parts") String[] fileParts,
|
@SerializedName("file_parts") String[] fileParts,
|
||||||
@SerializedName("primary_file") String primaryFile
|
@SerializedName("primary_file") String primaryFile
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.oliver.deployment.modrinth;
|
package de.oliver.deployment.modrinth;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
import de.oliver.deployment.Configuration;
|
import de.oliver.deployment.Configuration;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
|
||||||
@@ -8,10 +9,18 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ModrinthService {
|
public class ModrinthService {
|
||||||
private static final String BASE_URL = "https://api.modrinth.com/v2/version";
|
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||||
private final OkHttpClient client = new OkHttpClient();
|
private static final String BASE_URL = "https://api.modrinth.com/version";
|
||||||
|
private final OkHttpClient client = new OkHttpClient(
|
||||||
|
new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.callTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(60, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(60, TimeUnit.SECONDS)
|
||||||
|
);
|
||||||
private final String apiKey;
|
private final String apiKey;
|
||||||
|
|
||||||
public ModrinthService(String apiKey) {
|
public ModrinthService(String apiKey) {
|
||||||
@@ -26,29 +35,36 @@ public class ModrinthService {
|
|||||||
version,
|
version,
|
||||||
version,
|
version,
|
||||||
changelog,
|
changelog,
|
||||||
|
new String[0],
|
||||||
config.supportedVersions(),
|
config.supportedVersions(),
|
||||||
config.channel(),
|
config.channel(),
|
||||||
config.loaders(),
|
config.loaders(),
|
||||||
config.featured(),
|
config.featured(),
|
||||||
"draft",
|
"draft",
|
||||||
|
"draft",
|
||||||
config.projectID(),
|
config.projectID(),
|
||||||
new String[]{"plugin"},
|
new String[]{"plugin", "data"},
|
||||||
"plugin"
|
"plugin"
|
||||||
);
|
);
|
||||||
|
|
||||||
File pluginFile = new File(config.pluginJarPath());
|
String pluginJarPath = config.pluginJarPath().replace("%VERSION%", version);
|
||||||
|
File pluginFile = new File(pluginJarPath);
|
||||||
|
|
||||||
|
String jsonData = GSON.toJson(req);
|
||||||
|
|
||||||
RequestBody body = new MultipartBody.Builder()
|
RequestBody body = new MultipartBody.Builder()
|
||||||
.setType(MultipartBody.FORM)
|
.setType(MultipartBody.FORM)
|
||||||
.addFormDataPart("data", new Gson().toJson(req))
|
.addFormDataPart("data", jsonData)
|
||||||
.addFormDataPart("plugin", pluginFile.getName(),
|
.addFormDataPart("plugin", pluginFile.getName(),
|
||||||
RequestBody.create(pluginFile, MediaType.parse("application/java-archive")))
|
RequestBody.create(pluginFile, MediaType.parse("application/java-archive")))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
System.out.println(jsonData);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(BASE_URL)
|
.url(BASE_URL)
|
||||||
.addHeader("Authorization", apiKey)
|
|
||||||
.post(body)
|
.post(body)
|
||||||
|
.addHeader("Authorization", apiKey)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user