Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/api/ModrinthApi.java

53 lines
2.5 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.launcher.api;
2021-11-02 16:44:26 +01:00
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.modrinth.ModrinthProject.GC_ModrinthProject;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.modrinth.ModrinthSearchResult.GC_ModrinthSearchResult;
import gsoncompile.extensions.io.gitlab.jfronny.inceptum.launcher.model.modrinth.ModrinthVersion.GC_ModrinthVersion;
2022-11-02 00:38:04 +01:00
import io.gitlab.jfronny.gson.compile.util.GList;
import io.gitlab.jfronny.gson.stream.JsonReader;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.common.Net;
import io.gitlab.jfronny.inceptum.launcher.model.modrinth.*;
2021-11-02 16:44:26 +01:00
import java.io.IOException;
import java.io.StringReader;
2022-09-04 21:21:24 +02:00
import java.util.*;
2021-11-02 16:44:26 +01:00
public class ModrinthApi {
private static final String API_HOST = "https://api.modrinth.com/";
private static final int ITEMS_PER_PAGE = 20;
2021-11-02 19:52:35 +01:00
//TODO search by categories: facets:[["versions:$ver","versions:$ver"],["categories:$cat","categories:$cat"]]
//TODO filter server/client-only mods
2022-02-28 10:32:20 +01:00
public static ModrinthSearchResult search(String query, int page, String version, ModrinthProjectType type) throws IOException {
2022-09-04 21:21:24 +02:00
return Net.downloadObject(Net.buildUrl(API_HOST, "v2/search", Map.of(
2021-11-02 16:44:26 +01:00
"query", query,
2022-02-28 10:32:20 +01:00
"facets", "[[\"versions:" + version + "\"],[\"categories:fabric\"],[\"project_type:" + type + "\"]]",
2021-11-02 16:44:26 +01:00
"index", "relevance",
"offset", Integer.toString(page * ITEMS_PER_PAGE),
"limit", Integer.toString(ITEMS_PER_PAGE)
2022-11-02 00:38:04 +01:00
)), GC_ModrinthSearchResult::read);
2021-11-02 16:44:26 +01:00
}
2022-02-28 10:32:20 +01:00
public static ModrinthProject getMod(String id) throws IOException {
2022-11-02 00:38:04 +01:00
return Net.downloadObject(API_HOST + "v2/project/" + id, GC_ModrinthProject::read);
2021-11-02 16:44:26 +01:00
}
public static List<ModrinthVersion> getVersions(String mod) throws IOException {
2022-11-02 00:38:04 +01:00
List<ModrinthVersion> versions = Net.downloadObject(API_HOST + "v2/project/" + mod + "/version", s -> {
try (JsonReader r = new JsonReader(new StringReader(s))) {
return GList.read(r, GC_ModrinthVersion::read);
}
});
versions.sort(Comparator.comparing(version -> version.date_published));
return versions;
2021-11-02 16:44:26 +01:00
}
public static ModrinthVersion getVersion(String id) throws IOException {
2022-11-02 00:38:04 +01:00
return Net.downloadObject(API_HOST + "v2/version/" + id, GC_ModrinthVersion::read);
2021-11-02 16:44:26 +01:00
}
2021-11-02 19:52:35 +01:00
public static ModrinthVersion getVersionByHash(String sha1) throws IOException {
2022-11-02 00:38:04 +01:00
return Net.downloadObject(API_HOST + "v2/version_file/" + sha1 + "?algorithm=sha1", GC_ModrinthVersion::read);
2021-11-02 19:52:35 +01:00
}
2021-11-02 16:44:26 +01:00
}