Skip to content

Commit

Permalink
Added /api/v2/readonly/tracks
Browse files Browse the repository at this point in the history
Adjusted api_key check to apply to both v1 and v2 endpoints.

Closes #14
  • Loading branch information
JustBru00 committed Jul 20, 2023
1 parent 7173996 commit be3af3f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.frosthex</groupId>
<artifactId>timingsystem.restapi</artifactId>
<name>timingsystem.restapi</name>
<version>0.0.4</version>
<version>0.0.5</version>
<url>http://maven.apache.org</url>
<build>
<sourceDirectory>src</sourceDirectory>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.frosthex</groupId>
<artifactId>timingsystem.restapi</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<packaging>jar</packaging>
<name>timingsystem.restapi</name>
<url>http://maven.apache.org</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TimingSystemRESTApiPlugin extends JavaPlugin {

private static TimingSystemRESTApiPlugin instance;
private static final int BSTATS_PLUGIN_ID = 18069;
private static final String[] TIMING_SYSTEM_SUPPORTED_VERSIONS = {"1.2", "1.3"};
private static final String[] TIMING_SYSTEM_SUPPORTED_VERSIONS = {"1.2", "1.3", "1.5"};

public static ConsoleCommandSender clogger = Bukkit.getServer().getConsoleSender();
public static Logger log = Bukkit.getLogger();
Expand Down Expand Up @@ -108,7 +108,7 @@ public void onEnable() {
// Commands
getCommand("timingsystemrestapi").setExecutor(new TimingSystemRestApiCommand());

// Strike the flint, ignite the spark IN 20 seconds
// Strike the flint, ignite the spark in 20 seconds
if (getConfig().getBoolean("rest_api_enabled")) {
Bukkit.getScheduler().runTaskLaterAsynchronously(instance, new Runnable() {

Expand Down
79 changes: 66 additions & 13 deletions src/com/frosthex/timingsystem/restapi/network/SparkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public static void initSpark() {
port(port);
staticFiles.externalLocation(pathToPublicHtmlFolder);

before("/api/v1/readonly/*", (request, response) -> {
before("/api/*/readonly/*", (request, response) -> {
// Allow all origins
response.header("Access-Control-Allow-Origin", "*");

// Authenticate READ ONLY
String apiKey = request.queryParams("api_key");
if (apiKey == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Couldn't read api_key. Please provide a valid api_key in your request.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Couldn't read api_key. Please provide a valid api_key in your request.\"}");
}

boolean authenticated = false;
Expand All @@ -72,13 +72,13 @@ public static void initSpark() {
}

if (!authenticated) {
halt(401, "{\"error\":true,\"errorMessage\":\"Unknown api_key. Please provide a valid api_key in your request.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Unknown api_key. Please provide a valid api_key in your request.\"}");
}

// TODO RATE LIMIT HERE
});

before("/api/v1/readwrite/*", (request, response) -> {
before("/api/*/readwrite/*", (request, response) -> {
// Allow all origins
response.header("Access-Control-Allow-Origin", "*");

Expand All @@ -90,7 +90,7 @@ public static void initSpark() {
var tracks = TimingSystemAPI.getTracks();

if (tracks == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong TimingSystemAPI.getTracks() is null.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. TimingSystemAPI.getTracks() is null.\"}");
}

JsonObject tracksResponseObject = new JsonObject();
Expand All @@ -112,17 +112,70 @@ public static void initSpark() {
return tracksResponseObject.toString();
});

// /api/v2/readonly/tracks
get("/api/v2/readonly/tracks", (request, response) -> {
var tracks = TimingSystemAPI.getTracks();

if (tracks == null) {
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. TimingSystemAPI.getTracks() is null.\"}");
}

JsonObject tracksResponseObject = new JsonObject();

tracksResponseObject.addProperty("number", tracks.size());

JsonArray tracksListObject = new JsonArray();

for (Track track : tracks) {
if (track.getCommandName() == null) {
continue;
}
JsonObject trackObj = new JsonObject();
trackObj.addProperty("command_name", track.getCommandName());
trackObj.addProperty("display_name", track.getDisplayName());
trackObj.addProperty("mode", track.getModeAsString());
trackObj.addProperty("type", track.getTypeAsString());
trackObj.addProperty("open", track.isOpen());
trackObj.addProperty("date_created", track.getDateCreated());
trackObj.addProperty("id", track.getId());
trackObj.addProperty("total_attempts", track.getTotalAttempts());
trackObj.addProperty("total_finishes", track.getTotalFinishes());
trackObj.addProperty("total_time_spent", track.getTotalTimeSpent());
trackObj.addProperty("weight", track.getWeight());
trackObj.addProperty("gui_item", track.getGuiItem().toString());
JsonArray optionsArray = new JsonArray();
for (char c : track.getOptions()) {
optionsArray.add(c);
}
trackObj.add("options", optionsArray);
trackObj.addProperty("owner", track.getOwner().getUniqueId().toString());
trackObj.add("spawn_location", serializeLocation(track.getSpawnLocation()));
JsonArray tagsArray = new JsonArray();
for (TrackTag trackTag : track.getTags()) {
tagsArray.add(trackTag.getValue());
}
trackObj.add("tags", tagsArray);

tracksListObject.add(trackObj);
}

tracksResponseObject.add("tracks", tracksListObject);

response.status(200);
return tracksResponseObject.toString();
});

// /api/v1/readonly/tracks/:trackname
get("/api/v1/readonly/tracks/:trackname", (request, response) -> {
String trackInternalName = request.params("trackname");

if (trackInternalName == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. The track name provided is null.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. The track name provided is null.\"}");
}

Optional<Track> optionalTrack = TimingSystemAPI.getTrack(trackInternalName);
if (optionalTrack.isEmpty()) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. Could find a track with that name.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. Could find a track with that name.\"}");
}

Track track = optionalTrack.get();
Expand Down Expand Up @@ -172,12 +225,12 @@ public static void initSpark() {
String trackInternalName = request.params("trackname");

if (trackInternalName == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. The track name provided is null.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. The track name provided is null.\"}");
}

Optional<Track> optionalTrack = TimingSystemAPI.getTrack(trackInternalName);
if (optionalTrack.isEmpty()) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. Could find a track with that name.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. Could find a track with that name.\"}");
}

Track track = optionalTrack.get();
Expand Down Expand Up @@ -228,7 +281,7 @@ public static void initSpark() {
String uuidOrUsernameString = request.params("uuidorusername");

if (uuidOrUsernameString == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. UUID or username argument was null\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. UUID or username argument was null\"}");
}

UUID uuid = UUID.randomUUID();
Expand All @@ -240,7 +293,7 @@ public static void initSpark() {
OfflinePlayer offline = Bukkit.getOfflinePlayerIfCached(uuidOrUsernameString);

if (offline == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. UUID or username couldn't be parsed from input.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. UUID or username couldn't be parsed from input.\"}");
} else {
uuid = offline.getUniqueId();
}
Expand All @@ -249,7 +302,7 @@ public static void initSpark() {
tPlayer = TimingSystemAPI.getTPlayer(uuid);

if (tPlayer == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong. That player couldn't be found.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong. That player couldn't be found.\"}");
}

JsonObject responseObject = new JsonObject();
Expand All @@ -271,7 +324,7 @@ public static void initSpark() {
var heats = TimingSystemAPI.getRunningHeats();

if (heats == null) {
halt(401, "{\"error\":true,\"errorMessage\":\"Something went wrong TimingSystem.getRunningHeats() is null.\"}");
halt(401, "{\"error\":true,\"error_message\":\"Something went wrong TimingSystem.getRunningHeats() is null.\"}");
}

JsonArray arrayObj = new JsonArray();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: TimingSystemRESTApi
version: 0.0.5
version: 0.0.6
authors: [Justin Brubaker (JustBru00)]
softdepend: [TimingSystem]
main: com.frosthex.timingsystem.restapi.TimingSystemRESTApiPlugin
Expand Down

0 comments on commit be3af3f

Please sign in to comment.