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 java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Main {
|
||||
|
||||
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];
|
||||
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");
|
||||
ModrinthService modrinthService = new ModrinthService(modrinthApiKey);
|
||||
|
||||
@@ -6,11 +6,13 @@ public record CreateVersionRequest(
|
||||
String name,
|
||||
@SerializedName("version_number") String versionName,
|
||||
String changelog,
|
||||
String[] dependencies,
|
||||
@SerializedName("game_versions") String[] gameVersions,
|
||||
@SerializedName("version_type") String versionType,
|
||||
String[] loaders,
|
||||
boolean featured,
|
||||
String status,
|
||||
@SerializedName("requested_status") String requestedStatus,
|
||||
@SerializedName("project_id") String projectId,
|
||||
@SerializedName("file_parts") String[] fileParts,
|
||||
@SerializedName("primary_file") String primaryFile
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.oliver.deployment.modrinth;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import de.oliver.deployment.Configuration;
|
||||
import okhttp3.*;
|
||||
|
||||
@@ -8,10 +9,18 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ModrinthService {
|
||||
private static final String BASE_URL = "https://api.modrinth.com/v2/version";
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
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;
|
||||
|
||||
public ModrinthService(String apiKey) {
|
||||
@@ -26,29 +35,36 @@ public class ModrinthService {
|
||||
version,
|
||||
version,
|
||||
changelog,
|
||||
new String[0],
|
||||
config.supportedVersions(),
|
||||
config.channel(),
|
||||
config.loaders(),
|
||||
config.featured(),
|
||||
"draft",
|
||||
"draft",
|
||||
config.projectID(),
|
||||
new String[]{"plugin"},
|
||||
new String[]{"plugin", "data"},
|
||||
"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()
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("data", new Gson().toJson(req))
|
||||
.addFormDataPart("data", jsonData)
|
||||
.addFormDataPart("plugin", pluginFile.getName(),
|
||||
RequestBody.create(pluginFile, MediaType.parse("application/java-archive")))
|
||||
.build();
|
||||
|
||||
System.out.println(jsonData);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(BASE_URL)
|
||||
.addHeader("Authorization", apiKey)
|
||||
.post(body)
|
||||
.addHeader("Authorization", apiKey)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
|
||||
Reference in New Issue
Block a user