feat: refactor JSON fetcher
This commit is contained in:
parent
10e36d97c4
commit
00f77897c9
@ -1,15 +1,11 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcher;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -23,7 +19,7 @@ import eirb.pg203.utils.Coords;
|
|||||||
public class City {
|
public class City {
|
||||||
private String cityName;
|
private String cityName;
|
||||||
private Coords cityCoords;
|
private Coords cityCoords;
|
||||||
JSONFetcherInterface JSONFetcher = new JSONFetcher();
|
JSONFetcher JSONFetcher = new JSONFetcher();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch data from adresse.data.gouv.fr
|
* Fetch data from adresse.data.gouv.fr
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcher;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
@ -24,7 +22,7 @@ import eirb.pg203.WeatherData.Condition;
|
|||||||
public class OpenMeteo implements WeatherDataAPI {
|
public class OpenMeteo implements WeatherDataAPI {
|
||||||
private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast";
|
private static final String forecastBaseURL = "https://api.open-meteo.com/v1/forecast";
|
||||||
private static final String dailyQuery = "weather_code,temperature_2m_max,temperature_2m_min,wind_speed_10m_max,wind_direction_10m_dominant";
|
private static final String dailyQuery = "weather_code,temperature_2m_max,temperature_2m_min,wind_speed_10m_max,wind_direction_10m_dominant";
|
||||||
JSONFetcherInterface JSONFetcher = new JSONFetcher();
|
JSONFetcher JSONFetcher = new JSONFetcher();
|
||||||
Clock clock = Clock.systemUTC();
|
Clock clock = Clock.systemUTC();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ public class OpenWeatherMap implements WeatherDataAPI {
|
|||||||
private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast";
|
private static final String forecastBaseURL = "https://api.openweathermap.org/data/2.5/forecast";
|
||||||
private String APIKey;
|
private String APIKey;
|
||||||
Clock clock = Clock.systemUTC();
|
Clock clock = Clock.systemUTC();
|
||||||
JSONFetcherInterface JSONFetcher = new JSONFetcher();
|
JSONFetcher JSONFetcher = new JSONFetcher();
|
||||||
|
|
||||||
OpenWeatherMap(String APIKey) {
|
OpenWeatherMap(String APIKey) {
|
||||||
this.APIKey = APIKey;
|
this.APIKey = APIKey;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -19,7 +18,7 @@ import java.util.Locale;
|
|||||||
*/
|
*/
|
||||||
public class WeatherAPI implements WeatherDataAPI{
|
public class WeatherAPI implements WeatherDataAPI{
|
||||||
private final String weatherAPIKey;
|
private final String weatherAPIKey;
|
||||||
JSONFetcherInterface JSONFetcher = new JSONFetcher();
|
JSONFetcher JSONFetcher = new JSONFetcher();
|
||||||
private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json";
|
private static final String forecastBaseURL = "https://api.weatherapi.com/v1/forecast.json";
|
||||||
|
|
||||||
WeatherAPI(String weatherAPIKey) {
|
WeatherAPI(String weatherAPIKey) {
|
||||||
|
@ -12,7 +12,7 @@ import org.json.JSONObject;
|
|||||||
/**
|
/**
|
||||||
* Util for http calls
|
* Util for http calls
|
||||||
*/
|
*/
|
||||||
public class JSONFetcher implements JSONFetcherInterface{
|
public class JSONFetcher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* No need for constructor
|
* No need for constructor
|
||||||
@ -44,7 +44,6 @@ public class JSONFetcher implements JSONFetcherInterface{
|
|||||||
* @return Json object of the response
|
* @return Json object of the response
|
||||||
* @throws IOException if the request failed
|
* @throws IOException if the request failed
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public JSONObject fetch(URL url) throws IOException {
|
public JSONObject fetch(URL url) throws IOException {
|
||||||
String result = fetchString(url);
|
String result = fetchString(url);
|
||||||
|
|
||||||
@ -57,7 +56,6 @@ public class JSONFetcher implements JSONFetcherInterface{
|
|||||||
* @return JSON array
|
* @return JSON array
|
||||||
* @throws IOException when request failed
|
* @throws IOException when request failed
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
public JSONArray fetchArray(URL url) throws IOException {
|
public JSONArray fetchArray(URL url) throws IOException {
|
||||||
String result = fetchString(url);
|
String result = fetchString(url);
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package eirb.pg203.utils;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface use to be overrided by a fake jsonFetcher during tests
|
|
||||||
*/
|
|
||||||
public interface JSONFetcherInterface {
|
|
||||||
JSONObject fetch(URL url) throws IOException;
|
|
||||||
|
|
||||||
JSONArray fetchArray(URL url) throws IOException;
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ package eirb.pg203.fakeJSONFetcher;
|
|||||||
|
|
||||||
import eirb.pg203.utils.FileResourcesUtils;
|
import eirb.pg203.utils.FileResourcesUtils;
|
||||||
import eirb.pg203.utils.JSONFetcher;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
|
||||||
import eirb.pg203.utils.SplitQueryUrl;
|
import eirb.pg203.utils.SplitQueryUrl;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -13,7 +12,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class FakeJSONFetcherCity implements JSONFetcherInterface {
|
public class FakeJSONFetcherCity extends JSONFetcher{
|
||||||
JSONObject unknownCity = FileResourcesUtils.getFileFromResourceAsJson("City/fakeCity.json");
|
JSONObject unknownCity = FileResourcesUtils.getFileFromResourceAsJson("City/fakeCity.json");
|
||||||
|
|
||||||
private static HashMap<String, JSONObject> cities(){
|
private static HashMap<String, JSONObject> cities(){
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
package eirb.pg203.fakeJSONFetcher;
|
package eirb.pg203.fakeJSONFetcher;
|
||||||
|
|
||||||
import eirb.pg203.utils.FileResourcesUtils;
|
import eirb.pg203.utils.FileResourcesUtils;
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
import eirb.pg203.utils.SplitQueryUrl;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class FakeJSONFetcherOpenMeteo implements JSONFetcherInterface {
|
public class FakeJSONFetcherOpenMeteo extends JSONFetcher {
|
||||||
private JSONObject responseExample() {
|
private JSONObject responseExample() {
|
||||||
return FileResourcesUtils.getFileFromResourceAsJson("OpenMeteo/Bordeaux-partial-cloudy-rain-sunny.json");
|
return FileResourcesUtils.getFileFromResourceAsJson("OpenMeteo/Bordeaux-partial-cloudy-rain-sunny.json");
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
package eirb.pg203.fakeJSONFetcher;
|
package eirb.pg203.fakeJSONFetcher;
|
||||||
|
|
||||||
import eirb.pg203.utils.FileResourcesUtils;
|
import eirb.pg203.utils.FileResourcesUtils;
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
import eirb.pg203.utils.SplitQueryUrl;
|
import eirb.pg203.utils.SplitQueryUrl;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class FakeJSONFetcherOpenWeatherMap implements JSONFetcherInterface {
|
public class FakeJSONFetcherOpenWeatherMap extends JSONFetcher {
|
||||||
private final String apiKey = "realKey";
|
private final String apiKey = "realKey";
|
||||||
private final JSONObject wrongKeyResponse = FileResourcesUtils.getFileFromResourceAsJson("OpenWeatherMap/wrong-apikey.json");
|
private final JSONObject wrongKeyResponse = FileResourcesUtils.getFileFromResourceAsJson("OpenWeatherMap/wrong-apikey.json");
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package eirb.pg203.fakeJSONFetcher;
|
package eirb.pg203.fakeJSONFetcher;
|
||||||
|
|
||||||
import eirb.pg203.utils.FileResourcesUtils;
|
import eirb.pg203.utils.FileResourcesUtils;
|
||||||
import eirb.pg203.utils.JSONFetcherInterface;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
import eirb.pg203.utils.SplitQueryUrl;
|
import eirb.pg203.utils.SplitQueryUrl;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@ -11,7 +11,7 @@ import java.net.URL;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class FakeJSONFetcherWeatherAPI implements JSONFetcherInterface {
|
public class FakeJSONFetcherWeatherAPI extends JSONFetcher {
|
||||||
private final static String baseUrlFormat = "https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d";
|
private final static String baseUrlFormat = "https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d";
|
||||||
private final String apiKey = "realKey";
|
private final String apiKey = "realKey";
|
||||||
private static final JSONObject wrongKeyRequest = FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/wrong-apikey.json");
|
private static final JSONObject wrongKeyRequest = FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/wrong-apikey.json");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user