-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added APICharityScraper & APICharityData class.
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
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/APICharityScraper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
|
|
||
| } |