mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-10 11:56:44 +03:00
Implemented locale, unit and accuracy changing. Added InvalidAuthTokenException.
This commit is contained in:
parent
77f38e2b9a
commit
37d9a1cf5b
@ -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";
|
||||
|
||||
}
|
||||
@ -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";
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user