Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T> extends FetchBuilderBase<ObjectListFetchBuilder<T>> {

Expand All @@ -20,7 +21,7 @@ public List<T> execute() throws IOException {
.block();
}

public Mono<List<T>> assemble() throws IOException {
public Mono<List<T>> assemble() {
return delegate.assembleToList();
}
}
26 changes: 25 additions & 1 deletion src/main/java/me/itzg/helpers/http/Uris.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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<String> 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()
Expand Down
26 changes: 9 additions & 17 deletions src/main/java/me/itzg/helpers/modrinth/ModrinthApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,23 @@ public Mono<Path> 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
*/
public Mono<List<Version>> 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<Version> getVersionFromId(String versionId) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -233,8 +233,11 @@ private Project getProject(String projectIdOrSlug) {
private List<Version> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/itzg/helpers/modrinth/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

/**
* <a href="https://docs.modrinth.com/api-spec/#tag/projects/operation/getProject">Spec</a>
* <a href="https://docs.modrinth.com/api-spec/#tag/project_model">Spec</a>
*/
@Data
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
Expand Down