Work around serverside curseforge errors by retrying later
This commit is contained in:
parent
f669060d1e
commit
e445547337
|
@ -14,6 +14,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -64,9 +65,11 @@ public class HttpUtils {
|
|||
}
|
||||
|
||||
public static class Request {
|
||||
private static final Pattern CURSEFORGE_API = Pattern.compile("(?:http(s)?://)?addons-ecs\\.forgesvc\\.net/api/+");
|
||||
private final String url;
|
||||
private final HttpRequest.Builder builder;
|
||||
private Method method;
|
||||
private int sent = 0;
|
||||
|
||||
public Request(Method method, String url) throws URISyntaxException {
|
||||
this.url = url.replace(" ", "%20");
|
||||
|
@ -127,6 +130,8 @@ public class HttpUtils {
|
|||
}
|
||||
|
||||
private <T> T _send(String accept, HttpResponse.BodyHandler<T> responseBodyHandler) throws IOException {
|
||||
sent++;
|
||||
if (sent > 3) throw new IOException("Attempted third redirect, stopping");
|
||||
builder.header("Accept", accept);
|
||||
if (method != null) builder.method(method.name(), HttpRequest.BodyPublishers.noBody());
|
||||
|
||||
|
@ -138,6 +143,7 @@ public class HttpUtils {
|
|||
}
|
||||
if (res.statusCode() == 200) return res.body();
|
||||
Optional<String> location = res.headers().firstValue("location");
|
||||
// Redirect
|
||||
if (location.isPresent() && (res.statusCode() == 302 || res.statusCode() == 307) && method == Method.GET) {
|
||||
try {
|
||||
return HttpUtils.get(location.get())._send(accept, responseBodyHandler);
|
||||
|
@ -145,6 +151,15 @@ public class HttpUtils {
|
|||
throw new IOException("Could not follow redirect", e);
|
||||
}
|
||||
}
|
||||
// CurseForge serverside error
|
||||
if (CURSEFORGE_API.matcher(url).matches() && res.statusCode() == 504) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException("Could not sleep before resending request" + e);
|
||||
}
|
||||
return _send(accept, responseBodyHandler);
|
||||
}
|
||||
throw new IOException("Unexpected return method: " + res.statusCode() + " (URL=" + url + ")");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue