feat: use JSONFetcher in WeatherAPI

This commit is contained in:
Nemo D'ACREMONT 2024-11-14 10:43:16 +01:00
parent cb18096f2e
commit 3a19820ffa
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276

View File

@ -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);
}
/**