From 636351e20b725d5647844beaf38271e18740283e Mon Sep 17 00:00:00 2001 From: Roar Date: Wed, 4 Mar 2026 21:22:34 +0100 Subject: [PATCH] Updated APICharityScraper Added JavaDocs. --- .../team6/models/APICharityScraper.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java index 8bb1b1d..e92baca 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java @@ -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; @@ -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 response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -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 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 parseJSON(String JSONData) { Gson gson = new Gson(); APICharityData[] charityData = gson.fromJson(JSONData, APICharityData[].class);