diff --git a/src/main/java/eirb/pg203/OpenMeteo.java b/src/main/java/eirb/pg203/OpenMeteo.java index 0031692..75fa51f 100644 --- a/src/main/java/eirb/pg203/OpenMeteo.java +++ b/src/main/java/eirb/pg203/OpenMeteo.java @@ -1,5 +1,7 @@ package eirb.pg203; +import eirb.pg203.exceptions.WeatherFetchingException; +import eirb.pg203.exceptions.WeatherFetchingExceptionApi; import org.json.JSONObject; import eirb.pg203.utils.JSONFetcher; @@ -32,16 +34,25 @@ public class OpenMeteo implements WeatherDataAPI { // https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM - private JSONObject fetchWeather(int days, City city) throws IOException { - URL url = URI.create( - String.format(Locale.ENGLISH, forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery, - city.getCityCoords().getLat(), - city.getCityCoords().getLon(), - days - ) - ).toURL(); + private JSONObject fetchWeather(int days, City city) throws WeatherFetchingException{ + URL url = null; + try { + url = URI.create( + String.format(Locale.ENGLISH, forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery, + city.getCityCoords().getLat(), + city.getCityCoords().getLon(), + days + ) + ).toURL(); + } catch (IOException e) { + throw new WeatherFetchingException("Impossible to get city coords"); + } - return JSONFetcher.fetch(url); + try { + return JSONFetcher.fetch(url); + } catch (IOException e) { + throw new WeatherFetchingExceptionApi(); + } } /** @@ -85,19 +96,19 @@ public class OpenMeteo implements WeatherDataAPI { * @param day Day, 0 ≤ day ≤ 14 */ @Override - public WeatherData getTemperature(int day, String city) throws IOException { + public WeatherData getTemperature(int day, String city) throws WeatherFetchingException { JSONObject result = fetchWeather(day + 1, new City(city)); return getWeatherDataFromForecast(result, day, city); } @Override - public WeatherData getTemperature(int day, int hour, String city) throws IOException{ + public WeatherData getTemperature(int day, int hour, String city) throws WeatherFetchingException{ return getTemperature(day, city); } @Override - public ArrayList getTemperatures(int days, String city) throws IOException { + public ArrayList getTemperatures(int days, String city) throws WeatherFetchingException{ JSONObject result = fetchWeather(days, new City(city)); ArrayList weatherDatas = new ArrayList<>(); diff --git a/src/main/java/eirb/pg203/OpenWeatherMap.java b/src/main/java/eirb/pg203/OpenWeatherMap.java index 333b77c..2e4e424 100644 --- a/src/main/java/eirb/pg203/OpenWeatherMap.java +++ b/src/main/java/eirb/pg203/OpenWeatherMap.java @@ -1,5 +1,8 @@ package eirb.pg203; +import eirb.pg203.exceptions.WeatherFetchingException; +import eirb.pg203.exceptions.WeatherFetchingExceptionApi; +import eirb.pg203.exceptions.WeatherFetchingExceptionCityCoords; import org.json.JSONObject; import org.json.JSONArray; @@ -30,16 +33,25 @@ public class OpenWeatherMap implements WeatherDataAPI { this.APIKey = APIKey; } - private JSONObject fetchWeather(City city) throws IOException { - URL url = URI.create( - String.format(Locale.ENGLISH, forecastBaseURL + "?appid=%s&lat=%.2f&lon=%.2f&units=metric", - APIKey, - city.getCityCoords().getLat(), - city.getCityCoords().getLon() - ) - ).toURL(); + private JSONObject fetchWeather(City city) throws WeatherFetchingException { + URL url = null; + try { + url = URI.create( + String.format(Locale.ENGLISH, forecastBaseURL + "?appid=%s&lat=%.2f&lon=%.2f&units=metric", + APIKey, + city.getCityCoords().getLat(), + city.getCityCoords().getLon() + ) + ).toURL(); + } catch (IOException e) { + throw new WeatherFetchingExceptionCityCoords(); + } - return JSONFetcher.fetch(url); + try { + return JSONFetcher.fetch(url); + } catch (IOException e) { + throw new WeatherFetchingExceptionApi(); + } } private WeatherData getWeatherDataFromForecast(JSONObject response, int day, String city) { @@ -103,19 +115,19 @@ public class OpenWeatherMap implements WeatherDataAPI { * @param day Day, 0 ≤ day ≤ 14 */ @Override - public WeatherData getTemperature(int day, String city) throws IOException { + public WeatherData getTemperature(int day, String city) throws WeatherFetchingException{ JSONObject result = fetchWeather(new City(city)); return getWeatherDataFromForecast(result, day, city); } @Override - public WeatherData getTemperature(int day, int hour, String city) throws IOException{ + public WeatherData getTemperature(int day, int hour, String city) throws WeatherFetchingException{ return getTemperature(day, city); } @Override - public ArrayList getTemperatures(int days, String city) throws IOException { + public ArrayList getTemperatures(int days, String city) throws WeatherFetchingException{ JSONObject result = fetchWeather(new City(city)); ArrayList weatherDatas = new ArrayList<>(); diff --git a/src/main/java/eirb/pg203/WeatherAPI.java b/src/main/java/eirb/pg203/WeatherAPI.java index 3431c86..570b169 100644 --- a/src/main/java/eirb/pg203/WeatherAPI.java +++ b/src/main/java/eirb/pg203/WeatherAPI.java @@ -1,5 +1,7 @@ package eirb.pg203; +import eirb.pg203.exceptions.WeatherFetchingException; +import eirb.pg203.exceptions.WeatherFetchingExceptionApi; import org.json.JSONArray; import org.json.JSONObject; @@ -7,6 +9,7 @@ import eirb.pg203.WeatherData.Condition; import eirb.pg203.utils.JSONFetcher; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.time.Instant; @@ -25,16 +28,25 @@ public class WeatherAPI implements WeatherDataAPI{ this.weatherAPIKey = weatherAPIKey; } - private JSONObject fetchWeather(int days, String city) throws IOException { - URL url = URI.create( - String.format(Locale.ENGLISH, forecastBaseURL + "?key=%s&q=%s&days=%d", - this.weatherAPIKey, - city, - days - ) - ).toURL(); + private JSONObject fetchWeather(int days, String city) throws WeatherFetchingException { + URL url = null; + try { + url = URI.create( + String.format(Locale.ENGLISH, forecastBaseURL + "?key=%s&q=%s&days=%d", + this.weatherAPIKey, + city, + days + ) + ).toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } - return JSONFetcher.fetch(url); + try { + return JSONFetcher.fetch(url); + } catch (IOException e) { + throw new WeatherFetchingExceptionApi(); + } } private static WeatherData.Condition getConditionFromString(String str) { @@ -82,18 +94,18 @@ public class WeatherAPI implements WeatherDataAPI{ * @param day Day, 0 ≤ day ≤ 14 */ @Override - public WeatherData getTemperature(int day, String city) throws IOException { + public WeatherData getTemperature(int day, String city) throws WeatherFetchingException{ JSONObject result = fetchWeather(day+1, city); return getWeatherDataFromForecast(result, day, city); } @Override - public WeatherData getTemperature(int day, int hour, String city) throws IOException{ + public WeatherData getTemperature(int day, int hour, String city) throws WeatherFetchingException{ return getTemperature(day, city); } @Override - public ArrayList getTemperatures(int days, String city) throws IOException { + public ArrayList getTemperatures(int days, String city) throws WeatherFetchingException{ JSONObject result = fetchWeather(days, city); ArrayList weatherDatas = new ArrayList<>(); diff --git a/src/main/java/eirb/pg203/WeatherDataAPI.java b/src/main/java/eirb/pg203/WeatherDataAPI.java index c91c416..3f9ec02 100644 --- a/src/main/java/eirb/pg203/WeatherDataAPI.java +++ b/src/main/java/eirb/pg203/WeatherDataAPI.java @@ -1,5 +1,7 @@ package eirb.pg203; +import eirb.pg203.exceptions.WeatherFetchingException; + import java.io.IOException; import java.util.ArrayList; @@ -15,7 +17,7 @@ public interface WeatherDataAPI { * @return Temperature of the day from the city * @throws IOException when request failed */ - WeatherData getTemperature(int day, String city) throws IOException; + WeatherData getTemperature(int day, String city) throws WeatherFetchingException; /** * Get WeatherData for a specific day @@ -25,7 +27,7 @@ public interface WeatherDataAPI { * @return Temperature of the day for a hour from the city * @throws IOException when request failed */ - WeatherData getTemperature(int day, int hour, String city) throws IOException; + WeatherData getTemperature(int day, int hour, String city) throws WeatherFetchingException; /** * Fetch the temperature for multiple day since today @@ -34,7 +36,7 @@ public interface WeatherDataAPI { * @return List of WeatherData * @throws IOException when request failed */ - ArrayList getTemperatures(int days, String city) throws IOException; + ArrayList getTemperatures(int days, String city) throws WeatherFetchingException; /*** * Name of the API diff --git a/src/main/java/eirb/pg203/WeatherDisplayBasic.java b/src/main/java/eirb/pg203/WeatherDisplayBasic.java index dddc38c..887a27f 100644 --- a/src/main/java/eirb/pg203/WeatherDisplayBasic.java +++ b/src/main/java/eirb/pg203/WeatherDisplayBasic.java @@ -1,5 +1,7 @@ package eirb.pg203; +import eirb.pg203.exceptions.WeatherFetchingException; + import java.util.ArrayList; import java.util.HashMap; @@ -149,7 +151,10 @@ class WeatherDisplayBasic implements WeatherDisplay { for (WeatherDataAPI w: apis) { try { weatherDatasMap.put(w, w.getTemperatures(days, city)); - } catch (Exception e) { + } catch (WeatherFetchingException e) { + System.err.println(w.getAPIName() + " failed to fetch meteo"); + } + catch (Exception e) { System.err.println(e); } } diff --git a/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionApi.java b/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionApi.java new file mode 100644 index 0000000..c6657e0 --- /dev/null +++ b/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionApi.java @@ -0,0 +1,7 @@ +package eirb.pg203.exceptions; + +public class WeatherFetchingExceptionApi extends WeatherFetchingException{ + public WeatherFetchingExceptionApi() { + super("An error occurred during API fetching"); + } +} diff --git a/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionCityCoords.java b/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionCityCoords.java new file mode 100644 index 0000000..75f472c --- /dev/null +++ b/src/main/java/eirb/pg203/exceptions/WeatherFetchingExceptionCityCoords.java @@ -0,0 +1,7 @@ +package eirb.pg203.exceptions; + +public class WeatherFetchingExceptionCityCoords extends WeatherFetchingException{ + public WeatherFetchingExceptionCityCoords() { + super("Impossible to get city coords"); + } +}