From b2440a2441f95ea27fb2d5a8ea2804c3189bc3a9 Mon Sep 17 00:00:00 2001 From: rohit Date: Tue, 24 Oct 2023 19:43:25 +0800 Subject: [PATCH 1/2] Add API Class --- src/main/java/seedu/duke/models/Api.java | 39 ++++++++++++++++++ src/test/java/seedu/duke/ApiTest.java | 51 ++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/seedu/duke/models/Api.java create mode 100644 src/test/java/seedu/duke/ApiTest.java diff --git a/src/main/java/seedu/duke/models/Api.java b/src/main/java/seedu/duke/models/Api.java new file mode 100644 index 0000000000..a7a014f0af --- /dev/null +++ b/src/main/java/seedu/duke/models/Api.java @@ -0,0 +1,39 @@ +package seedu.duke.models; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class Api { + + public static String 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 response = client.send(request, HttpResponse.BodyHandlers.ofString()); + return response.body(); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } + public static String getDescription(String moduleInfo) { + int indexOfDescriptionStart = moduleInfo.indexOf("description"); + int indexOfDescriptionEnd = moduleInfo.indexOf("workload"); + if (indexOfDescriptionEnd != -1 | (indexOfDescriptionEnd != -1)) { + System.out.println("Index of 'description': " + indexOfDescriptionStart); + System.out.println("Index of 'description' end: " + indexOfDescriptionEnd); + } else { + System.out.println("'description' not found in the string."); + } + // assuming that workload is always the next object after description + String description = moduleInfo.substring(indexOfDescriptionStart + 8, indexOfDescriptionEnd - 2); + System.out.println(description); + return description; + } +} diff --git a/src/test/java/seedu/duke/ApiTest.java b/src/test/java/seedu/duke/ApiTest.java new file mode 100644 index 0000000000..9762bc37b9 --- /dev/null +++ b/src/test/java/seedu/duke/ApiTest.java @@ -0,0 +1,51 @@ +package seedu.duke; + +import seedu.duke.models.Api; + +import java.net.URISyntaxException; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ApiTest { + @Test + void testGetModuleInfo() { + String correctModuleInfo = "\"description\":\"This course introduces the necessary skills for systematic " + + "and rigorous development of software systems. It covers"; + String moduleCode = "CS2113"; + String moduleInfo = null; + try { + moduleInfo = Api.getModuleInfo(moduleCode); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + assertNotNull(moduleInfo, "Module info should not be null"); + assertTrue(moduleInfo.contains(correctModuleInfo), "Module info should contain relevant info"); + } + + @Test + void testGetDescription() { + String correctDescription = "This course introduces the necessary skills for systematic and " + + "rigorous development of software systems. It covers requirements, design, implementation, " + + "quality assurance, and project management aspects of small-to-medium size multi-person software" + + " projects. The course uses the Object Oriented Programming paradigm. Students of this course will " + + "receive hands-on practice of tools commonly used in the industry, such as test automation tools," + + " build automation tools, and code revisioning tools will be covered."; + String moduleInfo = "\"CS2040C:D\",{\"and\":[\"CS2030:D\",{\"or\":[\"CS2040S:D\",\"CS204" + + "0:D\"]}]}]},\"description\":\"This course introduces the necessary skills for systemati" + + "c and rigorous development of software systems. It covers requirements, design, implementat" + + "ion, quality assurance, and project management aspects of small-to-medium size multi-person " + + "software projects. The course uses the Object Oriented Programming paradigm. Students of thi" + + "s course will receive hands-on practice of tools commonly used in the industry, such as te" + + "st automation tools, build automation tools, and code revisioning tools will be covered.\",\"w" + + "orkload\":[2,1,0,3,4],\"title\":\"Software Engineering & Object-Oriented Programming\",\"aca" + + "dYear\":\"2023\\/2024\",\"faculty\":\"Computing\",\"gradingBa"; + String description = Api.getDescription(moduleInfo); + assertTrue(description.contains(correctDescription), "Module info should contain relevant info"); + } + + +} From a74c4c143857f517b7518f354aa339e5b90c9eb6 Mon Sep 17 00:00:00 2001 From: rohit Date: Tue, 24 Oct 2023 19:44:05 +0800 Subject: [PATCH 2/2] Add API Class --- src/test/java/seedu/duke/ApiTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/seedu/duke/ApiTest.java b/src/test/java/seedu/duke/ApiTest.java index 9762bc37b9..f77850c007 100644 --- a/src/test/java/seedu/duke/ApiTest.java +++ b/src/test/java/seedu/duke/ApiTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; public class ApiTest {