mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Remove Unirest and replace with Java's native HttpClient (#4068)
Co-authored-by: J3fftw <44972470+J3fftw1@users.noreply.github.com>
This commit is contained in:
parent
070d72f9ea
commit
b841828454
18
pom.xml
18
pom.xml
@ -204,10 +204,6 @@
|
|||||||
<pattern>io.papermc.lib</pattern>
|
<pattern>io.papermc.lib</pattern>
|
||||||
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.paperlib</shadedPattern>
|
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.paperlib</shadedPattern>
|
||||||
</relocation>
|
</relocation>
|
||||||
<relocation>
|
|
||||||
<pattern>kong.unirest</pattern>
|
|
||||||
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.unirest</shadedPattern>
|
|
||||||
</relocation>
|
|
||||||
<relocation>
|
<relocation>
|
||||||
<pattern>org.apache.commons.lang</pattern>
|
<pattern>org.apache.commons.lang</pattern>
|
||||||
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.commons.lang</shadedPattern>
|
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.commons.lang</shadedPattern>
|
||||||
@ -357,20 +353,6 @@
|
|||||||
<version>1.0.8</version>
|
<version>1.0.8</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.konghq</groupId>
|
|
||||||
<artifactId>unirest-java</artifactId>
|
|
||||||
<version>3.14.5</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<!-- No need to shade Gson, Spigot does that already -->
|
|
||||||
<groupId>com.google.code.gson</groupId>
|
|
||||||
<artifactId>gson</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Testing dependencies -->
|
<!-- Testing dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -4,11 +4,22 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.http.HttpResponse.BodyHandler;
|
||||||
|
import java.net.http.HttpResponse.BodySubscriber;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.time.Duration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import java.util.concurrent.Flow.Subscription;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
@ -16,15 +27,13 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import io.github.bakedlibs.dough.common.CommonPatterns;
|
import io.github.bakedlibs.dough.common.CommonPatterns;
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
||||||
|
|
||||||
import kong.unirest.GetRequest;
|
|
||||||
import kong.unirest.HttpResponse;
|
|
||||||
import kong.unirest.JsonNode;
|
|
||||||
import kong.unirest.Unirest;
|
|
||||||
import kong.unirest.UnirestException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Class represents a Metrics Service that sends data to https://bstats.org/
|
* This Class represents a Metrics Service that sends data to https://bstats.org/
|
||||||
* This data is used to analyse the usage of this {@link Plugin}.
|
* This data is used to analyse the usage of this {@link Plugin}.
|
||||||
@ -66,22 +75,12 @@ public class MetricsService {
|
|||||||
private final Slimefun plugin;
|
private final Slimefun plugin;
|
||||||
private final File parentFolder;
|
private final File parentFolder;
|
||||||
private final File metricsModuleFile;
|
private final File metricsModuleFile;
|
||||||
|
private final HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
private URLClassLoader moduleClassLoader;
|
private URLClassLoader moduleClassLoader;
|
||||||
private String metricVersion = null;
|
private String metricVersion = null;
|
||||||
private boolean hasDownloadedUpdate = false;
|
private boolean hasDownloadedUpdate = false;
|
||||||
|
|
||||||
static {
|
|
||||||
// @formatter:off (We want this to stay this nicely aligned :D )
|
|
||||||
Unirest.config()
|
|
||||||
.concurrency(2, 1)
|
|
||||||
.setDefaultHeader("User-Agent", "MetricsModule Auto-Updater")
|
|
||||||
.setDefaultHeader("Accept", "application/vnd.github.v3+json")
|
|
||||||
.enableCookieManagement(false)
|
|
||||||
.cookieSpec("ignoreCookies");
|
|
||||||
// @formatter:on
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constructs a new instance of our {@link MetricsService}.
|
* This constructs a new instance of our {@link MetricsService}.
|
||||||
*
|
*
|
||||||
@ -199,20 +198,16 @@ public class MetricsService {
|
|||||||
*/
|
*/
|
||||||
private int getLatestVersion() {
|
private int getLatestVersion() {
|
||||||
try {
|
try {
|
||||||
HttpResponse<JsonNode> response = Unirest.get(RELEASES_URL).asJson();
|
HttpResponse<String> response = client.send(buildBaseRequest(URI.create(RELEASES_URL)), HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if (!response.isSuccess()) {
|
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode node = response.getBody();
|
JsonElement element = JsonParser.parseString(response.body());
|
||||||
|
|
||||||
if (node == null) {
|
return element.getAsJsonObject().get("tag_name").getAsInt();
|
||||||
return -1;
|
} catch (IOException | InterruptedException | JsonParseException e) {
|
||||||
}
|
|
||||||
|
|
||||||
return node.getObject().getInt("tag_name");
|
|
||||||
} catch (UnirestException e) {
|
|
||||||
plugin.getLogger().log(Level.WARNING, "Failed to fetch latest builds for Metrics: {0}", e.getMessage());
|
plugin.getLogger().log(Level.WARNING, "Failed to fetch latest builds for Metrics: {0}", e.getMessage());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -235,19 +230,13 @@ public class MetricsService {
|
|||||||
Files.delete(file.toPath());
|
Files.delete(file.toPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
AtomicInteger lastPercentPosted = new AtomicInteger();
|
HttpResponse<Path> response = client.send(
|
||||||
GetRequest request = Unirest.get(DOWNLOAD_URL + "/" + version + "/" + JAR_NAME + ".jar");
|
buildBaseRequest(URI.create(DOWNLOAD_URL + "/" + version + "/" + JAR_NAME + ".jar")),
|
||||||
|
downloadMonitor(HttpResponse.BodyHandlers.ofFile(file.toPath()))
|
||||||
|
);
|
||||||
|
|
||||||
HttpResponse<File> response = request.downloadMonitor((b, fileName, bytesWritten, totalBytes) -> {
|
|
||||||
int percent = (int) (20 * (Math.round((((double) bytesWritten / totalBytes) * 100) / 20)));
|
|
||||||
|
|
||||||
if (percent != 0 && percent != lastPercentPosted.get()) {
|
if (response.statusCode() >= 200 && response.statusCode() < 300) {
|
||||||
plugin.getLogger().info("# Downloading... " + percent + "% " + "(" + bytesWritten + "/" + totalBytes + " bytes)");
|
|
||||||
lastPercentPosted.set(percent);
|
|
||||||
}
|
|
||||||
}).asFile(file.getPath());
|
|
||||||
|
|
||||||
if (response.isSuccess()) {
|
|
||||||
plugin.getLogger().log(Level.INFO, "Successfully downloaded {0} build: #{1}", new Object[] { JAR_NAME, version });
|
plugin.getLogger().log(Level.INFO, "Successfully downloaded {0} build: #{1}", new Object[] { JAR_NAME, version });
|
||||||
|
|
||||||
// Replace the metric file with the new one
|
// Replace the metric file with the new one
|
||||||
@ -258,7 +247,7 @@ public class MetricsService {
|
|||||||
hasDownloadedUpdate = true;
|
hasDownloadedUpdate = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (UnirestException e) {
|
} catch (InterruptedException | JsonParseException e) {
|
||||||
plugin.getLogger().log(Level.WARNING, "Failed to fetch the latest jar file from the builds page. Perhaps GitHub is down? Response: {0}", e.getMessage());
|
plugin.getLogger().log(Level.WARNING, "Failed to fetch the latest jar file from the builds page. Perhaps GitHub is down? Response: {0}", e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
plugin.getLogger().log(Level.WARNING, "Failed to replace the old metric file with the new one. Please do this manually! Error: {0}", e.getMessage());
|
plugin.getLogger().log(Level.WARNING, "Failed to replace the old metric file with the new one. Please do this manually! Error: {0}", e.getMessage());
|
||||||
@ -287,4 +276,58 @@ public class MetricsService {
|
|||||||
public boolean hasAutoUpdates() {
|
public boolean hasAutoUpdates() {
|
||||||
return Slimefun.instance().getConfig().getBoolean("metrics.auto-update");
|
return Slimefun.instance().getConfig().getBoolean("metrics.auto-update");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpRequest buildBaseRequest(@Nonnull URI uri) {
|
||||||
|
return HttpRequest.newBuilder()
|
||||||
|
.uri(uri)
|
||||||
|
.timeout(Duration.ofSeconds(5))
|
||||||
|
.header("User-Agent", "MetricsModule Auto-Updater")
|
||||||
|
.header("Accept", "application/vnd.github.v3+json")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> BodyHandler<T> downloadMonitor(BodyHandler<T> h) {
|
||||||
|
return info -> new BodySubscriber<T>() {
|
||||||
|
|
||||||
|
private BodySubscriber<T> delegateSubscriber = h.apply(info);
|
||||||
|
private int lastPercentPosted = 0;
|
||||||
|
private long bytesWritten = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(Subscription subscription) {
|
||||||
|
delegateSubscriber.onSubscribe(subscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(List<ByteBuffer> item) {
|
||||||
|
bytesWritten += item.stream().mapToLong(ByteBuffer::capacity).sum();
|
||||||
|
long totalBytes = info.headers().firstValue("Content-Length").map(Long::parseLong).orElse(-1L);
|
||||||
|
|
||||||
|
int percent = (int) (20 * (Math.round((((double) bytesWritten / totalBytes) * 100) / 20)));
|
||||||
|
|
||||||
|
if (percent != 0 && percent != lastPercentPosted) {
|
||||||
|
plugin.getLogger().info("# Downloading... " + percent + "% " + "(" + bytesWritten + "/" + totalBytes + " bytes)");
|
||||||
|
lastPercentPosted = percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
delegateSubscriber.onNext(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable throwable) {
|
||||||
|
delegateSubscriber.onError(throwable);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
delegateSubscriber.onComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletionStage<T> getBody() {
|
||||||
|
return delegateSubscriber.getBody();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,11 @@ import java.util.logging.Level;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import kong.unirest.JsonNode;
|
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
||||||
import kong.unirest.json.JSONArray;
|
|
||||||
import kong.unirest.json.JSONObject;
|
|
||||||
|
|
||||||
class ContributionsConnector extends GitHubConnector {
|
class ContributionsConnector extends GitHubConnector {
|
||||||
|
|
||||||
@ -90,11 +90,11 @@ class ContributionsConnector extends GitHubConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(@Nonnull JsonNode response) {
|
public void onSuccess(@Nonnull JsonElement response) {
|
||||||
finished = true;
|
finished = true;
|
||||||
|
|
||||||
if (response.isArray()) {
|
if (response.isJsonArray()) {
|
||||||
computeContributors(response.getArray());
|
computeContributors(response.getAsJsonArray());
|
||||||
} else {
|
} else {
|
||||||
Slimefun.logger().log(Level.WARNING, "Received an unusual answer from GitHub, possibly a timeout? ({0})", response);
|
Slimefun.logger().log(Level.WARNING, "Received an unusual answer from GitHub, possibly a timeout? ({0})", response);
|
||||||
}
|
}
|
||||||
@ -123,13 +123,13 @@ class ContributionsConnector extends GitHubConnector {
|
|||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void computeContributors(@Nonnull JSONArray array) {
|
private void computeContributors(@Nonnull JsonArray array) {
|
||||||
for (int i = 0; i < array.length(); i++) {
|
for (JsonElement element : array) {
|
||||||
JSONObject object = array.getJSONObject(i);
|
JsonObject object = element.getAsJsonObject();
|
||||||
|
|
||||||
String name = object.getString("login");
|
String name = object.get("login").getAsString();
|
||||||
int commits = object.getInt("contributions");
|
int commits = object.get("contributions").getAsInt();
|
||||||
String profile = object.getString("html_url");
|
String profile = object.get("html_url").getAsString();
|
||||||
|
|
||||||
if (!ignoredAccounts.contains(name)) {
|
if (!ignoredAccounts.contains(name)) {
|
||||||
String username = aliases.getOrDefault(name, name);
|
String username = aliases.getOrDefault(name, name);
|
||||||
|
@ -7,10 +7,10 @@ import java.util.Map;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import kong.unirest.JsonNode;
|
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
|
||||||
import kong.unirest.json.JSONObject;
|
|
||||||
|
|
||||||
class GitHubActivityConnector extends GitHubConnector {
|
class GitHubActivityConnector extends GitHubConnector {
|
||||||
|
|
||||||
@ -23,11 +23,11 @@ class GitHubActivityConnector extends GitHubConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(@Nonnull JsonNode response) {
|
public void onSuccess(@Nonnull JsonElement response) {
|
||||||
JSONObject object = response.getObject();
|
JsonObject object = response.getAsJsonObject();
|
||||||
int forks = object.getInt("forks");
|
int forks = object.get("forks").getAsInt();
|
||||||
int stars = object.getInt("stargazers_count");
|
int stars = object.get("stargazers_count").getAsInt();
|
||||||
LocalDateTime lastPush = NumberUtils.parseGitHubDate(object.getString("pushed_at"));
|
LocalDateTime lastPush = NumberUtils.parseGitHubDate(object.get("pushed_at").getAsString());
|
||||||
|
|
||||||
callback.accept(forks, stars, lastPush);
|
callback.accept(forks, stars, lastPush);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,12 @@ import java.io.FileInputStream;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -14,13 +19,11 @@ import java.util.logging.Level;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import kong.unirest.HttpResponse;
|
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
||||||
import kong.unirest.JsonNode;
|
|
||||||
import kong.unirest.Unirest;
|
|
||||||
import kong.unirest.UnirestException;
|
|
||||||
import kong.unirest.json.JSONException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link GitHubConnector} is used to connect to the GitHub API service.
|
* The {@link GitHubConnector} is used to connect to the GitHub API service.
|
||||||
@ -34,6 +37,7 @@ abstract class GitHubConnector {
|
|||||||
|
|
||||||
private static final String API_URL = "https://api.github.com/";
|
private static final String API_URL = "https://api.github.com/";
|
||||||
private static final String USER_AGENT = "Slimefun4 (https://github.com/Slimefun)";
|
private static final String USER_AGENT = "Slimefun4 (https://github.com/Slimefun)";
|
||||||
|
private static final HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
protected final GitHubService github;
|
protected final GitHubService github;
|
||||||
private final String url;
|
private final String url;
|
||||||
@ -83,7 +87,7 @@ abstract class GitHubConnector {
|
|||||||
* @param response
|
* @param response
|
||||||
* The response
|
* The response
|
||||||
*/
|
*/
|
||||||
public abstract void onSuccess(@Nonnull JsonNode response);
|
public abstract void onSuccess(@Nonnull JsonElement response);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called when the connection has failed.
|
* This method is called when the connection has failed.
|
||||||
@ -105,38 +109,44 @@ abstract class GitHubConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// @formatter:off
|
String params = getParameters().entrySet().stream()
|
||||||
HttpResponse<JsonNode> response = Unirest.get(url)
|
.map(p -> p.getKey() + "=" + p.getValue())
|
||||||
.queryString(getParameters())
|
.reduce((p1, p2) -> p1 + "&" + p2)
|
||||||
.header("User-Agent", USER_AGENT)
|
.map(s -> "?" + s)
|
||||||
.asJson();
|
.orElse("");
|
||||||
// @formatter:on
|
URI uri = new URI(url + params);
|
||||||
|
|
||||||
if (response.isSuccess()) {
|
HttpResponse<String> response = client.send(
|
||||||
onSuccess(response.getBody());
|
HttpRequest.newBuilder(uri).header("User-Agent", USER_AGENT).build(),
|
||||||
writeCacheFile(response.getBody());
|
HttpResponse.BodyHandlers.ofString()
|
||||||
|
);
|
||||||
|
JsonElement element = JsonParser.parseString(response.body());
|
||||||
|
|
||||||
|
if (response.statusCode() >= 200 && response.statusCode() < 300) {
|
||||||
|
onSuccess(element);
|
||||||
|
writeCacheFile(element);
|
||||||
} else {
|
} else {
|
||||||
if (github.isLoggingEnabled()) {
|
if (github.isLoggingEnabled()) {
|
||||||
Slimefun.logger().log(Level.WARNING, "Failed to fetch {0}: {1} - {2}", new Object[] { url, response.getStatus(), response.getBody() });
|
Slimefun.logger().log(Level.WARNING, "Failed to fetch {0}: {1} - {2}", new Object[] { url, response.statusCode(), element });
|
||||||
}
|
}
|
||||||
|
|
||||||
// It has the cached file, let's just read that then
|
// It has the cached file, let's just read that then
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
JsonNode cache = readCacheFile();
|
JsonElement cache = readCacheFile();
|
||||||
|
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
onSuccess(cache);
|
onSuccess(cache);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (UnirestException e) {
|
} catch (IOException | InterruptedException | JsonParseException | URISyntaxException e) {
|
||||||
if (github.isLoggingEnabled()) {
|
if (github.isLoggingEnabled()) {
|
||||||
Slimefun.logger().log(Level.WARNING, "Could not connect to GitHub in time.", e);
|
Slimefun.logger().log(Level.WARNING, "Could not connect to GitHub in time.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// It has the cached file, let's just read that then
|
// It has the cached file, let's just read that then
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
JsonNode cache = readCacheFile();
|
JsonElement cache = readCacheFile();
|
||||||
|
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
onSuccess(cache);
|
onSuccess(cache);
|
||||||
@ -150,16 +160,16 @@ abstract class GitHubConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private JsonNode readCacheFile() {
|
private JsonElement readCacheFile() {
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
|
||||||
return new JsonNode(reader.readLine());
|
return JsonParser.parseString(reader.readLine());
|
||||||
} catch (IOException | JSONException e) {
|
} catch (IOException | JsonParseException e) {
|
||||||
Slimefun.logger().log(Level.WARNING, "Failed to read Github cache file: {0} - {1}: {2}", new Object[] { file.getName(), e.getClass().getSimpleName(), e.getMessage() });
|
Slimefun.logger().log(Level.WARNING, "Failed to read Github cache file: {0} - {1}: {2}", new Object[] { file.getName(), e.getClass().getSimpleName(), e.getMessage() });
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeCacheFile(@Nonnull JsonNode node) {
|
private void writeCacheFile(@Nonnull JsonElement node) {
|
||||||
try (FileOutputStream output = new FileOutputStream(file)) {
|
try (FileOutputStream output = new FileOutputStream(file)) {
|
||||||
output.write(node.toString().getBytes(StandardCharsets.UTF_8));
|
output.write(node.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -7,11 +7,11 @@ import java.util.logging.Level;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import kong.unirest.JsonNode;
|
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
|
||||||
import kong.unirest.json.JSONArray;
|
|
||||||
import kong.unirest.json.JSONObject;
|
|
||||||
|
|
||||||
class GitHubIssuesConnector extends GitHubConnector {
|
class GitHubIssuesConnector extends GitHubConnector {
|
||||||
|
|
||||||
@ -24,15 +24,15 @@ class GitHubIssuesConnector extends GitHubConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(@Nonnull JsonNode response) {
|
public void onSuccess(@Nonnull JsonElement response) {
|
||||||
if (response.isArray()) {
|
if (response.isJsonArray()) {
|
||||||
JSONArray array = response.getArray();
|
JsonArray array = response.getAsJsonArray();
|
||||||
|
|
||||||
int issues = 0;
|
int issues = 0;
|
||||||
int pullRequests = 0;
|
int pullRequests = 0;
|
||||||
|
|
||||||
for (int i = 0; i < array.length(); i++) {
|
for (JsonElement element : array) {
|
||||||
JSONObject obj = array.getJSONObject(i);
|
JsonObject obj = element.getAsJsonObject();
|
||||||
|
|
||||||
if (obj.has("pull_request")) {
|
if (obj.has("pull_request")) {
|
||||||
pullRequests++;
|
pullRequests++;
|
||||||
|
Loading…
Reference in New Issue
Block a user