Skip to content

Commit

Permalink
Added APICharityScraper & APICharityData class.
Browse files Browse the repository at this point in the history
Added a class for scraping the IK API and parsing the JSON values, pushing them to a APICharityData object for easy migrating to a database later.
  • Loading branch information
roaraf committed Feb 27, 2026
1 parent 675612f commit f26ab51
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

return response.body();
}
public List<APICharityData> 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));
}

}

0 comments on commit f26ab51

Please sign in to comment.