Skip to content

Commit

Permalink
don't depend on docs.javacord.org
Browse files Browse the repository at this point in the history
add fallback javadoc.io docs to use if docs.javacord.org doesn't work
use ci.javacord.org for getting the version
  • Loading branch information
Joshix-1 committed Aug 23, 2020
1 parent d7b7f93 commit 996641a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/javacord/bot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public final class Constants {
/**
* The API URL where to obtain the latest release version for Javacord.
*/
public static final String LATEST_VERSION_URL = "https://docs.javacord.org/rest/latest-version/release";
public static final String LATEST_VERSION_URL = "https://ci.javacord.org/app/rest/builds/buildType:(id:Javacord_Release),status:SUCCESS?guest=1";

/**
* The URL to the Javacord docs on docs.javacord.org, with a placeholder for api/core.
*/
public static final String JAVACORD_DOCS_URL_1 = "https://docs.javacord.org/%s/";

/**
* The URL to the Javacord docs on javadoc.io, with a placeholder for api/core and another for the version.
*/
public static final String JAVACORD_DOCS_URL_2 = "https://javadoc.io/static/org.javacord/javacord-%s/%s/";

private Constants() { /* nope */ }

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/javacord/bot/util/LatestVersionFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.ResponseBody;
import org.javacord.api.DiscordApi;
import org.javacord.api.util.logging.ExceptionLogger;
Expand Down Expand Up @@ -49,25 +50,30 @@ public CompletableFuture<String> findLatestVersion() {
private String getAndUpdateVersionSync() {
Request request = new Request.Builder()
.url(Constants.LATEST_VERSION_URL)
.header("Accept", "application/json")
.build();

try (ResponseBody body = client.newCall(request).execute().body()) {
if (body == null) {
throw new RuntimeException("Error while requesting the latest version: No response body.");
}

JsonNode response = mapper.readTree(body.charStream());
// Response format is a JSON object {"version":"x.y.z"}

// Response format is a JSON object {"number":"x.y.z (#n)", ...}
if (!response.isObject()) {
throw new AssertionError("Latest Version API result differs from expectation");
}
String latestVersion = response.get("version").asText();
if (latestVersion == null || latestVersion.isEmpty()) {

String latestVersion = response.get("number").asText();
if (latestVersion == null || latestVersion.isEmpty() || !latestVersion.contains(" ")) {
throw new AssertionError("Latest Version API result differs from expectation");
}
// Set cached version
this.latestVersion = latestVersion;

this.latestVersion = latestVersion.split(" ", 2)[0]; //converts "x.y.z (#n)" to "x.y.z"
// Eventually clean up update task
return latestVersion;
} catch (NullPointerException | IOException e) {
} catch (IOException e) {
throw new RuntimeException("Error while requesting the latest version", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.javacord.api.DiscordApi;
import org.javacord.bot.Constants;
import org.javacord.bot.util.LatestVersionFinder;

import java.io.IOException;
import java.util.HashSet;
Expand Down Expand Up @@ -43,7 +45,7 @@ public JavadocParser(DiscordApi api, String url) {
* @return The latest JavaDoc link.
*/
public static CompletableFuture<String> getLatestJavaDocs(DiscordApi api) {
return getJavadocUrl(api, "https://docs.javacord.org/api/");
return getJavadocUrl(api, "api");
}

/**
Expand All @@ -53,21 +55,27 @@ public static CompletableFuture<String> getLatestJavaDocs(DiscordApi api) {
* @return The latest core JavaDoc link.
*/
public static CompletableFuture<String> getLatestCoreJavaDocs(DiscordApi api) {
return getJavadocUrl(api, "https://docs.javacord.org/core/");
return getJavadocUrl(api, "core");
}

private static CompletableFuture<String> getJavadocUrl(DiscordApi api, String url) {
private static CompletableFuture<String> getJavadocUrl(DiscordApi api, String type) {
return CompletableFuture.supplyAsync(() -> {
try {
Request request = new Request.Builder()
.url(url)
.url(String.format(Constants.JAVACORD_DOCS_URL_1, type))
.build();

try (Response response = client.newCall(request).execute()) {
return response.request().url().toString();
}
} catch (Exception e) {
throw new CompletionException(e);
try {
return String.format(Constants.JAVACORD_DOCS_URL_2, type,
new LatestVersionFinder(api).findLatestVersion().join());
} catch (Exception ignored) {
//Just handle first exception, because it is more important.
throw new CompletionException(e);
}
}
}, api.getThreadPool().getExecutorService());
}
Expand Down

0 comments on commit 996641a

Please sign in to comment.