diff --git a/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java b/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java index 8a7b7a4..9ea8f0b 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/DailyForecastRequester.java @@ -26,7 +26,7 @@ import com.github.prominence.openweathermap.api.constants.Unit; import com.github.prominence.openweathermap.api.exception.DataNotFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.DailyForecast; -import com.github.prominence.openweathermap.api.utils.JsonUtils; +import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.io.IOException; @@ -69,7 +69,7 @@ public class DailyForecastRequester extends BasicRequester { try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - forecastResponse = (DailyForecast)JsonUtils.parseJson(requestResult, DailyForecast.class); + forecastResponse = (DailyForecast)JSONUtils.parseJSON(requestResult, DailyForecast.class); char temperatureUnit = Unit.getTemperatureUnit(unitSystem); String windUnit = Unit.getWindUnit(unitSystem); diff --git a/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java b/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java index 6841376..220684e 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/HourlyForecastRequester.java @@ -26,7 +26,7 @@ import com.github.prominence.openweathermap.api.constants.Unit; import com.github.prominence.openweathermap.api.exception.DataNotFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.HourlyForecast; -import com.github.prominence.openweathermap.api.utils.JsonUtils; +import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.io.IOException; @@ -63,7 +63,7 @@ public class HourlyForecastRequester extends BasicRequester { try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - forecastResponse = (HourlyForecast)JsonUtils.parseJson(requestResult, HourlyForecast.class); + forecastResponse = (HourlyForecast)JSONUtils.parseJSON(requestResult, HourlyForecast.class); char temperatureUnit = Unit.getTemperatureUnit(unitSystem); String windUnit = Unit.getWindUnit(unitSystem); diff --git a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java index 6b29743..c051402 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapManager.java @@ -41,4 +41,8 @@ public class OpenWeatherMapManager { public DailyForecastRequester getDailyForecastRequester() { return new DailyForecastRequester(authToken); } + + public UltravioletIndexRequester getUltravioletIndexRequester() { + return new UltravioletIndexRequester(authToken); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java b/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java new file mode 100644 index 0000000..82b7ec9 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/UltravioletIndexRequester.java @@ -0,0 +1,113 @@ +/* + * 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 com.github.prominence.openweathermap.api; + +import com.alibaba.fastjson.TypeReference; +import com.github.prominence.openweathermap.api.constants.System; +import com.github.prominence.openweathermap.api.exception.DataNotFoundException; +import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; +import com.github.prominence.openweathermap.api.model.Coordinates; +import com.github.prominence.openweathermap.api.model.response.UltravioletIndex; +import com.github.prominence.openweathermap.api.utils.JSONUtils; +import com.github.prominence.openweathermap.api.utils.RequestUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Date; +import java.util.List; + +public class UltravioletIndexRequester extends AuthenticationTokenBasedRequester { + + private Coordinates coordinates; + + UltravioletIndexRequester(String authToken) { + super(authToken); + } + + public UltravioletIndexRequester setCoordinates(Coordinates coordinates) { + this.coordinates = coordinates; + return this; + } + + public UltravioletIndexRequester setCoordinates(float latitude, float longitude) { + this.coordinates = new Coordinates(latitude, longitude); + return this; + } + + public UltravioletIndex getCurrentUVIndex() throws InvalidAuthTokenException, DataNotFoundException { + String requestParameters = String.format("lat=%f&lon=%f", coordinates.getLatitude(), coordinates.getLongitude()); + return getSingleObject(requestParameters); + } + + public List getUVIndexForecast(int amountOfDays) throws InvalidAuthTokenException, DataNotFoundException { + String requestParameters = String.format("lat=%f&lon=%f&cnt=%d", coordinates.getLatitude(), coordinates.getLongitude(), amountOfDays); + return getListOfObjects(requestParameters); + } + + public List getUVIndexByPeriod(Date from, Date to) throws InvalidAuthTokenException, DataNotFoundException { + String requestParameters = String.format("lat=%f&lon=%f&start=%d&end=%d", coordinates.getLatitude(), coordinates.getLongitude(), from.getTime() / 1000, to.getTime() / 1000); + return getListOfObjects(requestParameters); + } + + private UltravioletIndex getSingleObject(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException { + UltravioletIndex ultravioletIndex = null; + + try (InputStream response = executeRequest("uvi", requestParameters)) { + ultravioletIndex = (UltravioletIndex) JSONUtils.parseJSON(response, UltravioletIndex.class); + } catch (IOException ex) { + ex.printStackTrace(); + } + + return ultravioletIndex; + } + + private List getListOfObjects(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException { + List ultravioletIndex = null; + + TypeReference> typeRef = new TypeReference>() {}; + + try (InputStream response = executeRequest("uvi/forecast", requestParameters)) { + ultravioletIndex = (List) JSONUtils.parseJSON(response, typeRef); + } catch (IOException ex) { + ex.printStackTrace(); + } + + return ultravioletIndex; + } + + private InputStream executeRequest(String requestType, String requestSpecificParameters) throws MalformedURLException, InvalidAuthTokenException, DataNotFoundException { + + StringBuilder urlBuilder = new StringBuilder(System.OPEN_WEATHER_API_URL); + urlBuilder.append(requestType); + urlBuilder.append('?'); + urlBuilder.append(requestSpecificParameters); + + urlBuilder.append("&appid="); + urlBuilder.append(authToken); + + return RequestUtils.executeGetRequest(new URL(urlBuilder.toString())); + } + +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java index 08ab60b..e874cb8 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/WeatherRequester.java @@ -26,7 +26,7 @@ import com.github.prominence.openweathermap.api.constants.Unit; import com.github.prominence.openweathermap.api.exception.DataNotFoundException; import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; import com.github.prominence.openweathermap.api.model.response.Weather; -import com.github.prominence.openweathermap.api.utils.JsonUtils; +import com.github.prominence.openweathermap.api.utils.JSONUtils; import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.io.IOException; @@ -63,7 +63,7 @@ public class WeatherRequester extends BasicRequester { try { InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters)); - weather = (Weather)JsonUtils.parseJson(requestResult, Weather.class); + weather = (Weather)JSONUtils.parseJSON(requestResult, Weather.class); weather.getWind().setUnit(Unit.getWindUnit(unitSystem)); weather.getWeatherInfo().setTemperatureUnit(Unit.getTemperatureUnit(unitSystem)); diff --git a/src/main/java/com/github/prominence/openweathermap/api/model/response/UltravioletIndex.java b/src/main/java/com/github/prominence/openweathermap/api/model/response/UltravioletIndex.java new file mode 100644 index 0000000..d213d37 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/model/response/UltravioletIndex.java @@ -0,0 +1,67 @@ +/* + * 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 com.github.prominence.openweathermap.api.model.response; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import java.time.Instant; +import java.util.Date; + +@EqualsAndHashCode +public class UltravioletIndex { + + @JSONField(name = "lat") + @Getter + @Setter + float latitude; + + @JSONField(name = "lon") + @Getter + @Setter + float longitude; + + @JSONField(name = "date_iso") + @Getter + @Setter + String dateISO; + + @JSONField(name = "date") + @Getter + @Setter + int dateTimestamp; + + @Getter + @Setter + float value; + + public Date getCalculationDate() { + return Date.from(Instant.ofEpochSecond(dateTimestamp)); + } + + public String toString() { + return String.format("Date: %s, Ultraviolet value: %f", getCalculationDate(), value); + } +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/utils/JsonUtils.java b/src/main/java/com/github/prominence/openweathermap/api/utils/JSONUtils.java similarity index 74% rename from src/main/java/com/github/prominence/openweathermap/api/utils/JsonUtils.java rename to src/main/java/com/github/prominence/openweathermap/api/utils/JSONUtils.java index de2379b..ebc8abc 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/utils/JsonUtils.java +++ b/src/main/java/com/github/prominence/openweathermap/api/utils/JSONUtils.java @@ -23,17 +23,26 @@ package com.github.prominence.openweathermap.api.utils; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -public final class JsonUtils { +public final class JSONUtils { - private JsonUtils() {} + private JSONUtils() {} - public static Object parseJson(InputStream inputStream, Class clazz) throws IOException { + public static Object parseJSON(InputStream inputStream, Class clazz) throws IOException { + return JSON.parseObject(getStringFromStream(inputStream), clazz); + } + + public static Object parseJSON(InputStream inputStream, TypeReference typeReference) throws IOException { + return JSON.parseObject(getStringFromStream(inputStream), typeReference); + } + + private static String getStringFromStream(InputStream inputStream) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); @@ -44,6 +53,6 @@ public final class JsonUtils { reader.close(); - return JSON.parseObject(result.toString(), clazz); + return result.toString(); } }