Cleanup webpaste code (#2411)

This commit is contained in:
nicegamer7 2020-10-05 01:25:15 -04:00 committed by GitHub
parent d35363b10c
commit 75855826e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 8 deletions

View File

@ -16,7 +16,9 @@ class BitlyURLShortener extends URLShortener {
BitlyURLShortener() {
super(BITLY_POST_REQUEST, ACCESS_TOKEN);
if (ACCESS_TOKEN.endsWith("access-token")) throw new UnsupportedOperationException();
if (ACCESS_TOKEN.endsWith("access-token")) {
throw new UnsupportedOperationException();
}
}
/**

View File

@ -20,7 +20,9 @@ class GitHubPasteService extends PasteService {
GitHubPasteService(boolean isPrivate) {
super(GITHUB_POST_REQUEST, ACCESS_TOKEN);
this.isPrivate = isPrivate;
if (ACCESS_TOKEN.endsWith("access-token")) throw new UnsupportedOperationException();
if (ACCESS_TOKEN.endsWith("access-token")) {
throw new UnsupportedOperationException();
}
}
/**

View File

@ -14,7 +14,7 @@ class HastebinPasteService extends PasteService {
private static final String HASTEBIN_POST_REQUEST = "https://hastebin.com/documents";
HastebinPasteService() {
super(HASTEBIN_POST_REQUEST, null);
super(HASTEBIN_POST_REQUEST);
}
/**

View File

@ -29,6 +29,10 @@ abstract class HttpAPIClient {
URLENCODED
}
HttpAPIClient(String url) {
this(url, null);
}
HttpAPIClient(String url, String accessToken) {
this.url = url;
this.accessToken = accessToken;
@ -48,7 +52,7 @@ abstract class HttpAPIClient {
case URLENCODED:
return "application/x-www-form-urlencoded; charset=utf-8";
default:
throw new IllegalStateException("Unexpected value: " + type);
throw new IllegalArgumentException("Unexpected value: " + type);
}
}
@ -91,7 +95,9 @@ abstract class HttpAPIClient {
// this isn't required, but is technically correct
conn.addRequestProperty("Content-Type", getContentHeader(type));
// only some API requests require an access token
if (this.accessToken != null) conn.addRequestProperty("Authorization", this.accessToken);
if (this.accessToken != null) {
conn.addRequestProperty("Authorization", this.accessToken);
}
wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8.newEncoder());
wr.write(payload);
@ -102,7 +108,10 @@ abstract class HttpAPIClient {
// this has to be initialized AFTER the data has been flushed!
rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
while ((line = rd.readLine()) != null) responseString.append(line);
while ((line = rd.readLine()) != null) {
responseString.append(line);
}
return responseString.toString();
} finally {
if (wr != null) {

View File

@ -17,7 +17,7 @@ class PasteGGPasteService extends PasteService {
private static final String PASTEGG_POST_REQUEST = "https://api.paste.gg/v1/pastes";
PasteGGPasteService(boolean isPrivate) {
super(PASTEGG_POST_REQUEST, null);
super(PASTEGG_POST_REQUEST);
this.isPrivate = isPrivate;
}

View File

@ -14,6 +14,10 @@ import java.util.Map;
* instance is submitting; an example of this is the PastebinPasteService class.
*/
public abstract class PasteService extends HttpAPIClient {
PasteService(String url) {
super(url);
}
PasteService(String url, String accessToken) {
super(url, accessToken);
}

View File

@ -13,7 +13,7 @@ class PastebinPasteService extends PasteService {
private static final String PASTEBIN_POST_REQUEST = "https://pastebin.com/api/api_post.php";
PastebinPasteService(boolean isPrivate) {
super(PASTEBIN_POST_REQUEST, null);
super(PASTEBIN_POST_REQUEST);
this.isPrivate = isPrivate;
}

View File

@ -10,6 +10,10 @@ package com.onarandombox.MultiverseCore.utils.webpaste;
* An example of this, is the BitlyURLShortener.
*/
public abstract class URLShortener extends HttpAPIClient {
URLShortener(String url) {
super(url);
}
URLShortener(String url, String accessToken) {
super(url, accessToken);
}