From 36ddb10fe26aca1f36d270342524ccdb5ffbbf10 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:25:45 -0400 Subject: [PATCH] add RequestFunctions class --- .../requests/RequestFunctions.java | 45 +++++++++++++++++++ .../requests/SunriseSunsetRequestObject.java | 24 +++------- .../requests/WeatherRequestObject.java | 44 +++++++----------- 3 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java b/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java new file mode 100644 index 0000000..119e55c --- /dev/null +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java @@ -0,0 +1,45 @@ +package io.github.jack1424.realtimeweather.requests; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Scanner; + +public class RequestFunctions { + public static Object makeRequest(String URLString) throws IOException, HTTPResponseException, ParseException { + int responseCode = getResponseCode(URLString); + if (responseCode > 399) + throw new HTTPResponseException(responseCode); + + Scanner scanner = new Scanner(new URL(URLString).openStream()); + StringBuilder response = new StringBuilder(); + while (scanner.hasNextLine()) + response.append(scanner.nextLine()); + scanner.close(); + + return new JSONParser().parse(response.toString()); + } + + public static int getResponseCode(String URLString) throws IOException { + URL url = new URL(URLString); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.connect(); + return con.getResponseCode(); + } + + public static String getLatestVersion() throws Exception { + return ((JSONObject) ((JSONArray) makeRequest("https://api.modrinth.com/v2/project/WRA6ODcm/version")).get(0)).get("version_number").toString(); + } + + public static class HTTPResponseException extends Exception { + public HTTPResponseException(int responseCode) { + super(String.valueOf(responseCode)); + } + } +} diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java index d503fe0..70bd9d9 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java @@ -1,14 +1,10 @@ package io.github.jack1424.realtimeweather.requests; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.naming.ConfigurationException; -import javax.net.ssl.HttpsURLConnection; import java.io.IOException; -import java.net.ProtocolException; -import java.net.URL; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; @@ -20,23 +16,13 @@ public class SunriseSunsetRequestObject { private String sunriseTime, sunsetTime; public SunriseSunsetRequestObject(TimeZone timeZone, String lat, String lon) throws IOException, ParseException, ConfigurationException { - URL url = new URL(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon)); - - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.connect(); - int responseCode = con.getResponseCode(); - if (responseCode > 399) - throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); - - Scanner scanner = new Scanner(url.openStream()); - StringBuilder data = new StringBuilder(); - while (scanner.hasNext()) { - data.append(scanner.nextLine()); + JSONObject response; + try { + response = (JSONObject) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon))).get("results"); + } catch (RequestFunctions.HTTPResponseException e) { + throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")"); } - scanner.close(); - JSONObject response = (JSONObject) ((JSONObject) new JSONParser().parse(data.toString())).get("results"); sunriseTime = response.get("sunrise").toString(); sunsetTime = response.get("sunset").toString(); diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java index c1873d3..bdacf7d 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java @@ -2,46 +2,34 @@ package io.github.jack1424.realtimeweather.requests; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.naming.ConfigurationException; -import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.net.ProtocolException; -import java.net.URL; -import java.util.Scanner; public class WeatherRequestObject { private boolean rain = false, thunder = false; public WeatherRequestObject(String apiKey, String lat, String lon) throws IOException, ParseException, ConfigurationException { - URL url = new URL(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey)); + JSONArray conditions; + try { + conditions = (JSONArray) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey))).get("weather"); + } catch (RequestFunctions.HTTPResponseException e) { + int responseCode = Integer.parseInt(e.getMessage()); + if (responseCode > 499) { + throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); + } else if (responseCode > 399) { + String message = "Error when getting weather information: "; - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.connect(); - int responseCode = con.getResponseCode(); - if (responseCode > 499) { - throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); + if (responseCode == 401) + throw new ConfigurationException(message + "API key invalid. Check the Wiki for troubleshooting steps."); + else + throw new ProtocolException(message + "Unknown error"); + } else { + throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")"); + } } - else if (responseCode > 399) { - String message = "Error when getting weather information: "; - - if (responseCode == 401) - throw new ConfigurationException(message + "API key invalid. Check the Wiki for troubleshooting steps."); - else - throw new ProtocolException(message + "Unknown error"); - } - - Scanner scanner = new Scanner(url.openStream()); - StringBuilder data = new StringBuilder(); - while (scanner.hasNext()) { - data.append(scanner.nextLine()); - } - scanner.close(); - - JSONArray conditions = (JSONArray) ((JSONObject) new JSONParser().parse(data.toString())).get("weather"); for (Object rawCondition : conditions) { int id = Integer.parseInt(String.valueOf(((JSONObject) rawCondition).get("id")));