diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityData.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityData.java new file mode 100644 index 0000000..d94db88 --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityData.java @@ -0,0 +1,37 @@ +package ntnu.sytemutvikling.team6.models; + +public class APICharityData { + private final String org_number; + private final String name; + private final String status; + private final String url; + private final boolean is_pre_approved; + + public APICharityData(String org_number, String name, String status, String url, boolean is_pre_approved) { + this.org_number = org_number; + this.name = name; + this.status = status; + this.url = url; + this.is_pre_approved = is_pre_approved; + } + + public String getOrg_number() { + return this.org_number; + } + + public String getName() { + return name; + } + + public String getStatus() { + return status; + } + + public String getUrl() { + return url; + } + + public boolean getIs_pre_approved() { + return this.is_pre_approved; + } +} diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java new file mode 100644 index 0000000..27f2e60 --- /dev/null +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java @@ -0,0 +1,63 @@ +package ntnu.sytemutvikling.team6.models; + +import com.google.gson.Gson; + +import java.io.IOException; +import java.net.*; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class APICharityScraper { + private final URL url; + private HttpURLConnection connection; + private final HttpClient client; + private final HttpRequest request; + + public APICharityScraper() throws IOException, URISyntaxException { + this.url = new URI("https://app.innsamlingskontrollen.no/api/public/v1/all").toURL(); + this.client = HttpClient.newHttpClient(); + this.connection = (HttpURLConnection) url.openConnection(); + this.request = HttpRequest.newBuilder() + .uri(url.toURI()) + .GET() + .build(); + } + + // Checks whether a connection returns a get response + public boolean checkConnection() throws IOException { + this.connection = (HttpURLConnection) url.openConnection(); + this.connection.setRequestMethod("GET"); + this.connection.connect(); + + int response = this.connection.getResponseCode(); + String response_text = this.connection.getResponseMessage(); + + if (response == 200) { + return true; + } + throw new RuntimeException("Connection failed : HTTP error code : " + response + ". \n Response text: " + + response_text); + } + + 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(); + } + public List parseJSON(String JSONData) { + Gson gson = new Gson(); + APICharityData[] charityData = gson.fromJson(JSONData, APICharityData[].class); + + if (charityData == null) { + return new ArrayList<>(); + } + // Returns mutable list of JSON data converted to APICharityData Objects + return new ArrayList<>(Arrays.asList(charityData)); + } + +}