The search
method
I'm not going to implement the search
method! It will be your homework.
But I will help you with a sample code to show how you can use the Unirest library.
Update the search
method as follows:
private static void search(String query) {
final String BASE_URL = "https://sis.jhu.edu/api/classes";
final String KEY = "YOUR_KEY_GOES_HERE";
String endpoint = BASE_URL + "/codes/schools?Key=" + KEY;
HttpResponse<JsonNode> jsonResponse = Unirest.get(endpoint).asJson();
System.out.println(jsonResponse.getBody().toString());
}
This method will find all the schools (and their codes) and return it to you (irrespective of the search query you enter):
Okay, that was easy! We want to now commit the changes to the Git repository. However, you should not store sensitive informations like API keys, credentials, etc., in source code. You certainly don't want to commit it to your Git repository!
A common practice is to store sensitive information such as API keys as "environment variables".
In IntelliJ, you can add Environment Variables as part of "run" configuration. Click where it is pointed by arrow in the image below and select "Edit Configuration":
Add the environment variable as key-value pair where it is pointed to in the screenshot bellow.
Now update the implementation of search
so you are reading the value of KEY
from rhe environment variable JHU_API_KEY
.
private static void search(String query) {
final String BASE_URL = "https://sis.jhu.edu/api/classes";
final String KEY = System.getenv("JHU_API_KEY");
String endpoint = BASE_URL + "/codes/schools?Key=" + KEY;
HttpResponse<JsonNode> jsonResponse = Unirest.get(endpoint).asJson();
System.out.println(jsonResponse.getBody().toString());
}
Run the application an make sure it works. Then, commit the changes.