|
| 1 | +package core.version.hangar; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import core.annotation.FieldsAreNotNullByDefault; |
| 5 | +import core.annotation.MethodsReturnNotNullByDefault; |
| 6 | +import core.annotation.ParametersAreNotNullByDefault; |
| 7 | +import core.annotation.TypesAreNotNullByDefault; |
| 8 | +import core.version.Version; |
| 9 | +import core.version.VersionChecker; |
| 10 | +import lombok.Getter; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | +import org.jetbrains.annotations.Unmodifiable; |
| 13 | + |
| 14 | +import java.net.URI; |
| 15 | +import java.net.http.HttpClient; |
| 16 | +import java.net.http.HttpRequest; |
| 17 | +import java.net.http.HttpResponse; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +/** |
| 23 | + * The HangarVersionChecker class is an abstract class that provides methods |
| 24 | + * for retrieving and checking the latest supported version of a specific component in a hangar project. |
| 25 | + * |
| 26 | + * @param <V> the type parameter for the version |
| 27 | + */ |
| 28 | +@Getter |
| 29 | +@RequiredArgsConstructor |
| 30 | +@TypesAreNotNullByDefault |
| 31 | +@FieldsAreNotNullByDefault |
| 32 | +@MethodsReturnNotNullByDefault |
| 33 | +@ParametersAreNotNullByDefault |
| 34 | +public abstract class HangarVersionChecker<V extends Version> implements VersionChecker<HangarVersion, V> { |
| 35 | + private static final String API_URL = "https://hangar.papermc.io/api/v1"; |
| 36 | + private static final HttpClient client = HttpClient.newBuilder() |
| 37 | + .version(HttpClient.Version.HTTP_1_1) |
| 38 | + .build(); |
| 39 | + private static final Gson gson = new Gson(); |
| 40 | + |
| 41 | + /** |
| 42 | + * The slug of the project. |
| 43 | + */ |
| 44 | + private final String slug; |
| 45 | + |
| 46 | + @Override |
| 47 | + public CompletableFuture<V> retrieveLatestVersion() { |
| 48 | + return get("/projects/" + getSlug() + "/latestrelease") |
| 49 | + .thenApply(HttpResponse::body) |
| 50 | + .thenApply(this::parseVersion); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public CompletableFuture<@Unmodifiable Set<V>> retrieveVersions() { |
| 55 | + return retrieveHangarVersions().thenApply(versions -> versions.stream() |
| 56 | + .map(this::parseVersion) |
| 57 | + .collect(Collectors.toUnmodifiableSet())); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public CompletableFuture<V> retrieveLatestSupportedVersion() { |
| 62 | + return retrieveHangarVersions().thenApply(versions -> versions.stream() |
| 63 | + .filter(this::isSupported) |
| 64 | + .map(this::parseVersion) |
| 65 | + .max(Version::compareTo) |
| 66 | + .orElseThrow()); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public V parseVersion(HangarVersion version) { |
| 71 | + return parseVersion(version.name()); |
| 72 | + } |
| 73 | + |
| 74 | + public final CompletableFuture<Set<HangarVersion>> retrieveHangarVersions() { |
| 75 | + return get("/projects/" + getSlug() + "/versions") |
| 76 | + .thenApply(response -> gson.fromJson(response.body(), HangarVersions.class)) |
| 77 | + .thenApply(HangarVersions::result); |
| 78 | + } |
| 79 | + |
| 80 | + private CompletableFuture<HttpResponse<String>> get(String path) { |
| 81 | + return client.sendAsync(HttpRequest.newBuilder() |
| 82 | + .uri(URI.create(API_URL + path)) |
| 83 | + .build(), |
| 84 | + HttpResponse.BodyHandlers.ofString()); |
| 85 | + } |
| 86 | +} |
0 commit comments