forked from nus-cs2113-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from rohitcube/Add-API-class-and-parser
Add API class and functions
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
|
||
} |