diff --git a/src/main/java/me/itzg/helpers/http/ObjectListFetchBuilder.java b/src/main/java/me/itzg/helpers/http/ObjectListFetchBuilder.java index 44b131f0..33652d6f 100644 --- a/src/main/java/me/itzg/helpers/http/ObjectListFetchBuilder.java +++ b/src/main/java/me/itzg/helpers/http/ObjectListFetchBuilder.java @@ -1,9 +1,10 @@ package me.itzg.helpers.http; import com.fasterxml.jackson.databind.ObjectMapper; +import reactor.core.publisher.Mono; + import java.io.IOException; import java.util.List; -import reactor.core.publisher.Mono; public class ObjectListFetchBuilder extends FetchBuilderBase> { @@ -20,7 +21,7 @@ public List execute() throws IOException { .block(); } - public Mono> assemble() throws IOException { + public Mono> assemble() { return delegate.assembleToList(); } } diff --git a/src/main/java/me/itzg/helpers/http/Uris.java b/src/main/java/me/itzg/helpers/http/Uris.java index 8d057e99..14ed7eb0 100644 --- a/src/main/java/me/itzg/helpers/http/Uris.java +++ b/src/main/java/me/itzg/helpers/http/Uris.java @@ -5,7 +5,9 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -77,7 +79,7 @@ public static QueryParameters queryParameters() { } /** - * @param value if null, the parameter will not be added otherwise it is URL encoded + * @param value add query parameter if not null */ public QueryParameters add(String name, String value) { if (value != null) { @@ -86,6 +88,28 @@ public QueryParameters add(String name, String value) { return this; } + /** + * Adds a query parameter formatted into {@code ["str","str"]} + * @param values adds the query parameter is not null and not empty + */ + public QueryParameters addStringArray(String name, List values) { + if (values != null && !values.isEmpty()) { + parameters.put(name, + values.stream() + .map(s -> "\"" + s + "\"") + .collect(Collectors.joining(",", "[", "]")) + ); + } + return this; + } + + public QueryParameters addStringArray(String name, String value) { + if (value != null) { + return addStringArray(name, Collections.singletonList(value)); + } + return this; + } + public String build() { return !parameters.isEmpty() ? parameters.entrySet().stream() diff --git a/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java b/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java index f2011fd5..294ea0d2 100644 --- a/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java +++ b/src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java @@ -102,10 +102,6 @@ public Mono downloadMrPack(VersionFile versionFile) { ); } - public static String arrayOfQuoted(String value) { - return "[\"" + value + "\"]"; - } - /** * @param loader can be null for any * @param gameVersion can be null for any @@ -113,20 +109,16 @@ public static String arrayOfQuoted(String value) { public Mono> getVersionsForProject(String projectIdOrSlug, Loader loader, String gameVersion ) { - try { - return sharedFetch.fetch( - uriBuilder.resolve("/project/{id|slug}/version", - queryParameters() - .add("loader", loader != null ? arrayOfQuoted(loader.toString()) : null) - .add("game_versions", gameVersion != null ? arrayOfQuoted(gameVersion) : null), - projectIdOrSlug - ) + return sharedFetch.fetch( + uriBuilder.resolve("/project/{id|slug}/version", + queryParameters() + .addStringArray("loader", loader != null ? loader.toString() : null) + .addStringArray("game_versions", gameVersion), + projectIdOrSlug ) - .toObjectList(Version.class) - .assemble(); - } catch (IOException e) { - throw new RuntimeException("Getting versions for project " + projectIdOrSlug, e); - } + ) + .toObjectList(Version.class) + .assemble(); } public Mono getVersionFromId(String versionId) { diff --git a/src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java b/src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java index 1953f733..c4844f36 100644 --- a/src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java +++ b/src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java @@ -7,6 +7,7 @@ import me.itzg.helpers.files.Manifests; import me.itzg.helpers.http.FailedRequestException; import me.itzg.helpers.http.Uris; +import me.itzg.helpers.http.Uris.QueryParameters; import me.itzg.helpers.json.ObjectMappers; import me.itzg.helpers.modrinth.model.DependencyType; import me.itzg.helpers.modrinth.model.Project; @@ -35,7 +36,6 @@ import static me.itzg.helpers.McImageHelper.OPTION_SPLIT_COMMAS; import static me.itzg.helpers.http.Fetch.fetch; -import static me.itzg.helpers.modrinth.ModrinthApiClient.arrayOfQuoted; @Command(name = "modrinth", description = "Automates downloading of modrinth resources") @Slf4j @@ -233,8 +233,11 @@ private Project getProject(String projectIdOrSlug) { private List getVersionsForProject(String project) { try { return fetch(Uris.populateToUri( - baseUrl + "/project/{id|slug}/version?loaders={loader}&game_versions={gameVersion}", - project, arrayOfQuoted(loader.toString()), arrayOfQuoted(gameVersion) + baseUrl + "/project/{id|slug}/version", + QueryParameters.queryParameters() + .addStringArray("loaders", loader != null ? loader.toString() : null) + .addStringArray("game_versions", gameVersion), + project )) .userAgentCommand("modrinth") .toObjectList(Version.class) diff --git a/src/main/java/me/itzg/helpers/modrinth/model/Project.java b/src/main/java/me/itzg/helpers/modrinth/model/Project.java index 13ae84c2..b7d80c90 100644 --- a/src/main/java/me/itzg/helpers/modrinth/model/Project.java +++ b/src/main/java/me/itzg/helpers/modrinth/model/Project.java @@ -7,7 +7,7 @@ import java.util.List; /** - * Spec + * Spec */ @Data @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)