feat: use WeatherFetching exception
This commit is contained in:
parent
5dfed9ff4c
commit
bc584b3ff0
@ -1,5 +1,7 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingException;
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingExceptionApi;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcher;
|
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
|
// 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 {
|
private JSONObject fetchWeather(int days, City city) throws WeatherFetchingException{
|
||||||
URL url = URI.create(
|
URL url = null;
|
||||||
|
try {
|
||||||
|
url = URI.create(
|
||||||
String.format(Locale.ENGLISH, forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery,
|
String.format(Locale.ENGLISH, forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery,
|
||||||
city.getCityCoords().getLat(),
|
city.getCityCoords().getLat(),
|
||||||
city.getCityCoords().getLon(),
|
city.getCityCoords().getLon(),
|
||||||
days
|
days
|
||||||
)
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeatherFetchingException("Impossible to get city coords");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return JSONFetcher.fetch(url);
|
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
|
* @param day Day, 0 ≤ day ≤ 14
|
||||||
*/
|
*/
|
||||||
@Override
|
@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));
|
JSONObject result = fetchWeather(day + 1, new City(city));
|
||||||
|
|
||||||
return getWeatherDataFromForecast(result, day, city);
|
return getWeatherDataFromForecast(result, day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
return getTemperature(day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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));
|
JSONObject result = fetchWeather(days, new City(city));
|
||||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package eirb.pg203;
|
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.JSONObject;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
|
||||||
@ -30,16 +33,25 @@ public class OpenWeatherMap implements WeatherDataAPI {
|
|||||||
this.APIKey = APIKey;
|
this.APIKey = APIKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject fetchWeather(City city) throws IOException {
|
private JSONObject fetchWeather(City city) throws WeatherFetchingException {
|
||||||
URL url = URI.create(
|
URL url = null;
|
||||||
|
try {
|
||||||
|
url = URI.create(
|
||||||
String.format(Locale.ENGLISH, forecastBaseURL + "?appid=%s&lat=%.2f&lon=%.2f&units=metric",
|
String.format(Locale.ENGLISH, forecastBaseURL + "?appid=%s&lat=%.2f&lon=%.2f&units=metric",
|
||||||
APIKey,
|
APIKey,
|
||||||
city.getCityCoords().getLat(),
|
city.getCityCoords().getLat(),
|
||||||
city.getCityCoords().getLon()
|
city.getCityCoords().getLon()
|
||||||
)
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeatherFetchingExceptionCityCoords();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return JSONFetcher.fetch(url);
|
return JSONFetcher.fetch(url);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeatherFetchingExceptionApi();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeatherData getWeatherDataFromForecast(JSONObject response, int day, String city) {
|
private WeatherData getWeatherDataFromForecast(JSONObject response, int day, String city) {
|
||||||
@ -103,19 +115,19 @@ public class OpenWeatherMap implements WeatherDataAPI {
|
|||||||
* @param day Day, 0 ≤ day ≤ 14
|
* @param day Day, 0 ≤ day ≤ 14
|
||||||
*/
|
*/
|
||||||
@Override
|
@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));
|
JSONObject result = fetchWeather(new City(city));
|
||||||
|
|
||||||
return getWeatherDataFromForecast(result, day, city);
|
return getWeatherDataFromForecast(result, day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
return getTemperature(day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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));
|
JSONObject result = fetchWeather(new City(city));
|
||||||
|
|
||||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingException;
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingExceptionApi;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -7,6 +9,7 @@ import eirb.pg203.WeatherData.Condition;
|
|||||||
import eirb.pg203.utils.JSONFetcher;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@ -25,16 +28,25 @@ public class WeatherAPI implements WeatherDataAPI{
|
|||||||
this.weatherAPIKey = weatherAPIKey;
|
this.weatherAPIKey = weatherAPIKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject fetchWeather(int days, String city) throws IOException {
|
private JSONObject fetchWeather(int days, String city) throws WeatherFetchingException {
|
||||||
URL url = URI.create(
|
URL url = null;
|
||||||
|
try {
|
||||||
|
url = URI.create(
|
||||||
String.format(Locale.ENGLISH, forecastBaseURL + "?key=%s&q=%s&days=%d",
|
String.format(Locale.ENGLISH, forecastBaseURL + "?key=%s&q=%s&days=%d",
|
||||||
this.weatherAPIKey,
|
this.weatherAPIKey,
|
||||||
city,
|
city,
|
||||||
days
|
days
|
||||||
)
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return JSONFetcher.fetch(url);
|
return JSONFetcher.fetch(url);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeatherFetchingExceptionApi();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WeatherData.Condition getConditionFromString(String str) {
|
private static WeatherData.Condition getConditionFromString(String str) {
|
||||||
@ -82,18 +94,18 @@ public class WeatherAPI implements WeatherDataAPI{
|
|||||||
* @param day Day, 0 ≤ day ≤ 14
|
* @param day Day, 0 ≤ day ≤ 14
|
||||||
*/
|
*/
|
||||||
@Override
|
@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);
|
JSONObject result = fetchWeather(day+1, city);
|
||||||
return getWeatherDataFromForecast(result, day, city);
|
return getWeatherDataFromForecast(result, day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
return getTemperature(day, city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
JSONObject result = fetchWeather(days, city);
|
||||||
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
ArrayList<WeatherData> weatherDatas = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -15,7 +17,7 @@ public interface WeatherDataAPI {
|
|||||||
* @return Temperature of the day from the city
|
* @return Temperature of the day from the city
|
||||||
* @throws IOException when request failed
|
* @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
|
* Get WeatherData for a specific day
|
||||||
@ -25,7 +27,7 @@ public interface WeatherDataAPI {
|
|||||||
* @return Temperature of the day for a hour from the city
|
* @return Temperature of the day for a hour from the city
|
||||||
* @throws IOException when request failed
|
* @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
|
* Fetch the temperature for multiple day since today
|
||||||
@ -34,7 +36,7 @@ public interface WeatherDataAPI {
|
|||||||
* @return List of WeatherData
|
* @return List of WeatherData
|
||||||
* @throws IOException when request failed
|
* @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
|
* Name of the API
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import eirb.pg203.exceptions.WeatherFetchingException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@ -149,7 +151,10 @@ class WeatherDisplayBasic implements WeatherDisplay {
|
|||||||
for (WeatherDataAPI w: apis) {
|
for (WeatherDataAPI w: apis) {
|
||||||
try {
|
try {
|
||||||
weatherDatasMap.put(w, w.getTemperatures(days, city));
|
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);
|
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