mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-09 19:46:41 +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>
|
||||
<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>
|
||||
<dependency>
|
||||
<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.WeatherProvider;
|
||||
import by.prominence.openweather.api.provider.OpenWeatherProvider;
|
||||
|
||||
public class OpenWeatherProviderManager {
|
||||
|
||||
@ -34,11 +33,16 @@ public class OpenWeatherProviderManager {
|
||||
this.authToken = token;
|
||||
}
|
||||
|
||||
public OpenWeatherProvider getWeather() {
|
||||
public WeatherProvider getWeather() {
|
||||
return new WeatherProvider(authToken);
|
||||
}
|
||||
public OpenWeatherProvider getForecast() {
|
||||
|
||||
public ForecastProvider getForecast() {
|
||||
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 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) {
|
||||
|
||||
@ -29,9 +29,12 @@ import by.prominence.openweather.api.model.Coordinates;
|
||||
import by.prominence.openweather.api.model.OpenWeatherResponse;
|
||||
import by.prominence.openweather.api.utils.JsonUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected T executeRequest(String parameterString) 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;
|
||||
}
|
||||
protected T executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
|
||||
|
||||
T openWeatherResponse = null;
|
||||
|
||||
try {
|
||||
URL requestUrl = new URL(url);
|
||||
URL requestUrl = buildURL(requestSpecificParameters);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||
openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type));
|
||||
|
||||
if (openWeatherResponse.getResponseCode() == 401) {
|
||||
switch (connection.getResponseCode()) {
|
||||
case HttpURLConnection.HTTP_OK:
|
||||
openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type));
|
||||
break;
|
||||
case HttpURLConnection.HTTP_UNAUTHORIZED:
|
||||
throw new InvalidAuthTokenException();
|
||||
}
|
||||
|
||||
if (openWeatherResponse.getResponseCode() == 404) {
|
||||
case HttpURLConnection.HTTP_NOT_FOUND:
|
||||
throw new DataNotFoundException();
|
||||
}
|
||||
} else {
|
||||
throw new DataNotFoundException();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
} catch (IOException | ClassCastException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return openWeatherResponse;
|
||||
}
|
||||
|
||||
private String getRequestUrl() {
|
||||
protected String getRequestUrl() {
|
||||
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();
|
||||
}
|
||||
|
||||
@ -24,13 +24,37 @@ package by.prominence.openweather.api.provider;
|
||||
|
||||
import by.prominence.openweather.api.model.forecast.ForecastResponse;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ForecastProvider extends AbstractOpenWeatherProvider<ForecastResponse> {
|
||||
|
||||
private int amountOfDays = -1;
|
||||
|
||||
public ForecastProvider(String authToken) {
|
||||
super(authToken);
|
||||
}
|
||||
|
||||
public ForecastProvider(String authToken, int amountOfDays) {
|
||||
super(authToken);
|
||||
this.amountOfDays = amountOfDays;
|
||||
}
|
||||
|
||||
protected String getRequestType() {
|
||||
if (amountOfDays != -1) {
|
||||
return "forecast/daily";
|
||||
}
|
||||
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