From 91d3dab986c867fd12703f8c65318bdcaf44e66f Mon Sep 17 00:00:00 2001 From: carm Date: Sat, 22 Jan 2022 13:47:48 +0800 Subject: [PATCH] =?UTF-8?q?[v1.2.1]=20[U]=20=E4=BF=AE=E6=94=B9=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E5=85=B6=E6=9B=B4=E5=8A=A0=E6=98=93=E6=87=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../lib/githubreleases4j/GitHubHttpUtils.java | 67 ++++++++----------- .../lib/githubreleases4j/GithubAsset.java | 12 ++-- .../lib/githubreleases4j/GithubRelease.java | 4 +- src/test/java/GithubReleasesTest.java | 3 +- 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/pom.xml b/pom.xml index 5caa68c..473428a 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ cc.carm.lib githubreleases4j - 1.2.0 + 1.2.1 GithubReleases4J Github Releases for Java diff --git a/src/main/java/cc/carm/lib/githubreleases4j/GitHubHttpUtils.java b/src/main/java/cc/carm/lib/githubreleases4j/GitHubHttpUtils.java index 71f6f42..b856467 100644 --- a/src/main/java/cc/carm/lib/githubreleases4j/GitHubHttpUtils.java +++ b/src/main/java/cc/carm/lib/githubreleases4j/GitHubHttpUtils.java @@ -21,57 +21,44 @@ public class GitHubHttpUtils { // Should not be the instance or use by other codes. } - protected static JSONObject getObject(@NotNull String urlString, @Nullable String token) throws IOException { - URL url = new URL(urlString); - URLConnection conn = url.openConnection(); - conn.setRequestProperty("User-Agent", "GithubReleases4J"); - if (token != null) { - conn.setRequestProperty("Authorization", "token " + token); - } - conn.setRequestProperty("Accept", "application/vnd.github.v3+json"); - conn.connect(); - - InputStream in = conn.getInputStream(); - - BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); - String responseBody = reader.lines().collect(Collectors.joining(System.lineSeparator())); - reader.close(); - return new JSONObject(responseBody); - } - - protected static JSONArray getArray(@NotNull String urlString, String token) throws IOException { - URL url = new URL(urlString); + private static URLConnection createConnection(@NotNull String httpURL, @Nullable String token) throws IOException { + URL url = new URL(httpURL); URLConnection conn = url.openConnection(); conn.setRequestProperty("User-Agent", "GithubReleases4J"); - if (token != null) { - conn.setRequestProperty("Authorization", "token " + token); - } conn.setRequestProperty("Accept", "application/vnd.github.v3+json"); - conn.connect(); - InputStream in = conn.getInputStream(); + if (token != null) conn.setRequestProperty("Authorization", "token " + token); - BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); - String responseBody = reader.lines().collect(Collectors.joining(System.lineSeparator())); - reader.close(); - - return new JSONArray(responseBody); + return conn; } - protected static File download(@NotNull String urlString, @Nullable String token, + private static String getResponse(@NotNull String httpURL, @Nullable String token) throws IOException { + URLConnection connection = createConnection(httpURL, token); + InputStream in = connection.getInputStream(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); + String response = reader.lines().collect(Collectors.joining(System.lineSeparator())); + reader.close(); + + return response; + } + + + protected static JSONObject getObject(@NotNull String httpURL, @Nullable String token) throws IOException { + return new JSONObject(getResponse(httpURL, token)); + } + + protected static JSONArray getArray(@NotNull String httpURL, String token) throws IOException { + return new JSONArray(getResponse(httpURL, token)); + } + + protected static File download(@NotNull String httpURL, @Nullable String token, @NotNull String path, CopyOption... copyOptions) throws IOException { - URL url = new URL(urlString); Path target = Paths.get(path); - URLConnection conn = url.openConnection(); - conn.setRequestProperty("User-Agent", "GithubReleases4J"); - if (token != null) { - conn.setRequestProperty("Authorization", "token " + token); - } - conn.setRequestProperty("Accept", "application/vnd.github.v3+json"); - - InputStream in = conn.getInputStream(); + URLConnection connection = createConnection(httpURL, token); + InputStream in = connection.getInputStream(); Files.copy(in, target, copyOptions); in.close(); diff --git a/src/main/java/cc/carm/lib/githubreleases4j/GithubAsset.java b/src/main/java/cc/carm/lib/githubreleases4j/GithubAsset.java index 0f2b1e0..3a22a8f 100644 --- a/src/main/java/cc/carm/lib/githubreleases4j/GithubAsset.java +++ b/src/main/java/cc/carm/lib/githubreleases4j/GithubAsset.java @@ -84,23 +84,23 @@ public class GithubAsset { /** * Download this asset to current path with original name. * + * @param options Copy options e.g. {@link java.nio.file.StandardCopyOption} * @return Downloaded file. * @throws IOException Throws when has any io exception */ - public File download() throws IOException { - return download(null); + public File download(@NotNull CopyOption... options) throws IOException { + return GitHubHttpUtils.download(getBrowserDownloadURL(), getSource().getAuthToken(), getName(), options); } /** * Download this asset with provided path and name. * - * @param path target path, e.g. "cache/build.zip" - * @param options Copy options + * @param path target path with file suffix , e.g. "cache/build.zip" + * @param options Copy options e.g. {@link java.nio.file.StandardCopyOption} * @return Downloaded file. * @throws IOException Throws when has any io exception */ - public File download(@Nullable String path, CopyOption... options) throws IOException { - path = path == null ? getName() : path; + public File download(@NotNull String path, @NotNull CopyOption... options) throws IOException { return GitHubHttpUtils.download(getBrowserDownloadURL(), getSource().getAuthToken(), path, options); } diff --git a/src/main/java/cc/carm/lib/githubreleases4j/GithubRelease.java b/src/main/java/cc/carm/lib/githubreleases4j/GithubRelease.java index 46ee95f..4aaefc7 100644 --- a/src/main/java/cc/carm/lib/githubreleases4j/GithubRelease.java +++ b/src/main/java/cc/carm/lib/githubreleases4j/GithubRelease.java @@ -135,7 +135,7 @@ public class GithubRelease { * * @return The author user {@link GithubUser} */ - public GithubUser getAuthor() { + public @Nullable GithubUser getAuthor() { return Optional.ofNullable(getContents().getJSONObject("author")) .map(GithubUser::of) .orElse(null); @@ -146,7 +146,7 @@ public class GithubRelease { * * @return {@link GithubAsset} */ - public List getAssets() { + public @NotNull List getAssets() { JSONArray assetsArray = getContents().getJSONArray("assets"); if (assetsArray == null) return new ArrayList<>(); return IntStream.range(0, assetsArray.length()) diff --git a/src/test/java/GithubReleasesTest.java b/src/test/java/GithubReleasesTest.java index a34da76..2a9dc8b 100644 --- a/src/test/java/GithubReleasesTest.java +++ b/src/test/java/GithubReleasesTest.java @@ -6,6 +6,7 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.List; @@ -48,7 +49,7 @@ public class GithubReleasesTest { if (!DOWNLOAD) return; if (assets == null) return; try { - File file = assets.download(null); + File file = assets.download(); System.out.println("- at " + file.getAbsolutePath()); } catch (IOException e) { e.printStackTrace();