2021-11-02 16:44:26 +01:00
|
|
|
package io.gitlab.jfronny.inceptum.util.api;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthMod;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthSearchResult;
|
|
|
|
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthVersion;
|
|
|
|
import io.gitlab.jfronny.inceptum.util.Utils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-11-02 19:52:35 +01:00
|
|
|
//TODO search by categories: facets:[["versions:$ver","versions:$ver"],["categories:$cat","categories:$cat"]]
|
2021-11-02 16:44:26 +01:00
|
|
|
public static ModrinthSearchResult search(String query, int page, String version) throws IOException {
|
|
|
|
return Utils.downloadObject(Utils.buildUrl(API_HOST, "api/v1/mod", Map.of(
|
|
|
|
"query", query,
|
|
|
|
"filters", "categories=\"fabric\"",
|
2021-11-02 19:52:35 +01:00
|
|
|
"facets", "[[\"versions:" + version + "\"]]",
|
2021-11-02 16:44:26 +01:00
|
|
|
"index", "relevance",
|
|
|
|
"offset", Integer.toString(page * ITEMS_PER_PAGE),
|
|
|
|
"limit", Integer.toString(ITEMS_PER_PAGE)
|
|
|
|
)), ModrinthSearchResult.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModrinthMod getMod(String id) throws IOException {
|
|
|
|
return Utils.downloadObject(API_HOST + "api/v1/mod/" + id, ModrinthMod.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<ModrinthVersion> getVersions(String mod) throws IOException {
|
|
|
|
return Utils.downloadObject(API_HOST + "api/v1/mod/" + mod + "/version", modrinthVersionListType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ModrinthVersion getVersion(String id) throws IOException {
|
|
|
|
return Utils.downloadObject(API_HOST + "api/v1/version/" + id, ModrinthVersion.class);
|
|
|
|
}
|
2021-11-02 19:52:35 +01:00
|
|
|
|
|
|
|
public static ModrinthVersion getVersionByHash(String sha1) throws IOException {
|
|
|
|
return Utils.downloadObject(API_HOST + "api/v1/version_file/" + sha1 + "?algorithm=sha1", ModrinthVersion.class);
|
|
|
|
}
|
2021-11-02 16:44:26 +01:00
|
|
|
}
|