mirror of
https://github.com/CarmJos/GithubReleases4J.git
synced 2024-09-19 21:55:45 +00:00
[v1.2.1] [U] 修改接口使用方式,使其更加易懂。
This commit is contained in:
parent
303dd703d6
commit
91d3dab986
2
pom.xml
2
pom.xml
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>githubreleases4j</artifactId>
|
<artifactId>githubreleases4j</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.1</version>
|
||||||
|
|
||||||
<name>GithubReleases4J</name>
|
<name>GithubReleases4J</name>
|
||||||
<description>Github Releases for Java</description>
|
<description>Github Releases for Java</description>
|
||||||
|
@ -21,57 +21,44 @@ public class GitHubHttpUtils {
|
|||||||
// Should not be the instance or use by other codes.
|
// Should not be the instance or use by other codes.
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static JSONObject getObject(@NotNull String urlString, @Nullable String token) throws IOException {
|
private static URLConnection createConnection(@NotNull String httpURL, @Nullable String token) throws IOException {
|
||||||
URL url = new URL(urlString);
|
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();
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
URLConnection conn = url.openConnection();
|
URLConnection conn = url.openConnection();
|
||||||
conn.setRequestProperty("User-Agent", "GithubReleases4J");
|
conn.setRequestProperty("User-Agent", "GithubReleases4J");
|
||||||
if (token != null) {
|
|
||||||
conn.setRequestProperty("Authorization", "token " + token);
|
|
||||||
}
|
|
||||||
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");
|
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
InputStream in = conn.getInputStream();
|
if (token != null) conn.setRequestProperty("Authorization", "token " + token);
|
||||||
|
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
||||||
String responseBody = reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
String response = reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
return new JSONArray(responseBody);
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static File download(@NotNull String urlString, @Nullable String token,
|
|
||||||
|
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 {
|
@NotNull String path, CopyOption... copyOptions) throws IOException {
|
||||||
URL url = new URL(urlString);
|
|
||||||
Path target = Paths.get(path);
|
Path target = Paths.get(path);
|
||||||
|
|
||||||
URLConnection conn = url.openConnection();
|
URLConnection connection = createConnection(httpURL, token);
|
||||||
conn.setRequestProperty("User-Agent", "GithubReleases4J");
|
InputStream in = connection.getInputStream();
|
||||||
if (token != null) {
|
|
||||||
conn.setRequestProperty("Authorization", "token " + token);
|
|
||||||
}
|
|
||||||
conn.setRequestProperty("Accept", "application/vnd.github.v3+json");
|
|
||||||
|
|
||||||
InputStream in = conn.getInputStream();
|
|
||||||
|
|
||||||
Files.copy(in, target, copyOptions);
|
Files.copy(in, target, copyOptions);
|
||||||
in.close();
|
in.close();
|
||||||
|
@ -84,23 +84,23 @@ public class GithubAsset {
|
|||||||
/**
|
/**
|
||||||
* Download this asset to current path with original name.
|
* Download this asset to current path with original name.
|
||||||
*
|
*
|
||||||
|
* @param options Copy options e.g. {@link java.nio.file.StandardCopyOption}
|
||||||
* @return Downloaded file.
|
* @return Downloaded file.
|
||||||
* @throws IOException Throws when has any io exception
|
* @throws IOException Throws when has any io exception
|
||||||
*/
|
*/
|
||||||
public File download() throws IOException {
|
public File download(@NotNull CopyOption... options) throws IOException {
|
||||||
return download(null);
|
return GitHubHttpUtils.download(getBrowserDownloadURL(), getSource().getAuthToken(), getName(), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download this asset with provided path and name.
|
* Download this asset with provided path and name.
|
||||||
*
|
*
|
||||||
* @param path target path, e.g. "cache/build.zip"
|
* @param path target path with file suffix , e.g. "cache/build.zip"
|
||||||
* @param options Copy options
|
* @param options Copy options e.g. {@link java.nio.file.StandardCopyOption}
|
||||||
* @return Downloaded file.
|
* @return Downloaded file.
|
||||||
* @throws IOException Throws when has any io exception
|
* @throws IOException Throws when has any io exception
|
||||||
*/
|
*/
|
||||||
public File download(@Nullable String path, CopyOption... options) throws IOException {
|
public File download(@NotNull String path, @NotNull CopyOption... options) throws IOException {
|
||||||
path = path == null ? getName() : path;
|
|
||||||
return GitHubHttpUtils.download(getBrowserDownloadURL(), getSource().getAuthToken(), path, options);
|
return GitHubHttpUtils.download(getBrowserDownloadURL(), getSource().getAuthToken(), path, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ public class GithubRelease {
|
|||||||
*
|
*
|
||||||
* @return The author user {@link GithubUser}
|
* @return The author user {@link GithubUser}
|
||||||
*/
|
*/
|
||||||
public GithubUser getAuthor() {
|
public @Nullable GithubUser getAuthor() {
|
||||||
return Optional.ofNullable(getContents().getJSONObject("author"))
|
return Optional.ofNullable(getContents().getJSONObject("author"))
|
||||||
.map(GithubUser::of)
|
.map(GithubUser::of)
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
@ -146,7 +146,7 @@ public class GithubRelease {
|
|||||||
*
|
*
|
||||||
* @return {@link GithubAsset}
|
* @return {@link GithubAsset}
|
||||||
*/
|
*/
|
||||||
public List<GithubAsset> getAssets() {
|
public @NotNull List<GithubAsset> getAssets() {
|
||||||
JSONArray assetsArray = getContents().getJSONArray("assets");
|
JSONArray assetsArray = getContents().getJSONArray("assets");
|
||||||
if (assetsArray == null) return new ArrayList<>();
|
if (assetsArray == null) return new ArrayList<>();
|
||||||
return IntStream.range(0, assetsArray.length())
|
return IntStream.range(0, assetsArray.length())
|
||||||
|
@ -6,6 +6,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -48,7 +49,7 @@ public class GithubReleasesTest {
|
|||||||
if (!DOWNLOAD) return;
|
if (!DOWNLOAD) return;
|
||||||
if (assets == null) return;
|
if (assets == null) return;
|
||||||
try {
|
try {
|
||||||
File file = assets.download(null);
|
File file = assets.download();
|
||||||
System.out.println("- at " + file.getAbsolutePath());
|
System.out.println("- at " + file.getAbsolutePath());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
Reference in New Issue
Block a user