Skip to content

Commit 6fd53e3

Browse files
committed
don't depend on docs.javacord.org
add fallback javadoc.io docs to use if docs.javacord.org doesn't work use ci.javacord.org for getting the version
1 parent d7b7f93 commit 6fd53e3

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/main/java/org/javacord/bot/Constants.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ public final class Constants {
3535
/**
3636
* The API URL where to obtain the latest release version for Javacord.
3737
*/
38-
public static final String LATEST_VERSION_URL = "https://docs.javacord.org/rest/latest-version/release";
38+
public static final String LATEST_VERSION_URL = "https://ci.javacord.org/app/rest/builds/buildType:(id:Javacord_Release),status:SUCCESS?guest=1";
39+
40+
/**
41+
* The URL to the Javacord docs on docs.javacord.org, with a placeholder for api/core.
42+
*/
43+
public static final String JAVACORD_DOCS_URL_1 = "https://docs.javacord.org/%s/";
44+
45+
/**
46+
* The URL to the Javacord docs on javadoc.io, with a placeholder for api/core and another for the version.
47+
*/
48+
public static final String JAVACORD_DOCS_URL_2 = "https://javadoc.io/static/org.javacord/javacord-%s/%s/";
3949

4050
private Constants() { /* nope */ }
4151

src/main/java/org/javacord/bot/util/LatestVersionFinder.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import okhttp3.OkHttpClient;
66
import okhttp3.Request;
7+
import okhttp3.Request.Builder;
78
import okhttp3.ResponseBody;
89
import org.javacord.api.DiscordApi;
910
import org.javacord.api.util.logging.ExceptionLogger;
@@ -49,25 +50,30 @@ public CompletableFuture<String> findLatestVersion() {
4950
private String getAndUpdateVersionSync() {
5051
Request request = new Request.Builder()
5152
.url(Constants.LATEST_VERSION_URL)
53+
.header("Accept", "application/json")
5254
.build();
55+
5356
try (ResponseBody body = client.newCall(request).execute().body()) {
5457
if (body == null) {
5558
throw new RuntimeException("Error while requesting the latest version: No response body.");
5659
}
60+
5761
JsonNode response = mapper.readTree(body.charStream());
58-
// Response format is a JSON object {"version":"x.y.z"}
62+
63+
// Response format is a JSON object {"number":"x.y.z (#n)", ...}
5964
if (!response.isObject()) {
6065
throw new AssertionError("Latest Version API result differs from expectation");
6166
}
62-
String latestVersion = response.get("version").asText();
63-
if (latestVersion == null || latestVersion.isEmpty()) {
67+
68+
String latestVersion = response.get("number").asText();
69+
if (latestVersion == null || latestVersion.isEmpty() || !latestVersion.contains(" ")) {
6470
throw new AssertionError("Latest Version API result differs from expectation");
6571
}
66-
// Set cached version
67-
this.latestVersion = latestVersion;
72+
73+
this.latestVersion = latestVersion.split(" ", 2)[0]; //converts "x.y.z (#n)" to "x.y.z"
6874
// Eventually clean up update task
6975
return latestVersion;
70-
} catch (NullPointerException | IOException e) {
76+
} catch (IOException e) {
7177
throw new RuntimeException("Error while requesting the latest version", e);
7278
}
7379
}

src/main/java/org/javacord/bot/util/javadoc/parser/JavadocParser.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import okhttp3.Response;
88
import okhttp3.ResponseBody;
99
import org.javacord.api.DiscordApi;
10+
import org.javacord.bot.Constants;
11+
import org.javacord.bot.util.LatestVersionFinder;
1012

1113
import java.io.IOException;
1214
import java.util.HashSet;
@@ -43,7 +45,7 @@ public JavadocParser(DiscordApi api, String url) {
4345
* @return The latest JavaDoc link.
4446
*/
4547
public static CompletableFuture<String> getLatestJavaDocs(DiscordApi api) {
46-
return getJavadocUrl(api, "https://docs.javacord.org/api/");
48+
return getJavadocUrl(api, "api");
4749
}
4850

4951
/**
@@ -53,21 +55,26 @@ public static CompletableFuture<String> getLatestJavaDocs(DiscordApi api) {
5355
* @return The latest core JavaDoc link.
5456
*/
5557
public static CompletableFuture<String> getLatestCoreJavaDocs(DiscordApi api) {
56-
return getJavadocUrl(api, "https://docs.javacord.org/core/");
58+
return getJavadocUrl(api, "core");
5759
}
5860

59-
private static CompletableFuture<String> getJavadocUrl(DiscordApi api, String url) {
61+
private static CompletableFuture<String> getJavadocUrl(DiscordApi api, String type) {
6062
return CompletableFuture.supplyAsync(() -> {
6163
try {
6264
Request request = new Request.Builder()
63-
.url(url)
65+
.url(String.format(Constants.JAVACORD_DOCS_URL_1, type))
6466
.build();
6567

6668
try (Response response = client.newCall(request).execute()) {
6769
return response.request().url().toString();
6870
}
6971
} catch (Exception e) {
70-
throw new CompletionException(e);
72+
try {
73+
return String.format(Constants.JAVACORD_DOCS_URL_2, type, new LatestVersionFinder(api).findLatestVersion().join());
74+
} catch (Exception ignored) {
75+
//Just handle first exception, because it is more important.
76+
throw new CompletionException(e);
77+
}
7178
}
7279
}, api.getThreadPool().getExecutorService());
7380
}

0 commit comments

Comments
 (0)