diff --git a/src/main/java/by/prominence/openweather/api/BasicRequester.java b/src/main/java/by/prominence/openweather/api/BasicRequester.java index 659f226..84aefa2 100644 --- a/src/main/java/by/prominence/openweather/api/BasicRequester.java +++ b/src/main/java/by/prominence/openweather/api/BasicRequester.java @@ -23,12 +23,15 @@ package by.prominence.openweather.api; import by.prominence.openweather.api.constants.System; +import by.prominence.openweather.api.exception.DataNotFoundException; +import by.prominence.openweather.api.exception.InvalidAuthTokenException; +import by.prominence.openweather.api.model.Coordinates; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; -abstract class BasicRequester extends AuthenticationTokenBasedRequester { +abstract class BasicRequester extends AuthenticationTokenBasedRequester { protected String language; protected String unit; @@ -38,6 +41,26 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { super(authToken); } + public T getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException { + return executeRequest("?id=" + id); + } + + public T getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException { + return executeRequest("?q=" + name); + } + + public T getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException { + return executeRequest("?lat=" + latitude + "&lon=" + longitude); + } + + public T getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException { + return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); + } + + public T getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException { + return executeRequest("?zip=" + zipCode + "," + countryCode); + } + protected URL buildURL(String requestSpecificParameters) throws MalformedURLException { StringBuilder urlBuilder = new StringBuilder(System.OPEN_WEATHER_API_URL); @@ -81,5 +104,6 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester { } protected abstract String getRequestType(); + protected abstract T executeRequest(String requestSpecificParamsString) throws InvalidAuthTokenException, DataNotFoundException; } diff --git a/src/main/java/by/prominence/openweather/api/ForecastRequester.java b/src/main/java/by/prominence/openweather/api/ForecastRequester.java index 576d112..535a5a6 100644 --- a/src/main/java/by/prominence/openweather/api/ForecastRequester.java +++ b/src/main/java/by/prominence/openweather/api/ForecastRequester.java @@ -24,7 +24,6 @@ package by.prominence.openweather.api; import by.prominence.openweather.api.exception.DataNotFoundException; import by.prominence.openweather.api.exception.InvalidAuthTokenException; -import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.forecast.ForecastResponse; import by.prominence.openweather.api.utils.JsonUtils; import by.prominence.openweather.api.utils.RequestUtils; @@ -34,7 +33,7 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; -public class ForecastRequester extends BasicRequester { +public class ForecastRequester extends BasicRequester { private int amountOfDays = -1; @@ -62,26 +61,6 @@ public class ForecastRequester extends BasicRequester { return this; } - public ForecastResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?id=" + id); - } - - public ForecastResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?q=" + name); - } - - public ForecastResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?lat=" + latitude + "&lon=" + longitude); - } - - public ForecastResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException { - return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); - } - - public ForecastResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?zip=" + zipCode + "," + countryCode); - } - @Override protected Map getAdditionalParameters() { Map additionalParameters = null; @@ -100,7 +79,7 @@ public class ForecastRequester extends BasicRequester { return "forecast"; } - private ForecastResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected ForecastResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); diff --git a/src/main/java/by/prominence/openweather/api/WeatherRequester.java b/src/main/java/by/prominence/openweather/api/WeatherRequester.java index ee9e903..482ace6 100644 --- a/src/main/java/by/prominence/openweather/api/WeatherRequester.java +++ b/src/main/java/by/prominence/openweather/api/WeatherRequester.java @@ -24,7 +24,6 @@ package by.prominence.openweather.api; import by.prominence.openweather.api.exception.DataNotFoundException; import by.prominence.openweather.api.exception.InvalidAuthTokenException; -import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.weather.WeatherResponse; import by.prominence.openweather.api.utils.JsonUtils; import by.prominence.openweather.api.utils.RequestUtils; @@ -32,7 +31,7 @@ import by.prominence.openweather.api.utils.RequestUtils; import java.io.IOException; import java.io.InputStream; -public class WeatherRequester extends BasicRequester { +public class WeatherRequester extends BasicRequester { WeatherRequester(String authToken) { super(authToken); @@ -53,31 +52,11 @@ public class WeatherRequester extends BasicRequester { return this; } - public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?id=" + id); - } - - public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?q=" + name); - } - - public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?lat=" + latitude + "&lon=" + longitude); - } - - public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException { - return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); - } - - public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException { - return executeRequest("?zip=" + zipCode + "," + countryCode); - } - protected String getRequestType() { return "weather"; } - private WeatherResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { + protected WeatherResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException { try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters));