49 lines
2.1 KiB
Java
49 lines
2.1 KiB
Java
![]() |
package io.gitlab.jfronny.inceptum.util.api;
|
||
|
|
||
|
import com.google.gson.Gson;
|
||
|
import com.google.gson.reflect.TypeToken;
|
||
|
import io.gitlab.jfronny.inceptum.model.gitlab.GitlabProject;
|
||
|
import io.gitlab.jfronny.inceptum.model.gitlab.PackageFileInfo;
|
||
|
import io.gitlab.jfronny.inceptum.model.gitlab.PackageInfo;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.lang.reflect.Type;
|
||
|
import java.net.URL;
|
||
|
import java.util.List;
|
||
|
import java.util.function.Predicate;
|
||
|
|
||
|
public class GitlabApi {
|
||
|
private static final Gson GSON = new Gson();
|
||
|
private static final Type packageInfoListType = new TypeToken<List<PackageInfo>>() {}.getType();
|
||
|
private static final Type packageFileInfoListType = new TypeToken<List<PackageFileInfo>>() {}.getType();
|
||
|
private static final Type gitlabProjectType = new TypeToken<GitlabProject>() {}.getType();
|
||
|
|
||
|
public static GitlabProject getProject(Long projectId) throws IOException {
|
||
|
return downloadObject("https://gitlab.com/api/v4/projects/" + projectId, gitlabProjectType);
|
||
|
}
|
||
|
|
||
|
public static List<PackageInfo> getPackages(GitlabProject project) throws IOException {
|
||
|
return downloadObject("https://gitlab.com/api/v4/projects/" + project.id + "/packages", packageInfoListType);
|
||
|
}
|
||
|
|
||
|
public static PackageFileInfo getFile(GitlabProject project, PackageInfo packageInfo, Predicate<PackageFileInfo> isValid) throws IOException {
|
||
|
int page = 0;
|
||
|
while (true) {
|
||
|
List<PackageFileInfo> files = downloadObject("https://gitlab.com/api/v4/projects/" + project.id + "/packages/" + packageInfo.id + "/package_files?per_page=100&page=" + ++page, packageFileInfoListType);
|
||
|
if (files.isEmpty()) return null;
|
||
|
for (PackageFileInfo file : files) {
|
||
|
if (isValid.test(file))
|
||
|
return file;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static <T> T downloadObject(String source, Type type) throws IOException {
|
||
|
try (InputStream is = new URL(source).openStream(); InputStreamReader isr = new InputStreamReader(is)) {
|
||
|
return GSON.fromJson(isr, type);
|
||
|
}
|
||
|
}
|
||
|
}
|