Implemented Air Pollution retrieving.

This commit is contained in:
2018-08-02 21:34:41 +03:00
parent 0487e0f2d0
commit 39d56b6782
7 changed files with 359 additions and 1 deletions
@@ -0,0 +1,98 @@
/*
* 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.github.prominence.openweathermap.api.constants.TimeFrame;
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.AirPollution;
import com.github.prominence.openweathermap.api.utils.JSONUtils;
import com.github.prominence.openweathermap.api.utils.RequestUtils;
import com.github.prominence.openweathermap.api.utils.TimeFrameUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
public class AirPollutionRequester extends AuthenticationTokenBasedRequester {
private Coordinates coordinates;
private TimeFrame timeFrame;
private Date date;
AirPollutionRequester(String authToken) {
super(authToken);
}
public AirPollutionRequester setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
return this;
}
public AirPollutionRequester setCoordinates(float latitude, float longitude) {
this.coordinates = new Coordinates(latitude, longitude);
return this;
}
public AirPollutionRequester setTimeFrame(TimeFrame timeFrame) {
this.timeFrame = timeFrame;
return this;
}
public AirPollutionRequester setDate(Date date) {
this.date = date;
return this;
}
public AirPollution retrieve() throws InvalidAuthTokenException, DataNotFoundException {
if (coordinates == null || timeFrame == null || date == null) {
throw new IllegalArgumentException("You must execute 'setCoordinates', 'setTimeFrame' and 'setDate' and least once.");
}
String requestParameters = String.format("%s,%s/%s.json", coordinates.getLatitude(), coordinates.getLongitude(), TimeFrameUtils.formatDate(date, timeFrame));
AirPollution airPollution = null;
try (InputStream response = executeRequest("pollution/v1/co/", requestParameters)) {
airPollution = (AirPollution) JSONUtils.parseJSON(response, AirPollution.class);
} catch (IOException ex) {
ex.printStackTrace();
}
return airPollution;
}
private InputStream executeRequest(String requestType, String requestSpecificParameters) throws MalformedURLException, InvalidAuthTokenException, DataNotFoundException {
String url = OPEN_WEATHER_BASE_URL + requestType +
requestSpecificParameters +
"?appid=" +
authToken;
return RequestUtils.executeGetRequest(new URL(url));
}
}
@@ -25,7 +25,8 @@ package com.github.prominence.openweathermap.api;
abstract class AuthenticationTokenBasedRequester {
protected static final String OPEN_WEATHER_API_VERSION = "2.5";
protected static final String OPEN_WEATHER_API_URL = "http://api.openweathermap.org/data/" + OPEN_WEATHER_API_VERSION + "/";
protected static final String OPEN_WEATHER_BASE_URL = "http://api.openweathermap.org/";
protected static final String OPEN_WEATHER_API_URL = OPEN_WEATHER_BASE_URL + "data/" + OPEN_WEATHER_API_VERSION + "/";
protected String authToken;
@@ -45,4 +45,8 @@ public class OpenWeatherMapManager {
public UltravioletIndexRequester getUltravioletIndexRequester() {
return new UltravioletIndexRequester(authToken);
}
public AirPollutionRequester getAirPollutionRequester() {
return new AirPollutionRequester(authToken);
}
}
@@ -0,0 +1,33 @@
/*
* 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.constants;
public enum TimeFrame {
YEAR,
MONTH,
DAY,
HOUR,
MINUTE,
SECOND
}
@@ -0,0 +1,105 @@
/*
* 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@EqualsAndHashCode
public class AirPollution {
@Getter
@Setter
private String time;
@Getter
@Setter
@JSONField(name = "location")
private Coordinates coordinates;
@JSONField(name = "data")
@Getter
@Setter
private List<AirPollutionInfo> airPollutionInfo;
public Date getCalculationDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
return format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
return "AirPollution[Date: " + getCalculationDate() + "; Coordinates: " + coordinates + "]" + "\n" + airPollutionInfo;
}
@EqualsAndHashCode
public static class AirPollutionInfo {
@Getter
@Setter
private float precision;
@Getter
@Setter
private short pressure;
@Getter
@Setter
private float value;
@Override
public String toString() {
return "Value: " + value;
}
}
@EqualsAndHashCode
public static class Coordinates {
@Getter
@Setter
private float latitude;
@Getter
@Setter
private float longitude;
@Override
public String toString() {
return "latitude=" + latitude + ", longitude=" + longitude;
}
}
}
@@ -0,0 +1,82 @@
/*
* 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.utils;
import com.github.prominence.openweathermap.api.constants.TimeFrame;
import java.text.SimpleDateFormat;
import java.util.Date;
public final class TimeFrameUtils {
private TimeFrameUtils() {
}
/*
2016-01-02T15:04:05Z
searches between 2016-01-02T15:04:05Z and 2016-01-02T15:04:05.9999Z
2016-01-02T15:04Z
searches between 2016-01-02T15:04:00Z and 2016-01-02T15:04:59.9999Z
2016-01-02T15Z
searches between 2016-01-02T15:00:00Z and 2016-01-02T15:59:59.9999Z
2016-01-02Z
searches between 2016-01-02T00:00:00Z and 2016-01-02T23:59:59.9999Z
2016-01Z
searches between 2016-01-01T00:00:00Z and 2016-12-31T23:59:59.9999Z
2016Z
searches between 2016-01-01T00:00:00Z and 2016-12-31T23:59:99.9999Z
*/
public static String formatDate(Date date, TimeFrame timeFrame) {
SimpleDateFormat formatter;
switch (timeFrame) {
case YEAR:
formatter = new SimpleDateFormat("yyyy'Z'");
break;
case MONTH:
formatter = new SimpleDateFormat("yyyy-MM'Z'");
break;
case DAY:
formatter = new SimpleDateFormat("yyyy-MM-dd'Z'");
break;
case HOUR:
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH'Z'");
break;
case MINUTE:
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
break;
case SECOND:
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
break;
default:
formatter = new SimpleDateFormat("yyyy-MM'Z'");
}
return formatter.format(date);
}
}