package eirb.pg203.weather.fakeJSONFetcher; import eirb.pg203.weather.utils.FileResourcesUtils; import eirb.pg203.weather.utils.SplitQueryUrl; import eirb.pg203.weather.utils.JSONFetcher; import org.json.JSONArray; import org.json.JSONObject; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Map; 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 String apiKey = "realKey"; private static final JSONObject wrongKeyRequest = FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/wrong-apikey.json"); private static ArrayList bordeauxRequests() { ArrayList bordeauxRequest = new ArrayList<>(); bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-1-partial.json")); bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-2-partial-sunny.json")); bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-3-partial-sunny-rain.json")); bordeauxRequest.add(eirb.pg203.weather.utils.FileResourcesUtils.getFileFromResourceAsJson("WeatherAPI/Bordeaux-4-partial-sunny-rain-cloudy.json")); return bordeauxRequest; } @Override public JSONObject fetch(URL url) throws IOException { Map params = SplitQueryUrl.splitQuery(url); int days = Integer.parseInt(params.get("days")); if (!params.get("key").contentEquals(apiKey)) return wrongKeyRequest; return bordeauxRequests().get(days - 1); } }