diff --git a/src/main/java/by/prominence/openweather/api/constants/Accuracy.java b/src/main/java/by/prominence/openweather/api/constants/Accuracy.java new file mode 100644 index 0000000..0f9f30e --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/constants/Accuracy.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.constants; + +public class Accuracy { + + public static final String LIKE = "like"; + public static final String ACCURATE = "accurate"; + +} diff --git a/src/main/java/by/prominence/openweather/api/constants/Language.java b/src/main/java/by/prominence/openweather/api/constants/Language.java new file mode 100644 index 0000000..95f47b8 --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/constants/Language.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.constants; + +public class Language { + + public static final String ARABIC = "ar"; + public static final String BULGARIAN = "bg"; + public static final String CATALAN = "ca"; + public static final String CZECH = "cz"; + public static final String GERMAN = "de"; + public static final String GREEK = "el"; + public static final String ENGLISH = "en"; + public static final String PERSIAN = "fa"; + public static final String FINNISH = "fi"; + public static final String FRENCH = "fr"; + public static final String GALICIAN = "gl"; + public static final String CROATIAN = "hr"; + public static final String HUNGARIAN = "hu"; + public static final String ITALIAN = "it"; + public static final String JAPANESE = "ja"; + public static final String KOREAN = "kr"; + public static final String LATVIAN = "la"; + public static final String LITHUANIAN = "lt"; + public static final String MACEDONIAN = "mk"; + public static final String DUTCH = "nl"; + public static final String POLISH = "pl"; + public static final String PORTUGUESE = "pt"; + public static final String ROMANIAN ="ro"; + public static final String RUSSIAN = "ru"; + public static final String SWEDISH = "se"; + public static final String SLOVAK = "sk"; + public static final String SLOVENIAN = "sl"; + public static final String SPANISH = "en"; + public static final String TURKISH = "tr"; + public static final String UKRANIAN = "uk"; + public static final String VIETNAMESE = "vi"; + public static final String CHINESE_SIMPLIFIED = "zh_cn"; + public static final String CHINESE_TRADITIONAL = "zh_tw"; + +} diff --git a/src/main/java/by/prominence/openweather/api/constants/Unit.java b/src/main/java/by/prominence/openweather/api/constants/Unit.java new file mode 100644 index 0000000..dd7b65f --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/constants/Unit.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.constants; + +public class Unit { + + public static final String METRIC = "metric"; + public static final String IMPERIAL = "imperial"; + public static final String STANDARD = null; + +} diff --git a/src/main/java/by/prominence/openweather/api/exception/InvalidAuthTokenException.java b/src/main/java/by/prominence/openweather/api/exception/InvalidAuthTokenException.java new file mode 100644 index 0000000..b7dca0f --- /dev/null +++ b/src/main/java/by/prominence/openweather/api/exception/InvalidAuthTokenException.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018 Alexey Zinchenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package by.prominence.openweather.api.exception; + +public class InvalidAuthTokenException extends Exception { + + public InvalidAuthTokenException() { + super("Please, check you authentication token!"); + } + + public InvalidAuthTokenException(String message) { + super(message); + } +} diff --git a/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java b/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java index b62ec29..d6dc78b 100644 --- a/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java +++ b/src/main/java/by/prominence/openweather/api/provider/DefaultWeatherProvider.java @@ -22,6 +22,7 @@ package by.prominence.openweather.api.provider; +import by.prominence.openweather.api.exception.InvalidAuthTokenException; import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.WeatherResponse; import by.prominence.openweather.api.utils.JsonUtils; @@ -39,34 +40,65 @@ public class DefaultWeatherProvider implements WeatherProvider { private final String authToken; + private String language; + private String unit; + private String accuracy; + public DefaultWeatherProvider(String authToken) { this.authToken = authToken; } - public WeatherResponse getByCityId(String id) { + public WeatherProvider setLanguage(String language) { + this.language = language; + return this; + } + + public WeatherProvider setUnit(String unit) { + this.unit = unit; + return this; + } + + public WeatherProvider setAccuracy(String accuracy) { + this.accuracy = accuracy; + return this; + } + + public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException { return executeRequest("?id=" + id); } - public WeatherResponse getByCityName(String name) { + public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException { return executeRequest("?q=" + name); } - public WeatherResponse getByCoordinates(double latitude, double longitude) { + public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException { return executeRequest("?lat=" + latitude + "&lon=" + longitude); } - public WeatherResponse getByCoordinates(Coordinates coordinates) { + public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException { return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude()); } - public WeatherResponse getByZIPCode(String zipCode, String countryCode) { + public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException { return executeRequest("?zip=" + zipCode + "," + countryCode); } - private WeatherResponse executeRequest(String parameterString) { + private WeatherResponse executeRequest(String parameterString) throws InvalidAuthTokenException { String url = OPEN_WEATHER_API_URL + parameterString + "&appid=" + authToken; + if (language != null) { + url += "&lang=" + language; + } + + if (unit != null) { + url += "&units=" + unit; + } + + if (accuracy != null) { + url += "&type=" + accuracy; + } + HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); HttpResponse response = null; @@ -81,6 +113,10 @@ public class DefaultWeatherProvider implements WeatherProvider { if (response != null) { try { weatherResponse = (WeatherResponse)JsonUtils.parseJson(response.getEntity().getContent(), WeatherResponse.class); + + if (weatherResponse.getResponseCode() == 401) { + throw new InvalidAuthTokenException(); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java b/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java index 1e66303..8a3d9df 100644 --- a/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java +++ b/src/main/java/by/prominence/openweather/api/provider/WeatherProvider.java @@ -22,15 +22,20 @@ package by.prominence.openweather.api.provider; +import by.prominence.openweather.api.exception.InvalidAuthTokenException; import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.WeatherResponse; public interface WeatherProvider { - public WeatherResponse getByCityId(String id); - public WeatherResponse getByCityName(String name); - public WeatherResponse getByCoordinates(double latitude, double longitude); - public WeatherResponse getByCoordinates(Coordinates coordinates); - public WeatherResponse getByZIPCode(String zipCode, String countryCode); + public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException; + public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException; + public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException; + public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException; + public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException; + + public WeatherProvider setLanguage(String language); + public WeatherProvider setUnit(String unit); + public WeatherProvider setAccuracy(String accuracy); }