Skip to content

Commit

Permalink
Updated APICharityScraper
Browse files Browse the repository at this point in the history
Added JavaDocs.
  • Loading branch information
roaraf committed Mar 4, 2026
1 parent a723ced commit 636351e
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
import java.util.Arrays;
import java.util.List;

/**
* Fetches JSON information from the IK API and parses the JSON into a list of {@link APICharityData} objects.
*/

public class APICharityScraper {
private final HttpClient client;
private final HttpRequest request;

/**
* Constructs a new APICharityScraper object.
* @param client the client responsible for making the http connection.
* @throws URISyntaxException if the API URL is malformed or violates URI syntax rules
*/

public APICharityScraper(HttpClient client) throws URISyntaxException {
this.client = client;
Expand All @@ -23,7 +32,15 @@ public APICharityScraper(HttpClient client) throws URISyntaxException {
.build();
}

// Checks whether a connection returns a get response
/**
* Checks if the http request returns an 'OK' response.
*
* @return {@code true} if connection was successful
* @throws IOException if an I/O error occurs while sending or receiving the HTTP reques
* @throws InterruptedException if the operation is interrupted while waiting for the response
* @throws RuntimeException if the connection is unsuccessful
*/

public boolean checkConnection() throws IOException, InterruptedException {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

Expand All @@ -35,13 +52,28 @@ public boolean checkConnection() throws IOException, InterruptedException {
);
}

/**
* Fetches the JSON data from the IK API and stores it in a String.
*
* @return a String of the JSON values in IK API
* @throws IOException if an I/O error occurs while sending or receiving the HTTP request
* @throws InterruptedException if the operation is interrupted while waiting for the response
*/

public String getJSONData() throws IOException, InterruptedException {
// Gets the JSON data and stores it in response body
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

return response.body();
}

/**
* Parses the JSON data using gson and creates a list of APICharityData objects.
*
* @param JSONData the {@code String} of JSON data to be parsed
* @return a list of APICharityDaya objects
* @throws com.google.gson.JsonSyntaxException if the provided JSON is not valid
*/

public List<APICharityData> parseJSON(String JSONData) {
Gson gson = new Gson();
APICharityData[] charityData = gson.fromJson(JSONData, APICharityData[].class);
Expand Down

0 comments on commit 636351e

Please sign in to comment.