Skip to content

Commit

Permalink
Finish Command.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed May 2, 2020
1 parent eb6a5c9 commit 280843a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 45 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins{
id 'com.github.johnrengelman.shadow' version '5.2.0'
}

def ver = new Version(major: 1, minor: 0, revision: 7)
def ver = new Version(major: 1, minor: 0, revision: 8)

group = 'net.discordservices'
version = "$ver"
Expand Down
96 changes: 72 additions & 24 deletions src/main/java/net/discordservices/dservices4j/Command.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
package net.discordservices.dservices4j;

import net.discordservices.dservices4j.requests.RequestHandler;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* Class used for posting Command information (name, description and category) to the Discordservices API.
*/
public class Command{

private final String TOKEN, id;
private JSONObject json;
private JSONArray json;
private final RequestHandler REQUEST_HANDLER = new RequestHandler();

Command(String token, String id){
this.TOKEN = token;
this.id = id;
this.json = new JSONObject();
this.json = new JSONArray();
}

/**
* Adds a command to the list using a {@link net.discordservices.dservices4j.Command.CommandInfo CommandInfo}
* instance.
*
* @param commandInfo
* @param command
* The Command that should be added.
*
* @return This class after the command was added. Useful for chaining.
*/
public Command addCommand(CommandInfo commandInfo){
JSONObject command = new JSONObject()
.put("command", commandInfo.getName())
.put("description", commandInfo.getDescription())
.put("category", commandInfo.getCategory());
public Command addCommand(CommandInfo command){
JSONObject cmdJson = new JSONObject()
.put("command", command.getName())
.put("description", command.getDescription())
.put("category", command.getCategory());
json.put(command);

return this;
}
Expand All @@ -38,15 +46,28 @@ public Command addCommand(CommandInfo commandInfo){
* Adds the provided commands to the list using the provided {@link net.discordservices.dservices4j.Command.CommandInfo CommandInfo}
* instances.
*
* @param commandInfos
* @param commands
* The commands to add.
*
* @return This class after the command was added. Useful for chaining.
*/
public Command addCommands(CommandInfo... commandInfos){
for(CommandInfo info : commandInfos)
addCommand(info);

public Command addCommands(CommandInfo... commands){
return addCommands(Arrays.asList(commands));
}

/**
* Adds the provided commands to the list using the provided List of {@link net.discordservices.dservices4j.Command.CommandInfo CommandInfo}
* instances.
*
* @param commands
* The commands to add.
*
* @return This class after the command was added. Useful for chaining.
*/
public Command addCommands(List<CommandInfo> commands){
for(CommandInfo command : commands)
addCommand(command);

return this;
}

Expand All @@ -55,29 +76,56 @@ public Command addCommands(CommandInfo... commandInfos){
* instances.
* <br><b>This will remove all previously set commands!</b>
*
* @param commandInfos
* @param commands
* The commands to set.
*
* @return This class after the commands were set. Useful for chaining.
*/
public Command setCommands(CommandInfo... commandInfos){
json = new JSONObject();
public Command setCommands(CommandInfo... commands){
json = new JSONArray();

for(CommandInfo info : commandInfos)
addCommand(info);
return addCommands(commands);
}

/**
* Sets the provided commands for the list using the provided List of {@link net.discordservices.dservices4j.Command.CommandInfo CommandInfo}
* instances.
* <br><b>This will remove all previously set commands!</b>
*
* @param commands
* The commands to set.
*
* @return This class after the commands were set. Useful for chaining.
*/
public Command setCommands(List<CommandInfo> commands){
json = new JSONArray();

return this;
return addCommands(commands);
}

/**
* Posts the previously set commands to the Discordservices API.
*
* <p>This method can throw the following exceptions from the POST request:
* <br><ul>
* <li>{@link java.io.IOException IOException}
* <br>When the request wasn't successfull.</li>
* <li>{@link net.discordservices.dservices4j.exceptions.RatelimitedException RatelimitedException}
* <br>When the Bot got rate limited by the site.</li>
* </ul>
*
* @throws java.lang.NullPointerException
* When no command was previously set.
*/
public void postCommands(){
if(json.isEmpty())
throw new NullPointerException("Command list may not be empty.");

try {
REQUEST_HANDLER.postCommands(id, TOKEN, json);
}catch(IOException ex){
ex.printStackTrace();
}
}

/**
Expand Down Expand Up @@ -115,15 +163,15 @@ public CommandInfo(String name, String description, String category){
this.category = category;
}

public String getName(){
protected String getName(){
return name;
}
public String getDescription(){

protected String getDescription(){
return description;
}
public String getCategory(){

protected String getCategory(){
return category;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DServices4J{
private Stats stats = null;
private News news = null;

public DServices4J(String token, String id){
DServices4J(String token, String id){
this.TOKEN = token;
this.id = id;
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/net/discordservices/dservices4j/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ public class News{
* <br>The posts made with this method will never be seen as errors. Use {@link #postNews(String, String, boolean) postNews(String, String, true)}
* if you want to send an Error message.
*
* <p>Note that this might throw a {@link java.io.IOException IOException} on a not successfull POST.
* <p>This method can throw the following exceptions from the POST request:
* <br><ul>
* <li>{@link java.io.IOException IOException}
* <br>When the request wasn't successfull.</li>
* <li>{@link net.discordservices.dservices4j.exceptions.RatelimitedException RatelimitedException}
* <br>When the Bot got rate limited by the site.</li>
* </ul>
*
* @param title
* The title of the News post.
* @param message
* The message of the news.
*/
public void postNes(String title, String message){
public void postNews(String title, String message){
postNews(title, message, false);
}

/**
* Posts news to the bot page.
*
* <p>Note that this might throw a {@link java.io.IOException IOException} on a not successfull POST.
* <p>This method can throw the following exceptions from the POST request:
* <br><ul>
* <li>{@link java.io.IOException IOException}
* <br>When the request wasn't successfull.</li>
* <li>{@link net.discordservices.dservices4j.exceptions.RatelimitedException RatelimitedException}
* <br>When the Bot got rate limited by the site.</li>
* </ul>
*
* @param title
* The title of the News post.
Expand All @@ -55,7 +67,7 @@ public void postNews(String title, String message, boolean isError){
.put("error", isError);

try{
REQUEST_HANDLER.post("news", id, TOKEN, json);
REQUEST_HANDLER.postNews(id, TOKEN, json);
}catch(IOException ex){
ex.printStackTrace();
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/net/discordservices/dservices4j/Stats.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public class Stats{
*
* <p>Use {@link #postStats(long, long) postStats(long, long)} if you also want to provide the shards.
*
* <p>Note that this might throw a {@link java.io.IOException IOException} on a not successfull POST.
* <p>This method can throw the following exceptions from the POST request:
* <br><ul>
* <li>{@link java.io.IOException IOException}
* <br>When the request wasn't successfull.</li>
* <li>{@link net.discordservices.dservices4j.exceptions.RatelimitedException RatelimitedException}
* <br>When the Bot got rate limited by the site.</li>
* </ul>
*
* @param server
* Amount of servers your bot is in.
Expand All @@ -39,8 +45,14 @@ public void postStats(long server){
/**
* Performs a POST request towards the Stats endpoint.
* <br>This will post the provided servers with the provided shards.
*
* <p>Note that this might throw a {@link java.io.IOException IOException} on a not successfull POST.
*
* <p>This method can throw the following exceptions from the POST request:
* <br><ul>
* <li>{@link java.io.IOException IOException}
* <br>When the request wasn't successfull.</li>
* <li>{@link net.discordservices.dservices4j.exceptions.RatelimitedException RatelimitedException}
* <br>When the Bot got rate limited by the site.</li>
* </ul>
*
* @param server
* Amount of servers your bot is in.
Expand All @@ -52,7 +64,7 @@ public void postStats(long server, long shards){
.put("shards", shards);

try{
REQUEST_HANDLER.post("stats", id, TOKEN, json);
REQUEST_HANDLER.postStats(id, TOKEN, json);
}catch(IOException ex){
ex.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,51 @@

import net.discordservices.dservices4j.exceptions.RatelimitedException;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

public class RequestHandler{

private final String BASE_URL = "https://api.discordservices.net/bot/";
private final OkHttpClient CLIENT = new OkHttpClient();

public void post(String endpoint, String id, String token, JSONObject json) throws IOException{
String url = BASE_URL + id + endpoint;

RequestBody body = RequestBody.create(json.toString(), null);
public void postNews(String id, String token, JSONObject json) throws IOException{
post("news", id, token, json.toString());
}

public void postStats(String id, String token, JSONObject json) throws IOException{
post("stats", id, token, json.toString());
}

public void postCommands(String id, String token, JSONArray json) throws IOException{
post("commands", id, token, json.toString());
}

private void post(String endpoint, String id, String token, String json) throws IOException{
String url = "https://api.discordservices.net/bot/" + id + endpoint;

RequestBody body = RequestBody.create(json, null);
Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/json")
.addHeader("Authentication", token)
.post(body)
.build();

try(Response response = CLIENT.newCall(request).execute()){
ResponseBody responseBody = response.body();

if(responseBody == null)
throw new NullPointerException("Received empty Response body.");

String bodyString = responseBody.string();
if(bodyString.isEmpty())
throw new NullPointerException("Received empty Response body.");

if(!response.isSuccessful()){
if(response.code() == 429)
throw new RatelimitedException();

throw new IOException("Received non-successful response. (" + response.code() + " " + response.message() + ")");
}
}
Expand Down

0 comments on commit 280843a

Please sign in to comment.