diff --git a/src/main/java/eirb/pg203/WeatherAPI.java b/src/main/java/eirb/pg203/WeatherAPI.java index a27c677..6c6f2f1 100644 --- a/src/main/java/eirb/pg203/WeatherAPI.java +++ b/src/main/java/eirb/pg203/WeatherAPI.java @@ -3,10 +3,9 @@ package eirb.pg203; import org.json.JSONArray; import org.json.JSONObject; -import java.io.BufferedReader; +import eirb.pg203.utils.JSONFetcher; + import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.time.Instant; @@ -20,7 +19,6 @@ public class WeatherAPI implements WeatherDataAPI{ } private JSONObject fetchWeather(int days, String city) throws IOException { - StringBuilder result = new StringBuilder(); URL url = URI.create( String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d", this.weatherAPIKey, @@ -28,16 +26,8 @@ public class WeatherAPI implements WeatherDataAPI{ days ) ).toURL(); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - try (BufferedReader reader = new BufferedReader( - new InputStreamReader(conn.getInputStream()))) { - for (String line; (line = reader.readLine()) != null; ) { - result.append(line); - } - } - return new JSONObject(result.toString()); + return JSONFetcher.fetch(url); } /**