feat: use WeatherFetching exception
This commit is contained in:
parent
5dfed9ff4c
commit
bc584b3ff0
@ -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<WeatherData> getTemperatures(int days, String city) throws IOException {
|
||||
public ArrayList<WeatherData> getTemperatures(int days, String city) throws WeatherFetchingException{
|
||||
JSONObject result = fetchWeather(days, new City(city));
|
||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||
|
||||
|
@ -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<WeatherData> getTemperatures(int days, String city) throws IOException {
|
||||
public ArrayList<WeatherData> getTemperatures(int days, String city) throws WeatherFetchingException{
|
||||
JSONObject result = fetchWeather(new City(city));
|
||||
|
||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||
|
@ -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<WeatherData> getTemperatures(int days, String city) throws IOException {
|
||||
public ArrayList<WeatherData> getTemperatures(int days, String city) throws WeatherFetchingException{
|
||||
JSONObject result = fetchWeather(days, city);
|
||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||
|
||||
|
@ -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<WeatherData> getTemperatures(int days, String city) throws IOException;
|
||||
ArrayList<WeatherData> getTemperatures(int days, String city) throws WeatherFetchingException;
|
||||
|
||||
/***
|
||||
* Name of the API
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package eirb.pg203.exceptions;
|
||||
|
||||
public class WeatherFetchingExceptionApi extends WeatherFetchingException{
|
||||
public WeatherFetchingExceptionApi() {
|
||||
super("An error occurred during API fetching");
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package eirb.pg203.exceptions;
|
||||
|
||||
public class WeatherFetchingExceptionCityCoords extends WeatherFetchingException{
|
||||
public WeatherFetchingExceptionCityCoords() {
|
||||
super("Impossible to get city coords");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user