Removed duplicated code. Added generics.

This commit is contained in:
Prominence 2018-07-12 23:12:57 +03:00
parent 2bd088aee8
commit 175a092e83
3 changed files with 29 additions and 47 deletions

View File

@ -23,12 +23,15 @@
package by.prominence.openweather.api;
import by.prominence.openweather.api.constants.System;
import by.prominence.openweather.api.exception.DataNotFoundException;
import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
abstract class BasicRequester extends AuthenticationTokenBasedRequester {
abstract class BasicRequester<T> extends AuthenticationTokenBasedRequester {
protected String language;
protected String unit;
@ -38,6 +41,26 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester {
super(authToken);
}
public T getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?id=" + id);
}
public T getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?q=" + name);
}
public T getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?lat=" + latitude + "&lon=" + longitude);
}
public T getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException {
return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude());
}
public T getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?zip=" + zipCode + "," + countryCode);
}
protected URL buildURL(String requestSpecificParameters) throws MalformedURLException {
StringBuilder urlBuilder = new StringBuilder(System.OPEN_WEATHER_API_URL);
@ -81,5 +104,6 @@ abstract class BasicRequester extends AuthenticationTokenBasedRequester {
}
protected abstract String getRequestType();
protected abstract T executeRequest(String requestSpecificParamsString) throws InvalidAuthTokenException, DataNotFoundException;
}

View File

@ -24,7 +24,6 @@ package by.prominence.openweather.api;
import by.prominence.openweather.api.exception.DataNotFoundException;
import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates;
import by.prominence.openweather.api.model.forecast.ForecastResponse;
import by.prominence.openweather.api.utils.JsonUtils;
import by.prominence.openweather.api.utils.RequestUtils;
@ -34,7 +33,7 @@ import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class ForecastRequester extends BasicRequester {
public class ForecastRequester extends BasicRequester<ForecastResponse> {
private int amountOfDays = -1;
@ -62,26 +61,6 @@ public class ForecastRequester extends BasicRequester {
return this;
}
public ForecastResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?id=" + id);
}
public ForecastResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?q=" + name);
}
public ForecastResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?lat=" + latitude + "&lon=" + longitude);
}
public ForecastResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException {
return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude());
}
public ForecastResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?zip=" + zipCode + "," + countryCode);
}
@Override
protected Map<String, String> getAdditionalParameters() {
Map<String, String> additionalParameters = null;
@ -100,7 +79,7 @@ public class ForecastRequester extends BasicRequester {
return "forecast";
}
private ForecastResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
protected ForecastResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
try {
InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters));

View File

@ -24,7 +24,6 @@ package by.prominence.openweather.api;
import by.prominence.openweather.api.exception.DataNotFoundException;
import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates;
import by.prominence.openweather.api.model.weather.WeatherResponse;
import by.prominence.openweather.api.utils.JsonUtils;
import by.prominence.openweather.api.utils.RequestUtils;
@ -32,7 +31,7 @@ import by.prominence.openweather.api.utils.RequestUtils;
import java.io.IOException;
import java.io.InputStream;
public class WeatherRequester extends BasicRequester {
public class WeatherRequester extends BasicRequester<WeatherResponse> {
WeatherRequester(String authToken) {
super(authToken);
@ -53,31 +52,11 @@ public class WeatherRequester extends BasicRequester {
return this;
}
public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?id=" + id);
}
public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?q=" + name);
}
public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?lat=" + latitude + "&lon=" + longitude);
}
public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException {
return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude());
}
public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?zip=" + zipCode + "," + countryCode);
}
protected String getRequestType() {
return "weather";
}
private WeatherResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
protected WeatherResponse executeRequest(String requestSpecificParameters) throws InvalidAuthTokenException, DataNotFoundException {
try {
InputStream requestResult = RequestUtils.executeGetRequest(buildURL(requestSpecificParameters));