feat: fake jsonfetcher

This commit is contained in:
Martin Eyben 2024-11-23 10:42:04 +01:00
parent 70c573eb81
commit 52e63be79c
6 changed files with 31 additions and 5 deletions

View File

@ -9,6 +9,7 @@ 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.JSONObject; import org.json.JSONObject;
@ -21,6 +22,7 @@ import eirb.pg203.utils.Coords;
public class City { public class City {
private String cityName; private String cityName;
private Coords cityCoords; private Coords cityCoords;
private static final JSONFetcherInterface JSONFetcher = new JSONFetcher();
/** /**
* Fetch data from adresse.data.gouv.fr * Fetch data from adresse.data.gouv.fr

View File

@ -1,5 +1,6 @@
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;
@ -22,6 +23,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";
private static final JSONFetcherInterface JSONFetcher = new JSONFetcher();
/** /**
* Default constructor * Default constructor

View File

@ -1,5 +1,6 @@
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;
@ -22,6 +23,7 @@ import eirb.pg203.WeatherData.Condition;
public class OpenWeatherMap implements WeatherDataAPI { 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;
private static final JSONFetcherInterface JSONFetcher = new JSONFetcher();
OpenWeatherMap(String APIKey) { OpenWeatherMap(String APIKey) {
this.APIKey = APIKey; this.APIKey = APIKey;

View File

@ -1,5 +1,6 @@
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;
@ -18,6 +19,7 @@ import java.util.Locale;
*/ */
public class WeatherAPI implements WeatherDataAPI{ public class WeatherAPI implements WeatherDataAPI{
private final String weatherAPIKey; private final String weatherAPIKey;
private static final JSONFetcherInterface 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) {

View File

@ -12,12 +12,12 @@ import org.json.JSONObject;
/** /**
* Util for http calls * Util for http calls
*/ */
public class JSONFetcher { public class JSONFetcher implements JSONFetcherInterface{
/** /**
* No need for constructor * No need for constructor
*/ */
private JSONFetcher() {}; public JSONFetcher() {};
/** /**
* Make the request * Make the request
* @param url url to fetch * @param url url to fetch
@ -44,7 +44,8 @@ public class JSONFetcher {
* @return Json object of the response * @return Json object of the response
* @throws IOException if the request failed * @throws IOException if the request failed
*/ */
public static JSONObject fetch(URL url) throws IOException { @Override
public JSONObject fetch(URL url) throws IOException {
String result = fetchString(url); String result = fetchString(url);
return new JSONObject(result); return new JSONObject(result);
@ -56,7 +57,8 @@ public class JSONFetcher {
* @return JSON array * @return JSON array
* @throws IOException when request failed * @throws IOException when request failed
*/ */
public static JSONArray fetchArray(URL url) throws IOException { @Override
public JSONArray fetchArray(URL url) throws IOException {
String result = fetchString(url); String result = fetchString(url);
return new JSONArray(result); return new JSONArray(result);

View File

@ -0,0 +1,16 @@
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;
}