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
16 changes: 16 additions & 0 deletions dev/modrinth.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
###
GET https://api.modrinth.com/v2/project/cobblemon-fabric

###
GET https://api.modrinth.com/v2/project/cobblemon-fabric/version?loaders=fabric&game_versions=1.19.2

###
GET https://api.modrinth.com/v2/project/cobblemon-fabric/version

###
GET https://api.modrinth.com/v2/project/ids=[
"qVAfruPH",
"3yD7gvyr",
"kQU5UkIm",
"nvrqJg44"
]
8 changes: 7 additions & 1 deletion src/main/java/me/itzg/helpers/McImageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import me.itzg.helpers.find.FindCommand;
import me.itzg.helpers.forge.InstallForgeCommand;
import me.itzg.helpers.get.GetCommand;
import me.itzg.helpers.modrinth.InstallModrinthModpackCommand;
import me.itzg.helpers.modrinth.ModrinthCommand;
import me.itzg.helpers.mvn.MavenDownloadCommand;
import me.itzg.helpers.patch.PatchCommand;
Expand All @@ -28,7 +29,11 @@
import me.itzg.helpers.versions.JavaReleaseCommand;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.*;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.IVersionProvider;
import picocli.CommandLine.Option;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -50,6 +55,7 @@
InstallCurseForgeCommand.class,
InstallFabricLoaderCommand.class,
InstallForgeCommand.class,
InstallModrinthModpackCommand.class,
InterpolateCommand.class,
JavaReleaseCommand.class,
MavenDownloadCommand.class,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/me/itzg/helpers/forge/ForgeInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private static class VersionPair {
String forge;
}

/**
* @param forgeInstaller when non-null, specifies a provided installer to use
*/
public void install(String minecraftVersion, String forgeVersion,
Path outputDir, Path resultsFile,
boolean forceReinstall,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/itzg/helpers/http/SharedFetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class SharedFetch implements AutoCloseable {
private final HttpClient reactiveClient;

public SharedFetch(String forCommand, Options options) {
final String userAgent = String.format("%s/%s (cmd=%s)",
final String userAgent = String.format("%s/%s/%s (cmd=%s)",
"itzg",
"mc-image-helper",
McImageHelper.getVersion(),
forCommand != null ? forCommand : "unspecified"
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/me/itzg/helpers/http/UriBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public static UriBuilder withNoBaseUrl() {
public URI resolve(String path, Object... values) {
return Uris.populateToUri(baseUrl + path, values);
}

public URI resolve(String path, Uris.QueryParameters queryParameters, Object... values) {
return Uris.populateToUri(baseUrl + path, queryParameters, values);
}

}
102 changes: 72 additions & 30 deletions src/main/java/me/itzg/helpers/http/Uris.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,92 @@
package me.itzg.helpers.http;

import me.itzg.helpers.errors.GenericException;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public final class Uris {

private static final Pattern PLACEHOLDERS = Pattern.compile("\\{.*?}");
private static final Pattern URI_DETECT = Pattern.compile("^(http|https)://");
private static final Pattern PLACEHOLDERS = Pattern.compile("\\{.*?}");
private static final Pattern URI_DETECT = Pattern.compile("^(http|https)://");
public static final String ENC_UTF_8 = "utf-8";

public static String populate(String url, Object... values) {
if (values.length == 0) {
return url;
}

Matcher m = PLACEHOLDERS.matcher(url);
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find() && i < values.length) {
try {
m.appendReplacement(sb, URLEncoder.encode(values[i].toString(), ENC_UTF_8));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Internal error", e);
}
++i;
}
m.appendTail(sb);

public static String populate(String url, Object... values) {
if (values.length == 0) {
return url;
return sb.toString();
}

Matcher m = PLACEHOLDERS.matcher(url);
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find() && i < values.length) {
try {
m.appendReplacement(sb, URLEncoder.encode(values[i].toString(), "utf-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Internal error", e);
}
++i;
/**
* @param values replaces {@code {...}} placeholders in {@code url}
*/
public static URI populateToUri(String url, Object... values) {
return URI.create(populate(url, values));
}
m.appendTail(sb);

return sb.toString();
}
public static URI populateToUri(String url, Uris.QueryParameters queryParameters, Object... values) {
return URI.create(populate(url, values) + queryParameters.build());
}

/**
* @param values replaces {@code {...}} placeholders in {@code url}
*/
public static URI populateToUri(String url, Object... values) {
return URI.create(populate(url, values));
}
public static boolean isUri(String value) {
final Matcher m = URI_DETECT.matcher(value);
return m.lookingAt();
}

public static boolean isUri(String value) {
final Matcher m = URI_DETECT.matcher(value);
return m.lookingAt();
}
private Uris() {
}

public static class QueryParameters {
private final Map<String, String> parameters = new HashMap<>();

private Uris() {
}
public static QueryParameters queryParameters() {
return new QueryParameters();
}

/**
* @param value if null, the parameter will not be added otherwise it is URL encoded
*/
public QueryParameters add(String name, String value) {
if (value != null) {
parameters.put(name, value);
}
return this;
}

public String build() {
return !parameters.isEmpty() ?
parameters.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" +
URLEncoder.encode(entry.getValue(), ENC_UTF_8);
} catch (UnsupportedEncodingException e) {
throw new GenericException("Trying to encode URL query parameter", e);
}
})
.collect(Collectors.joining("&", "?", ""))
: "";
}
}
}
Loading