Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Added routes + input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminUrquhart authored Oct 24, 2018
1 parent 84d3542 commit 215cd4d
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 20 deletions.
14 changes: 9 additions & 5 deletions src/main/java/net/explodingbush/ksoftapi/KSoftAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import net.explodingbush.ksoftapi.entities.WikihowAction;
import net.explodingbush.ksoftapi.enums.ImageTag;
import net.explodingbush.ksoftapi.enums.ImageType;
import net.explodingbush.ksoftapi.enums.Routes;
import net.explodingbush.ksoftapi.enums.Routes;
import net.explodingbush.ksoftapi.utils.Checks;

public class KSoftAPI {

private final String token;
private String request;

public KSoftAPI(String token) {
Checks.notNull(token, "token");
this.token = token;
}

Expand All @@ -24,18 +26,19 @@ public KSoftAPI(String token) {
* @return {@link net.explodingbush.ksoftapi.entities.RedditAction RedditAction}s
*/
public RedditAction getRedditImage(ImageType type) {
Checks.notNull(type, "type");
switch (type) {
case RANDOM_MEME:
this.request = REDDIT + "random-meme";
this.request = Routes.REDDIT + "random-meme";
break;
case RANDOW_AWW:
this.request = REDDIT + "random-aww";
this.request = Routes.REDDIT + "random-aww";
break;
case RANDOM_NSFW:
this.request = REDDIT + "random-nsfw";
this.request = Routes.REDDIT + "random-nsfw";
break;
case RANDOM_REDDIT:
this.request = REDDIT + "rand-reddit";
this.request = Routes.REDDIT + "rand-reddit";
}
return new RedditAction(token, type, request);
}
Expand All @@ -47,6 +50,7 @@ public RedditAction getRedditImage(ImageType type) {
* @return {@link net.explodingbush.ksoftapi.entities.TaggedImageAction TaggedImageAction}
*/
public TaggedImageAction getTaggedImage(ImageTag tag) {
Checks.notNull(tag, "tag");
return new TaggedImageAction(token, tag);
}
/**
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/net/explodingbush/ksoftapi/entities/BanAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import net.explodingbush.ksoftapi.KSoftAction;
import net.explodingbush.ksoftapi.entities.impl.AddBanImpl;
import net.explodingbush.ksoftapi.entities.impl.BanImpl;
import net.explodingbush.ksoftapi.enums.Routes;
import net.explodingbush.ksoftapi.exceptions.AddBanException;
import net.explodingbush.ksoftapi.exceptions.LoginException;
import net.explodingbush.ksoftapi.exceptions.MissingArgumentException;
import net.explodingbush.ksoftapi.utils.Checks;
import net.explodingbush.ksoftapi.utils.JSONBuilder;
import org.json.JSONObject;

Expand Down Expand Up @@ -35,6 +37,7 @@ public BanAction(String token) {
tokenValue = token;
}
public BanAction(boolean isAddingBan, JSONObject banJson) {
Checks.notNull(banJson, "banJson");
this.isAddingBan = isAddingBan;
this.banJson = banJson;
}
Expand All @@ -46,6 +49,7 @@ public BanAction(boolean isAddingBan, JSONObject banJson) {
* @return BanAction instance. Useful for chaining.
*/
public BanAction getBanList(int results) {
Checks.positive(results, "results");
this.results = results;
return this;
}
Expand All @@ -56,6 +60,7 @@ public BanAction getBanList(int results) {
* @return BanAction instance. Useful for chaining.
*/
public BanAction setUserId(String userId) {
Checks.notNull(userId, "userId");
this.banId = userId;
return this;
}
Expand All @@ -79,7 +84,7 @@ public Ban execute() throws LoginException {
JSONObject json;
if(isAddingBan) {
json = banJson;
JSONObject newJson = new JSONBuilder().addBanKsoft(banJson, "https://api.ksoft.si/bans/add", tokenValue);
JSONObject newJson = new JSONBuilder().addBanKsoft(banJson, Routes.BAN_ADD, tokenValue);
if(newJson.has("error")) {
throw new AddBanException(newJson.getString("message"));
}
Expand All @@ -88,9 +93,9 @@ public Ban execute() throws LoginException {
throw new MissingArgumentException("Missing action value. Could not be parsed");
}
if (results == 0) {
json = new JSONBuilder().requestKsoft("https://api.ksoft.si/bans/info?user=" + String.valueOf(banId), tokenValue);
json = new JSONBuilder().requestKsoft(Routes.BAN_INFO + banId, tokenValue);
} else {
json = new JSONBuilder().requestKsoft("https://api.ksoft.si/bans/list?per_page=" + String.valueOf(results), tokenValue);
json = new JSONBuilder().requestKsoft(Routes.BAN_LIST + "" + results, tokenValue);
}
}
if (tokenValue.isEmpty() || !json.isNull("detail") && json.getString("detail").equalsIgnoreCase("Invalid token.")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.explodingbush.ksoftapi.exceptions.LoginException;
import net.explodingbush.ksoftapi.exceptions.MissingArgumentException;
import net.explodingbush.ksoftapi.exceptions.NotFoundException;
import net.explodingbush.ksoftapi.utils.Checks;
import net.explodingbush.ksoftapi.utils.JSONBuilder;
import okhttp3.Response;
import org.json.JSONObject;
Expand All @@ -21,6 +22,9 @@ public class RedditAction implements KSoftAction<Reddit> {
private Logger logger = LoggerFactory.getLogger(RedditAction.class);

public RedditAction(String token, ImageType type, String request) {
Checks.notNull(token, "token");
Checks.notNull(type, "type");
Checks.notNull(request, "request");
this.token = token;
this.type = type;
this.request = request;
Expand All @@ -33,6 +37,7 @@ public RedditAction(String token, ImageType type, String request) {
* @return RedditAction instance. Useful for chaining.
*/
public RedditAction setSubreddit(String subreddit) {
Checks.notNull(subreddit, "subreddit");
this.subreddit = subreddit;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.explodingbush.ksoftapi.entities;

import net.explodingbush.ksoftapi.enums.ImageTag;
import net.explodingbush.ksoftapi.enums.Routes;
import net.explodingbush.ksoftapi.KSoftAction;
import net.explodingbush.ksoftapi.entities.impl.TaggedImageImpl;
import net.explodingbush.ksoftapi.exceptions.LoginException;
import net.explodingbush.ksoftapi.utils.Checks;
import net.explodingbush.ksoftapi.utils.JSONBuilder;
import org.json.JSONObject;

Expand All @@ -15,6 +17,8 @@ public class TaggedImageAction implements KSoftAction<TaggedImage> {
private boolean allowNsfw = false;

public TaggedImageAction(String token, ImageTag tag) {
Checks.notNull(token, "token");
Checks.notNull(tag, "tag");
this.token = token;
this.tag = tag;
}
Expand All @@ -39,7 +43,7 @@ public TaggedImageAction allowNsfw(boolean nsfw) {
* If the token is not provided or incorrect.
*/
public TaggedImage execute() {
json = new JSONBuilder().requestKsoft("https://api.ksoft.si/images/random-image?tag=" + tag.name().toLowerCase() + "&nsfw=" + String.valueOf(allowNsfw), token);
json = new JSONBuilder().requestKsoft(String.format(Routes.IMAGE.toString(), tag.name().toLowerCase(), String.valueOf(allowNsfw)), token);
if (token.isEmpty() || !json.isNull("detail") && json.getString("detail").equalsIgnoreCase("Invalid token.")) {
throw new LoginException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import net.explodingbush.ksoftapi.KSoftAction;
import net.explodingbush.ksoftapi.entities.impl.WikihowImpl;
import net.explodingbush.ksoftapi.enums.Routes;
import net.explodingbush.ksoftapi.exceptions.LoginException;
import net.explodingbush.ksoftapi.utils.Checks;
import net.explodingbush.ksoftapi.utils.JSONBuilder;
import org.json.JSONObject;

Expand All @@ -13,6 +15,7 @@ public class WikihowAction implements KSoftAction<Wikihow> {
private boolean allowNsfw;

public WikihowAction(String token) {
Checks.notNull(token, "token");
this.token = token;
}

Expand All @@ -36,7 +39,7 @@ public WikihowAction allowNsfw(boolean nsfw) {
* If the token is not provided or incorrect.
*/
public Wikihow execute() {
json = new JSONBuilder().requestKsoft("https://api.ksoft.si/images/random-wikihow?nsfw=" + String.valueOf(allowNsfw), token);
json = new JSONBuilder().requestKsoft(String.format(Routes.WIKIHOW.toString(), String.valueOf(allowNsfw)), token);
if (token.isEmpty() || !json.isNull("detail") && json.getString("detail").equalsIgnoreCase("Invalid token.")) {
throw new LoginException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.explodingbush.ksoftapi.entities.AddBan;
import net.explodingbush.ksoftapi.entities.BanAction;
import net.explodingbush.ksoftapi.exceptions.MissingArgumentException;
import net.explodingbush.ksoftapi.utils.Checks;

import org.json.JSONObject;

public class AddBanImpl implements AddBan {
Expand All @@ -11,36 +13,42 @@ public class AddBanImpl implements AddBan {

@Override
public AddBan setUserId(String user) {
Checks.notNull(user, "user");
json.put("user", user);
return this;
}

@Override
public AddBan setModeratorId(String moderatorId) {
Checks.notNull(moderatorId, "moderatorId");
json.put("mod", moderatorId);
return this;
}

@Override
public AddBan setUsername(String userName) {
Checks.notNull(userName, "userName");
json.put("user_name", userName);
return this;
}

@Override
public AddBan setDiscriminator(String discriminator) {
Checks.notNull(discriminator, "discriminator");
json.put("user_discriminator", discriminator);
return this;
}

@Override
public AddBan setReason(String reason) {
Checks.notNull(reason, "reason");
json.put("reason", reason);
return this;
}

@Override
public AddBan setProof(String proof) {
Checks.notNull(proof, "proof");
json.put("proof", proof);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.explodingbush.ksoftapi.entities.impl;

import net.explodingbush.ksoftapi.entities.Reddit;
import net.explodingbush.ksoftapi.utils.Checks;

import org.json.JSONObject;

import java.time.Instant;
Expand All @@ -11,6 +13,7 @@ public class RedditImpl implements Reddit {

private final JSONObject json;
public RedditImpl(JSONObject json) {
Checks.notNull(json, "json");
this.json = json;
}

Expand Down
26 changes: 16 additions & 10 deletions src/main/java/net/explodingbush/ksoftapi/enums/Routes.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package net.explodingbush.ksoftapi.enums;

public enum Routes {
REDDIT("https://api.ksoft.si/meme/");

private final String route;

private Routes(String route){
this.route = route;
}
public String toString(){
return this.route;
}

BAN_ADD("https://api.ksoft.si/bans/add"),
BAN_INFO("https://api.ksoft.si/bans/info?user="),
BAN_LIST("https://api.ksoft.si/bans/list?per_page="),
IMAGE("https://api.ksoft.si/images/random-image?tag=%s&nsfw=%s"),
WIKIHOW("https://api.ksoft.si/images/random-wikihow?nsfw=%s"),
REDDIT("https://api.ksoft.si/meme/");

private final String route;

private Routes(String route){
this.route = route;
}
public String toString(){
return this.route;
}
}
15 changes: 15 additions & 0 deletions src/main/java/net/explodingbush/ksoftapi/utils/Checks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.explodingbush.ksoftapi.utils;

public class Checks {

public static void notNull(Object object, String name) {
if(object == null) {
throw new IllegalArgumentException(name + " cannot be null");
}
}
public static void positive(int num, String name) {
if(num < 1) {
throw new IllegalArgumentException(name + " cannot be smaller than 1");
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/net/explodingbush/ksoftapi/utils/JSONBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import okhttp3.Response;
import org.json.JSONObject;

import net.explodingbush.ksoftapi.enums.Routes;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -18,6 +20,7 @@ public class JSONBuilder {


public JSONObject request(String url) {
Checks.notNull(url, "url");
try {
String source = client.newCall(new Request.Builder().url(url).build()).execute().body().string();
return new JSONObject(source);
Expand All @@ -27,6 +30,7 @@ public JSONObject request(String url) {
return null;
}
public String requestRaw(String url) {
Checks.notNull(url, "url");
try {
return client.newCall(new Request.Builder().url(url).build()).execute().body().string();
} catch (IOException e) {
Expand All @@ -35,6 +39,7 @@ public String requestRaw(String url) {
return null;
}
public JSONObject getJSONResponse(Response response) {
Checks.notNull(response, "response");
try {
return new JSONObject(response.body().string());
} catch (IOException e) {
Expand All @@ -43,6 +48,8 @@ public JSONObject getJSONResponse(Response response) {
return null;
}
public JSONObject requestKsoft(String url, String token) {
Checks.notNull(url, "url");
Checks.notNull(token, "token");
try {
String source = client.newCall(new Request.Builder().addHeader("Authorization", "Bearer " + token)
.url(url).build()).execute().body().string();
Expand All @@ -52,7 +59,16 @@ public JSONObject requestKsoft(String url, String token) {
}
return null;
}
public JSONObject addBanKsoft(JSONObject json, Routes route, String token) {
Checks.notNull(json, "json");
Checks.notNull(route, "route");
Checks.notNull(token, "token");
return addBanKsoft(json, route.toString(), token);
}
public JSONObject addBanKsoft(JSONObject json, String url, String token) {
Checks.notNull(url, "url");
Checks.notNull(json, "json");
Checks.notNull(token, "token");
FormBody.Builder body = new FormBody.Builder();
if(json.has("user")) {
body.add("user", json.getString("user"));
Expand Down Expand Up @@ -85,6 +101,8 @@ public JSONObject addBanKsoft(JSONObject json, String url, String token) {
return null;
}
public Response requestKsoftResponse(String url, String token) {
Checks.notNull(url, "url");
Checks.notNull(token, "token");
try {
return client.newCall(new Request.Builder().addHeader("Authorization", "Bearer " + token)
.url(url).build()).execute();
Expand All @@ -94,6 +112,8 @@ public Response requestKsoftResponse(String url, String token) {
return null;
}
public File getImage(String title, String url) {
Checks.notNull(url, "url");
Checks.notNull(title, "title");
try {
URL imageURL = new URL(url);
BufferedImage img = ImageIO.read(imageURL.openStream());
Expand Down

0 comments on commit 215cd4d

Please sign in to comment.