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

46 lines
2.1 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.api;
import io.gitlab.jfronny.gson.reflect.TypeToken;
import io.gitlab.jfronny.inceptum.common.Net;
import io.gitlab.jfronny.inceptum.launcher.model.modrinth.*;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
public class ModrinthApi {
private static final String API_HOST = "https://api.modrinth.com/";
private static final int ITEMS_PER_PAGE = 20;
private static final Type modrinthVersionListType = new TypeToken<List<ModrinthVersion>>() {}.getType();
//TODO search by categories: facets:[["versions:$ver","versions:$ver"],["categories:$cat","categories:$cat"]]
//TODO filter server/client-only mods
public static ModrinthSearchResult search(String query, int page, String version, ModrinthProjectType type) throws IOException {
return Net.downloadObject(Net.buildUrl(API_HOST, "v2/search", Map.of(
"query", query,
"facets", "[[\"versions:" + version + "\"],[\"categories:fabric\"],[\"project_type:" + type + "\"]]",
"index", "relevance",
"offset", Integer.toString(page * ITEMS_PER_PAGE),
"limit", Integer.toString(ITEMS_PER_PAGE)
)), ModrinthSearchResult.class);
}
public static ModrinthProject getMod(String id) throws IOException {
return Net.downloadObject(API_HOST + "v2/project/" + id, ModrinthProject.class);
}
public static List<ModrinthVersion> getVersions(String mod) throws IOException {
List<ModrinthVersion> versions = Net.downloadObject(API_HOST + "v2/project/" + mod + "/version", modrinthVersionListType);
versions.sort(Comparator.comparing(version -> version.date_published));
return versions;
}
public static ModrinthVersion getVersion(String id) throws IOException {
return Net.downloadObject(API_HOST + "v2/version/" + id, ModrinthVersion.class);
}
public static ModrinthVersion getVersionByHash(String sha1) throws IOException {
return Net.downloadObject(API_HOST + "v2/version_file/" + sha1 + "?algorithm=sha1", ModrinthVersion.class);
}
}