Implemented UltravioletIndex retrieving. Renamed JSON utulity class.

This commit is contained in:
Prominence 2018-07-29 18:55:05 +03:00
parent 20adced4d7
commit e5dee248b6
7 changed files with 203 additions and 10 deletions

View File

@ -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<DailyForecast> {
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);

View File

@ -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<HourlyForecast> {
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);

View File

@ -41,4 +41,8 @@ public class OpenWeatherMapManager {
public DailyForecastRequester getDailyForecastRequester() {
return new DailyForecastRequester(authToken);
}
public UltravioletIndexRequester getUltravioletIndexRequester() {
return new UltravioletIndexRequester(authToken);
}
}

View File

@ -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<UltravioletIndex> 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<UltravioletIndex> 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<UltravioletIndex> getListOfObjects(String requestParameters) throws InvalidAuthTokenException, DataNotFoundException {
List<UltravioletIndex> ultravioletIndex = null;
TypeReference<List<UltravioletIndex>> typeRef = new TypeReference<List<UltravioletIndex>>() {};
try (InputStream response = executeRequest("uvi/forecast", requestParameters)) {
ultravioletIndex = (List<UltravioletIndex>) 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()));
}
}

View File

@ -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<Weather> {
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));

View File

@ -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);
}
}

View File

@ -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();
}
}