Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API class #20

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {
dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
}

test {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class Duke {
public static void main(String[] args) {
ModulePlannerController controller = new ModulePlannerController();
controller.start();

}

}
24 changes: 21 additions & 3 deletions src/main/java/seedu/duke/controllers/ModulePlannerController.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package seedu.duke.controllers;

import org.json.simple.JSONObject;
import seedu.duke.CompletePreqs;
import seedu.duke.ModuleList;
import seedu.duke.models.Major;
import seedu.duke.models.Student;
import seedu.duke.views.CommandLineView;
import seedu.duke.utils.Parser;
import seedu.duke.models.*;
import seedu.duke.views.ModuleInfo;

import java.net.URISyntaxException;

import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.Arrays;



import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.Scanner;
import java.util.HashMap;
import java.util.List;
public class ModulePlannerController {
private CommandLineView view;
private Parser parser;

private Student student;

private ModuleList modulesMajor;
private ModuleList modulesTaken;
private ModuleList modulesLeft;

private HashMap<String, List<String>> modsWithPreqs;
private CompletePreqs addModulePreqs;

Expand Down Expand Up @@ -87,6 +96,15 @@ public void start() {
computePace(words, creditsLeft);
break;
}
case "info": {
String moduleCode = words[1];
try {
JSONObject moduleInfo = Api.getModuleInfo(moduleCode);
ModuleInfo.printModule(moduleInfo);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
case "major": {
updateMajor(words[1]);
break;
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/seedu/duke/models/Api.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.duke.models;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import seedu.duke.views.ModuleInfo;


public class Api {
public static JSONObject getModuleInfo(String moduleCode) throws URISyntaxException {
String url = "https://api.nusmods.com/v2/2023-2024/modules/" + moduleCode + ".json";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url))
.GET()
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
int statusCode = response.statusCode();
String responseBody = response.body();
JSONParser parser = new JSONParser();
JSONObject moduleInfo = (JSONObject) parser.parse(responseBody);
ModuleInfo.printModule(moduleInfo);
return moduleInfo;
// Now you can access individual properties in the JSON
// String preclusionRule = (String) jsonObject.get("preclusionRule");
// String additionalInformation = (String) jsonObject.get("additionalInformation");
// JSONArray aliasesArray = (JSONArray) jsonObject.get("aliases");
// System.out.println(JsonFormatter.formatJson(responseBody));
// System.out.println(preclusionRule);
// System.out.println(additionalInformation);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


}
// public static String getModuleName(JSONObject module) {
// return (String) module.get("moduleName");
// }


Empty file.
39 changes: 39 additions & 0 deletions src/main/java/seedu/duke/models/JsonFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.duke.models;

public class JsonFormatter {
public static String formatJson(String jsonString) {
int indentLevel = 0;
StringBuilder formattedJson = new StringBuilder();

for (char charFromJson : jsonString.toCharArray()) {
if (charFromJson == '{' || charFromJson == '[') {
indentLevel++;
formattedJson.append(charFromJson).append("\n").append(getIndentString(indentLevel));
} else if (charFromJson == '}' || charFromJson == ']') {
indentLevel--;
formattedJson.append("\n").append(getIndentString(indentLevel)).append(charFromJson);
} else if (charFromJson == ',') {
formattedJson.append(charFromJson).append("\n").append(getIndentString(indentLevel));
} else {
formattedJson.append(charFromJson);
}
}

return formattedJson.toString();
}

private static String getIndentString(int indentLevel) {
StringBuilder indentString = new StringBuilder();
for (int i = 0; i < indentLevel; i++) {
indentString.append("\t"); // Use tabs for indentation (you can change this to spaces if desired)
}
return indentString.toString();
}
}







10 changes: 10 additions & 0 deletions src/main/java/seedu/duke/views/ModuleInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package seedu.duke.views;

import org.json.simple.JSONObject;

public class ModuleInfo {
public static void printModule(JSONObject module) {
System.out.println(module);
}

}
19 changes: 19 additions & 0 deletions src/test/java/seedu/duke/ApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.duke;

import org.json.simple.JSONObject;
import seedu.duke.models.Api;
import seedu.duke.views.ModuleInfo;

import java.util.concurrent.CompletableFuture;

public class ApiTest {
public static void main(String[] args) throws Exception {
// test setup
String moduleCode = "CS2113";
JSONObject moduleInfo = Api.getModuleInfo(moduleCode);
ModuleInfo.printModule(moduleInfo);

// more tests...
System.out.println("All tests passed");
}
}
Loading