mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-10 11:56:44 +03:00
Added possibility to define number of days for forecast. Code refactoring.
This commit is contained in:
parent
038c62ad14
commit
c087eb5aed
13
pom.xml
13
pom.xml
@ -8,6 +8,19 @@
|
|||||||
<artifactId>by.prominence.openweater.api</artifactId>
|
<artifactId>by.prominence.openweater.api</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
|
|||||||
@ -24,7 +24,6 @@ package by.prominence.openweather.api;
|
|||||||
|
|
||||||
import by.prominence.openweather.api.provider.ForecastProvider;
|
import by.prominence.openweather.api.provider.ForecastProvider;
|
||||||
import by.prominence.openweather.api.provider.WeatherProvider;
|
import by.prominence.openweather.api.provider.WeatherProvider;
|
||||||
import by.prominence.openweather.api.provider.OpenWeatherProvider;
|
|
||||||
|
|
||||||
public class OpenWeatherProviderManager {
|
public class OpenWeatherProviderManager {
|
||||||
|
|
||||||
@ -34,11 +33,16 @@ public class OpenWeatherProviderManager {
|
|||||||
this.authToken = token;
|
this.authToken = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenWeatherProvider getWeather() {
|
public WeatherProvider getWeather() {
|
||||||
return new WeatherProvider(authToken);
|
return new WeatherProvider(authToken);
|
||||||
}
|
}
|
||||||
public OpenWeatherProvider getForecast() {
|
|
||||||
|
public ForecastProvider getForecast() {
|
||||||
return new ForecastProvider(authToken);
|
return new ForecastProvider(authToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ForecastProvider getForecast(int amountOfDays) {
|
||||||
|
return new ForecastProvider(authToken, amountOfDays);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ package by.prominence.openweather.api.exception;
|
|||||||
public class InvalidAuthTokenException extends Exception {
|
public class InvalidAuthTokenException extends Exception {
|
||||||
|
|
||||||
public InvalidAuthTokenException() {
|
public InvalidAuthTokenException() {
|
||||||
super("Please, check you authentication token! You can get it here: https://home.openweathermap.org/api_keys/.");
|
super("Check you authentication token! You can get it here: https://home.openweathermap.org/api_keys/. Please, notice that some requests requires non-free subscription plan.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public InvalidAuthTokenException(String message) {
|
public InvalidAuthTokenException(String message) {
|
||||||
|
|||||||
@ -29,9 +29,12 @@ import by.prominence.openweather.api.model.Coordinates;
|
|||||||
import by.prominence.openweather.api.model.OpenWeatherResponse;
|
import by.prominence.openweather.api.model.OpenWeatherResponse;
|
||||||
import by.prominence.openweather.api.utils.JsonUtils;
|
import by.prominence.openweather.api.utils.JsonUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse> implements OpenWeatherProvider {
|
public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse> implements OpenWeatherProvider {
|
||||||
|
|
||||||
@ -82,53 +85,71 @@ public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse>
|
|||||||
return executeRequest("?zip=" + zipCode + "," + countryCode);
|
return executeRequest("?zip=" + zipCode + "," + countryCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected T executeRequest(String parameterString) throws InvalidAuthTokenException, DataNotFoundException {
|
protected T executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
|
||||||
|
|
||||||
String url = getRequestUrl() + parameterString + "&appid=" + authToken;
|
|
||||||
|
|
||||||
if (language != null) {
|
|
||||||
url += "&lang=" + language;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unit != null) {
|
|
||||||
url += "&units=" + unit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (accuracy != null) {
|
|
||||||
url += "&type=" + accuracy;
|
|
||||||
}
|
|
||||||
|
|
||||||
T openWeatherResponse = null;
|
T openWeatherResponse = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
URL requestUrl = new URL(url);
|
URL requestUrl = buildURL(requestSpecificParameters);
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
|
|
||||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
switch (connection.getResponseCode()) {
|
||||||
openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type));
|
case HttpURLConnection.HTTP_OK:
|
||||||
|
openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type));
|
||||||
if (openWeatherResponse.getResponseCode() == 401) {
|
break;
|
||||||
|
case HttpURLConnection.HTTP_UNAUTHORIZED:
|
||||||
throw new InvalidAuthTokenException();
|
throw new InvalidAuthTokenException();
|
||||||
}
|
case HttpURLConnection.HTTP_NOT_FOUND:
|
||||||
|
|
||||||
if (openWeatherResponse.getResponseCode() == 404) {
|
|
||||||
throw new DataNotFoundException();
|
throw new DataNotFoundException();
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new DataNotFoundException();
|
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (IOException | ClassCastException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return openWeatherResponse;
|
return openWeatherResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getRequestUrl() {
|
protected String getRequestUrl() {
|
||||||
return System.OPEN_WEATHER_API_URL + getRequestType();
|
return System.OPEN_WEATHER_API_URL + getRequestType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Map<String, String> getAdditionalParameters() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private URL buildURL(String requestSpecificParameters) throws MalformedURLException {
|
||||||
|
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(getRequestUrl() + requestSpecificParameters + "&appid=" + authToken);
|
||||||
|
|
||||||
|
if (language != null) {
|
||||||
|
urlBuilder.append("&lang=");
|
||||||
|
urlBuilder.append(language);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unit != null) {
|
||||||
|
urlBuilder.append("&units=");
|
||||||
|
urlBuilder.append(unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accuracy != null) {
|
||||||
|
urlBuilder.append("&type=");
|
||||||
|
urlBuilder.append(accuracy);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> additionalParameters = getAdditionalParameters();
|
||||||
|
if (additionalParameters != null) {
|
||||||
|
additionalParameters.forEach((key, value) -> {
|
||||||
|
urlBuilder.append("&");
|
||||||
|
urlBuilder.append(key);
|
||||||
|
urlBuilder.append("=");
|
||||||
|
urlBuilder.append(value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new URL(urlBuilder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract String getRequestType();
|
protected abstract String getRequestType();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,13 +24,37 @@ package by.prominence.openweather.api.provider;
|
|||||||
|
|
||||||
import by.prominence.openweather.api.model.forecast.ForecastResponse;
|
import by.prominence.openweather.api.model.forecast.ForecastResponse;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ForecastProvider extends AbstractOpenWeatherProvider<ForecastResponse> {
|
public class ForecastProvider extends AbstractOpenWeatherProvider<ForecastResponse> {
|
||||||
|
|
||||||
|
private int amountOfDays = -1;
|
||||||
|
|
||||||
public ForecastProvider(String authToken) {
|
public ForecastProvider(String authToken) {
|
||||||
super(authToken);
|
super(authToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ForecastProvider(String authToken, int amountOfDays) {
|
||||||
|
super(authToken);
|
||||||
|
this.amountOfDays = amountOfDays;
|
||||||
|
}
|
||||||
|
|
||||||
protected String getRequestType() {
|
protected String getRequestType() {
|
||||||
|
if (amountOfDays != -1) {
|
||||||
|
return "forecast/daily";
|
||||||
|
}
|
||||||
return "forecast";
|
return "forecast";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<String, String> getAdditionalParameters() {
|
||||||
|
Map<String, String> additionalParameters = null;
|
||||||
|
if (amountOfDays != -1) {
|
||||||
|
additionalParameters = new HashMap<>();
|
||||||
|
additionalParameters.put("cnt", String.valueOf(amountOfDays));
|
||||||
|
}
|
||||||
|
|
||||||
|
return additionalParameters;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user