Skip to content

Commit

Permalink
Merge pull request #35 from rohitcube/Add-API-class-and-parser
Browse files Browse the repository at this point in the history
Add API class and functions
  • Loading branch information
rohitcube authored Oct 24, 2023
2 parents a435819 + a74c4c1 commit 43d67d6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/seedu/duke/models/Api.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
50 changes: 50 additions & 0 deletions src/test/java/seedu/duke/ApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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.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");
}


}

0 comments on commit 43d67d6

Please sign in to comment.