Simplify GiteaMetadataDAO
This commit is contained in:
parent
9af8994c94
commit
8722fa6527
1 changed files with 2 additions and 68 deletions
|
|
@ -1,73 +1,7 @@
|
|||
package eu.m724.jarupdater.live;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import eu.m724.jarupdater.object.Version;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class GiteaMetadataDAO implements MetadataDAO {
|
||||
private final String url;
|
||||
private final String branch;
|
||||
|
||||
public class GiteaMetadataDAO extends HttpMetadataDAO {
|
||||
public GiteaMetadataDAO(String url, String branch) {
|
||||
this.url = url;
|
||||
this.branch = branch;
|
||||
super(url + "/raw/branch/" + branch + "/data/");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<String>> getChannels() {
|
||||
String url = getFileUrl("channels.txt");
|
||||
|
||||
return makeRequest(url).thenApply(response -> {
|
||||
if (response.statusCode() != 200)
|
||||
throw new CompletionException(new IOException("Server returned status code " + response.statusCode()));
|
||||
|
||||
return response.body().lines().collect(Collectors.toList());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Version> getMetadata(String channel, String versionLabel) {
|
||||
String url = getFileUrl(channel, versionLabel, "meta-v1.json");
|
||||
|
||||
// not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist
|
||||
|
||||
return makeRequest(url).thenApply(response -> {
|
||||
if (response.statusCode() != 200)
|
||||
throw new CompletionException(new IOException("Server returned status code " + response.statusCode()));
|
||||
// not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist
|
||||
|
||||
return new Gson().fromJson(response.body(), Version.class);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private CompletableFuture<HttpResponse<String>> makeRequest(String url) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("User-Agent", "ju/1") // jar updater v1
|
||||
.build();
|
||||
|
||||
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.proxy(ProxySelector.getDefault());
|
||||
|
||||
HttpClient httpClient = httpClientBuilder.build();
|
||||
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
private String getFileUrl(String... paths) {
|
||||
return url + "/raw/branch/" + branch + "/data/" + String.join("/", paths);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue