Skip to content

Commit

Permalink
Updated APICharityScraper.
Browse files Browse the repository at this point in the history
Replaced the connection/get request logic to make it easier to mocktest with mockito.
  • Loading branch information
roaraf committed Mar 3, 2026
1 parent f6cfff8 commit 9ebb476
Showing 1 changed file with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ntnu.sytemutvikling.team6.models;

import com.google.gson.Gson;

import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
Expand All @@ -12,35 +11,28 @@
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();

public APICharityScraper(HttpClient client) throws URISyntaxException {
this.client = client;
this.request = HttpRequest.newBuilder()
.uri(url.toURI())
.uri(new URI("https://app.innsamlingskontrollen.no/api/public/v1/all"))
.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();
public boolean checkConnection() throws IOException, InterruptedException {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

if (response == 200) {
if (response.statusCode() == 200) {
return true;
}
throw new RuntimeException("Connection failed : HTTP error code : " + response + ". \n Response text: " +
response_text);
throw new RuntimeException("Connection failed: HTTP error code: " + response.statusCode() +
"\nResponse text: " + response.body()
);
}

public String getJSONData() throws IOException, InterruptedException {
Expand All @@ -49,6 +41,7 @@ public String getJSONData() throws IOException, InterruptedException {

return response.body();
}

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

0 comments on commit 9ebb476

Please sign in to comment.