mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-09 19:46:41 +03:00
Started global refactoring. Added timeout parameters for requests.
This commit is contained in:
parent
1e1054903e
commit
2a531dd683
@ -23,6 +23,8 @@
|
||||
package com.github.prominence.openweathermap.api;
|
||||
|
||||
import com.github.prominence.openweathermap.api.annotation.SubscriptionAvailability;
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.air.pollution.AirPollutionRequester;
|
||||
import com.github.prominence.openweathermap.api.request.air.pollution.AirPollutionRequesterImpl;
|
||||
import com.github.prominence.openweathermap.api.request.forecast.free.FiveDayThreeHourStepForecastRequester;
|
||||
@ -30,9 +32,8 @@ import com.github.prominence.openweathermap.api.request.forecast.free.FiveDayThr
|
||||
import com.github.prominence.openweathermap.api.request.onecall.OneCallWeatherRequester;
|
||||
import com.github.prominence.openweathermap.api.request.onecall.OneCallWeatherRequesterImpl;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequester;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequesterImpl;
|
||||
|
||||
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.*;
|
||||
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.ALL;
|
||||
|
||||
/**
|
||||
* The main public API client to communicate with OpenWeatherMap services.
|
||||
@ -40,6 +41,7 @@ import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.*;
|
||||
*/
|
||||
public class OpenWeatherMapClient {
|
||||
private final String apiKey;
|
||||
private final TimeoutSettings timeoutSettings = new TimeoutSettings();
|
||||
|
||||
/**
|
||||
* Created OpenWeatherMap client object.
|
||||
@ -49,13 +51,21 @@ public class OpenWeatherMapClient {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public void setConnectionTimeout(int connectionTimeout) {
|
||||
timeoutSettings.setConnectionTimeout(connectionTimeout);
|
||||
}
|
||||
|
||||
public void setReadTimeout(int readTimeout) {
|
||||
timeoutSettings.setReadTimeout(readTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Current Weather <a href="https://openweathermap.org/current">API</a>.
|
||||
* @return requester for retrieving current weather information.
|
||||
*/
|
||||
@SubscriptionAvailability(plans = ALL)
|
||||
public CurrentWeatherRequester currentWeather() {
|
||||
return new CurrentWeatherRequesterImpl(apiKey);
|
||||
return new CurrentWeatherRequester(new RequestSettings(apiKey, timeoutSettings));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.conf;
|
||||
|
||||
public class TimeoutSettings {
|
||||
private Integer connectionTimeout;
|
||||
private Integer readTimeout;
|
||||
|
||||
public TimeoutSettings() {
|
||||
this(2000, 2000);
|
||||
}
|
||||
|
||||
public TimeoutSettings(Integer connectionTimeout, Integer readTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
this.readTimeout = readTimeout;
|
||||
}
|
||||
|
||||
public TimeoutSettings(TimeoutSettings from) {
|
||||
this.connectionTimeout = from.connectionTimeout;
|
||||
this.readTimeout = from.readTimeout;
|
||||
}
|
||||
|
||||
public Integer getConnectionTimeout() {
|
||||
return connectionTimeout;
|
||||
}
|
||||
|
||||
public void setConnectionTimeout(Integer connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
}
|
||||
|
||||
public Integer getReadTimeout() {
|
||||
return readTimeout;
|
||||
}
|
||||
|
||||
public void setReadTimeout(Integer readTimeout) {
|
||||
this.readTimeout = readTimeout;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.enums;
|
||||
|
||||
public enum ResponseType {
|
||||
HTML("html"),
|
||||
XML("xml");
|
||||
|
||||
private final String value;
|
||||
|
||||
ResponseType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns language's value.
|
||||
* @return value.
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.request;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RequestSettings {
|
||||
|
||||
private static final String LANG_PARAM = "lang";
|
||||
private static final String UNITS_PARAM = "units";
|
||||
private static final String MODE_PARAM = "mode";
|
||||
private static final String API_KEY_PARAM_NAME = "appid";
|
||||
|
||||
private final TimeoutSettings timeoutSettings;
|
||||
|
||||
private final Map<String, String> requestParameters = new HashMap<>(8);
|
||||
|
||||
private final StringBuilder urlAppenderBuilder = new StringBuilder("");
|
||||
|
||||
private Language language = Language.ENGLISH;
|
||||
private UnitSystem unitSystem = UnitSystem.STANDARD;
|
||||
|
||||
public RequestSettings(String apiKey, TimeoutSettings timeoutSettings) {
|
||||
this.putRequestParameter(API_KEY_PARAM_NAME, apiKey);
|
||||
// make a copy
|
||||
this.timeoutSettings = new TimeoutSettings(timeoutSettings);
|
||||
}
|
||||
|
||||
public TimeoutSettings getTimeoutSettings() {
|
||||
return timeoutSettings;
|
||||
}
|
||||
|
||||
public UnitSystem getUnitSystem() {
|
||||
return unitSystem;
|
||||
}
|
||||
|
||||
public void setUnitSystem(UnitSystem unitSystem) {
|
||||
this.putRequestParameter(UNITS_PARAM, unitSystem.getValue());
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
public Language getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
public void setLanguage(Language language) {
|
||||
this.putRequestParameter(LANG_PARAM, language.getValue());
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public void setResponseType(ResponseType responseType) {
|
||||
this.putRequestParameter(MODE_PARAM, responseType.getValue());
|
||||
}
|
||||
|
||||
public void putRequestParameter(String key, String value) {
|
||||
this.requestParameters.put(key, value);
|
||||
}
|
||||
|
||||
public Map<String, String> getRequestParameters() {
|
||||
return requestParameters;
|
||||
}
|
||||
|
||||
public void appendToURL(String appendix) {
|
||||
urlAppenderBuilder.append(appendix);
|
||||
}
|
||||
|
||||
public StringBuilder getUrlAppender() {
|
||||
return urlAppenderBuilder;
|
||||
}
|
||||
}
|
||||
@ -32,6 +32,7 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* The type Request url builder.
|
||||
*/
|
||||
@Deprecated
|
||||
public class RequestUrlBuilder {
|
||||
|
||||
private static final String API_KEY_PARAM_NAME = "appid";
|
||||
|
||||
@ -24,6 +24,7 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.air.pollution;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.air.pollution.current.CurrentAirPollutionRequester;
|
||||
import com.github.prominence.openweathermap.api.request.air.pollution.current.CurrentAirPollutionRequesterImpl;
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.forecast.free;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.model.Coordinate;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.onecall;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.onecall.current.OneCallCurrentWeatherRequester;
|
||||
import com.github.prominence.openweathermap.api.request.onecall.current.OneCallCurrentWeatherRequesterImpl;
|
||||
|
||||
@ -22,25 +22,30 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.multiple.MultipleLocationsCurrentWeatherRequester;
|
||||
import com.github.prominence.openweathermap.api.request.weather.single.SingleLocationCurrentWeatherRequester;
|
||||
|
||||
/**
|
||||
* An interface for <a href="https://openweathermap.org/current">API</a> methods.
|
||||
* The type Current weather requester.
|
||||
*/
|
||||
public interface CurrentWeatherRequester {
|
||||
public class CurrentWeatherRequester {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Single location current weather requester.
|
||||
* Instantiates a new Current weather requester.
|
||||
*
|
||||
* @return the single location current weather requester
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
SingleLocationCurrentWeatherRequester single();
|
||||
public CurrentWeatherRequester(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiple locations current weather requester.
|
||||
*
|
||||
* @return the multiple locations current weather requester
|
||||
*/
|
||||
MultipleLocationsCurrentWeatherRequester multiple();
|
||||
public SingleLocationCurrentWeatherRequester single() {
|
||||
return new SingleLocationCurrentWeatherRequester(requestSettings);
|
||||
}
|
||||
|
||||
public MultipleLocationsCurrentWeatherRequester multiple() {
|
||||
return new MultipleLocationsCurrentWeatherRequester(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.multiple.MultipleLocationsCurrentWeatherRequesterImpl;
|
||||
import com.github.prominence.openweathermap.api.request.weather.multiple.MultipleLocationsCurrentWeatherRequester;
|
||||
import com.github.prominence.openweathermap.api.request.weather.single.SingleLocationCurrentWeatherRequesterImpl;
|
||||
import com.github.prominence.openweathermap.api.request.weather.single.SingleLocationCurrentWeatherRequester;
|
||||
|
||||
/**
|
||||
* The type Current weather requester.
|
||||
*/
|
||||
public class CurrentWeatherRequesterImpl implements CurrentWeatherRequester {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
/**
|
||||
* Instantiates a new Current weather requester.
|
||||
*
|
||||
* @param apiKey the api key
|
||||
*/
|
||||
public CurrentWeatherRequesterImpl(String apiKey) {
|
||||
urlBuilder = new RequestUrlBuilder(apiKey);
|
||||
}
|
||||
|
||||
public SingleLocationCurrentWeatherRequester single() {
|
||||
return new SingleLocationCurrentWeatherRequesterImpl(urlBuilder);
|
||||
}
|
||||
|
||||
public MultipleLocationsCurrentWeatherRequester multiple() {
|
||||
return new MultipleLocationsCurrentWeatherRequesterImpl(urlBuilder);
|
||||
}
|
||||
}
|
||||
@ -24,36 +24,45 @@ package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.model.Coordinate;
|
||||
import com.github.prominence.openweathermap.api.model.CoordinateRectangle;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
|
||||
/**
|
||||
* The interface Multiple locations current weather requester.
|
||||
* The type Multiple locations current weather requester.
|
||||
*/
|
||||
public interface MultipleLocationsCurrentWeatherRequester {
|
||||
public class MultipleLocationsCurrentWeatherRequester {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* By rectangle multiple result current weather request customizer.
|
||||
* Instantiates a new Multiple locations current weather requester.
|
||||
*
|
||||
* @param rectangle the rectangle
|
||||
* @param zoom the zoom
|
||||
* @return the multiple result current weather request customizer
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
MultipleResultCurrentWeatherRequestCustomizer byRectangle(CoordinateRectangle rectangle, int zoom);
|
||||
public MultipleLocationsCurrentWeatherRequester(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* By cities in cycle multiple result current weather request customizer.
|
||||
*
|
||||
* @param point the point
|
||||
* @return the multiple result cities in circle current weather request customizer
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point);
|
||||
public MultipleResultCurrentWeatherRequestCustomizer byRectangle(CoordinateRectangle rectangle, int zoom) {
|
||||
String coordinates = rectangle.getFormattedRequestString() + "," + zoom;
|
||||
requestSettings.appendToURL("box/city");
|
||||
requestSettings.putRequestParameter("bbox", coordinates);
|
||||
|
||||
/**
|
||||
* By cities in cycle multiple result current weather request customizer.
|
||||
*
|
||||
* @param point the point
|
||||
* @param citiesCount the cities count
|
||||
* @return the multiple result cities in circle current weather request customizer
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point, int citiesCount);
|
||||
return new MultipleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point, int citiesCount) {
|
||||
requestSettings.appendToURL("find");
|
||||
requestSettings.putRequestParameter("lat", Double.toString(point.getLatitude()));
|
||||
requestSettings.putRequestParameter("lon", Double.toString(point.getLongitude()));
|
||||
requestSettings.putRequestParameter("cnt", Integer.toString(citiesCount));
|
||||
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point) {
|
||||
requestSettings.appendToURL("find");
|
||||
requestSettings.putRequestParameter("lat", Double.toString(point.getLatitude()));
|
||||
requestSettings.putRequestParameter("lon", Double.toString(point.getLongitude()));
|
||||
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.model.Coordinate;
|
||||
import com.github.prominence.openweathermap.api.model.CoordinateRectangle;
|
||||
|
||||
/**
|
||||
* The type Multiple locations current weather requester.
|
||||
*/
|
||||
public class MultipleLocationsCurrentWeatherRequesterImpl implements MultipleLocationsCurrentWeatherRequester {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple locations current weather requester.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
*/
|
||||
public MultipleLocationsCurrentWeatherRequesterImpl(RequestUrlBuilder urlBuilder) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCurrentWeatherRequestCustomizer byRectangle(CoordinateRectangle rectangle, int zoom) {
|
||||
String coordinates = rectangle.getFormattedRequestString() + "," + zoom;
|
||||
urlBuilder.append("box/city");
|
||||
urlBuilder.addRequestParameter("bbox", coordinates);
|
||||
|
||||
return new MultipleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point, int citiesCount) {
|
||||
urlBuilder.append("find");
|
||||
urlBuilder.addRequestParameter("lat", Double.toString(point.getLatitude()));
|
||||
urlBuilder.addRequestParameter("lon", Double.toString(point.getLongitude()));
|
||||
urlBuilder.addRequestParameter("cnt", Integer.toString(citiesCount));
|
||||
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer byCitiesInCycle(Coordinate point) {
|
||||
urlBuilder.append("find");
|
||||
urlBuilder.addRequestParameter("lat", Double.toString(point.getLatitude()));
|
||||
urlBuilder.addRequestParameter("lon", Double.toString(point.getLongitude()));
|
||||
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
}
|
||||
@ -22,20 +22,44 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.AsyncRequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather async request terminator.
|
||||
* The type Multiple result current weather async request terminator.
|
||||
*/
|
||||
public interface MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator extends AsyncRequestTerminator<List<Weather>, String> {
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* XML response format.
|
||||
* Instantiates a new Multiple result current weather async request terminator.
|
||||
*
|
||||
* @return the completable future
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
CompletableFuture<String> asXML();
|
||||
MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public CompletableFuture<List<Weather>> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse()));
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asXML() {
|
||||
requestSettings.setResponseType(ResponseType.XML);
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather async request terminator.
|
||||
*/
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl implements MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather async request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<Weather>> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asXML() {
|
||||
urlBuilder.addRequestParameter("mode", "xml");
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -22,23 +22,40 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestCustomizer;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather request customizer.
|
||||
* The type Multiple result current weather request customizer.
|
||||
*/
|
||||
public interface MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer extends RequestCustomizer<MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer> {
|
||||
/**
|
||||
* Retrieve multiple result current weather request terminator.
|
||||
*
|
||||
* @return the multiple result current weather request terminator
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestTerminator retrieve();
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Retrieve async multiple result current weather async request terminator.
|
||||
* Instantiates a new Multiple result current weather request customizer.
|
||||
*
|
||||
* @return the multiple result current weather async request terminator
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator retrieveAsync();
|
||||
}
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer language(Language language) {
|
||||
requestSettings.setLanguage(language);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
requestSettings.setUnitSystem(unitSystem);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestTerminator retrieve() {
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestTerminator(requestSettings);
|
||||
}
|
||||
|
||||
public MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator(requestSettings);
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather request customizer.
|
||||
*/
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl implements MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
private Language language;
|
||||
private UnitSystem unitSystem = UnitSystem.STANDARD;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather request customizer.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl(RequestUrlBuilder urlBuilder) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer language(Language language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
this.unitSystem = unitSystem;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherRequestTerminator retrieve() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
}
|
||||
@ -22,19 +22,43 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.RequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather request terminator.
|
||||
* The type Multiple result current weather request terminator.
|
||||
*/
|
||||
public interface MultipleResultCitiesInCircleCurrentWeatherRequestTerminator extends RequestTerminator<List<Weather>, String> {
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* XML response format.
|
||||
* Instantiates a new Multiple result current weather request terminator.
|
||||
*
|
||||
* @return the XML string
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
String asXML();
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public List<Weather> asJava() {
|
||||
return new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse());
|
||||
}
|
||||
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
public String asXML() {
|
||||
requestSettings.setResponseType(ResponseType.XML);
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather request terminator.
|
||||
*/
|
||||
public class MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl implements MultipleResultCitiesInCircleCurrentWeatherRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Weather> asJava() {
|
||||
return new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asXML() {
|
||||
urlBuilder.addRequestParameter("mode", "xml");
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -23,12 +23,37 @@
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.AsyncRequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather async request terminator.
|
||||
* The type Multiple result current weather async request terminator.
|
||||
*/
|
||||
public interface MultipleResultCurrentWeatherAsyncRequestTerminator extends AsyncRequestTerminator<List<Weather>, String> {
|
||||
public class MultipleResultCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather async request terminator.
|
||||
*
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
MultipleResultCurrentWeatherAsyncRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public CompletableFuture<List<Weather>> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse()));
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather async request terminator.
|
||||
*/
|
||||
public class MultipleResultCurrentWeatherAsyncRequestTerminatorImpl implements MultipleResultCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather async request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
MultipleResultCurrentWeatherAsyncRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<Weather>> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -22,23 +22,42 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestCustomizer;
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather request customizer.
|
||||
* The type Multiple result current weather request customizer.
|
||||
*/
|
||||
public interface MultipleResultCurrentWeatherRequestCustomizer extends RequestCustomizer<MultipleResultCurrentWeatherRequestCustomizer> {
|
||||
/**
|
||||
* Retrieve multiple result current weather request terminator.
|
||||
*
|
||||
* @return the multiple result current weather request terminator
|
||||
*/
|
||||
MultipleResultCurrentWeatherRequestTerminator retrieve();
|
||||
public class MultipleResultCurrentWeatherRequestCustomizer {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Retrieve async multiple result current weather async request terminator.
|
||||
* Instantiates a new Multiple result current weather request customizer.
|
||||
*
|
||||
* @return the multiple result current weather async request terminator
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
MultipleResultCurrentWeatherAsyncRequestTerminator retrieveAsync();
|
||||
}
|
||||
MultipleResultCurrentWeatherRequestCustomizer(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public MultipleResultCurrentWeatherRequestCustomizer language(Language language) {
|
||||
requestSettings.setLanguage(language);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MultipleResultCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
requestSettings.setUnitSystem(unitSystem);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MultipleResultCurrentWeatherRequestTerminator retrieve() {
|
||||
return new MultipleResultCurrentWeatherRequestTerminator(requestSettings);
|
||||
}
|
||||
|
||||
public MultipleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
return new MultipleResultCurrentWeatherAsyncRequestTerminator(requestSettings);
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather request customizer.
|
||||
*/
|
||||
public class MultipleResultCurrentWeatherRequestCustomizerImpl implements MultipleResultCurrentWeatherRequestCustomizer {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
private Language language;
|
||||
private UnitSystem unitSystem = UnitSystem.STANDARD;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather request customizer.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
*/
|
||||
MultipleResultCurrentWeatherRequestCustomizerImpl(RequestUrlBuilder urlBuilder) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCurrentWeatherRequestCustomizer language(Language language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
this.unitSystem = unitSystem;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCurrentWeatherRequestTerminator retrieve() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new MultipleResultCurrentWeatherRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultipleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new MultipleResultCurrentWeatherAsyncRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
}
|
||||
@ -23,12 +23,36 @@
|
||||
package com.github.prominence.openweathermap.api.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.RequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The interface Multiple result current weather request terminator.
|
||||
* The type Multiple result current weather request terminator.
|
||||
*/
|
||||
public interface MultipleResultCurrentWeatherRequestTerminator extends RequestTerminator<List<Weather>, String> {
|
||||
public class MultipleResultCurrentWeatherRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather request terminator.
|
||||
*
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
MultipleResultCurrentWeatherRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public List<Weather> asJava() {
|
||||
return new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse());
|
||||
}
|
||||
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.multiple;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The type Multiple result current weather request terminator.
|
||||
*/
|
||||
public class MultipleResultCurrentWeatherRequestTerminatorImpl implements MultipleResultCurrentWeatherRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiple result current weather request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
MultipleResultCurrentWeatherRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Weather> asJava() {
|
||||
return new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -23,69 +23,57 @@
|
||||
package com.github.prominence.openweathermap.api.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.model.Coordinate;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
|
||||
/**
|
||||
* The interface Single location current weather requester.
|
||||
* The type Single location current weather requester.
|
||||
*/
|
||||
public interface SingleLocationCurrentWeatherRequester {
|
||||
public class SingleLocationCurrentWeatherRequester {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* By city name current weather request customizer.
|
||||
* Instantiates a new Single location current weather requester.
|
||||
*
|
||||
* @param cityName the city name
|
||||
* @return the single result current weather request customizer
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName);
|
||||
public SingleLocationCurrentWeatherRequester(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
this.requestSettings.appendToURL("weather");
|
||||
}
|
||||
|
||||
/**
|
||||
* By city name current weather request customizer.
|
||||
*
|
||||
* @param cityName the city name
|
||||
* @param countryCode the country code
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String countryCode);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName) {
|
||||
requestSettings.putRequestParameter("q", cityName);
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* By city name current weather request customizer.
|
||||
*
|
||||
* @param cityName the city name
|
||||
* @param stateCode the state code
|
||||
* @param countryCode the country code
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String stateCode, String countryCode);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String countryCode) {
|
||||
requestSettings.putRequestParameter("q", cityName + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* By city id current weather request customizer.
|
||||
*
|
||||
* @param cityId the city id
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byCityId(long cityId);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String stateCode, String countryCode) {
|
||||
requestSettings.putRequestParameter("q", cityName + "," + stateCode + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* By coordinate current weather request customizer.
|
||||
*
|
||||
* @param coordinate the coordinate
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byCoordinate(Coordinate coordinate);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityId(long cityId) {
|
||||
requestSettings.putRequestParameter("id", String.valueOf(cityId));
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* By zip code and country current weather request customizer.
|
||||
*
|
||||
* @param zipCode the zip code
|
||||
* @param countryCode the country code
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCoordinate(Coordinate coordinate) {
|
||||
requestSettings.putRequestParameter("lat", String.valueOf(coordinate.getLatitude()));
|
||||
requestSettings.putRequestParameter("lon", String.valueOf(coordinate.getLongitude()));
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* By zip code in usa current weather request customizer.
|
||||
*
|
||||
* @param zipCode the zip code
|
||||
* @return the single result current weather request customizer
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizer byZipCodeInUSA(String zipCode);
|
||||
public SingleResultCurrentWeatherRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode) {
|
||||
requestSettings.putRequestParameter("zip", zipCode + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byZipCodeInUSA(String zipCode) {
|
||||
requestSettings.putRequestParameter("zip", zipCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizer(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.model.Coordinate;
|
||||
|
||||
/**
|
||||
* The type Single location current weather requester.
|
||||
*/
|
||||
public class SingleLocationCurrentWeatherRequesterImpl implements SingleLocationCurrentWeatherRequester {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
/**
|
||||
* Instantiates a new Single location current weather requester.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
*/
|
||||
public SingleLocationCurrentWeatherRequesterImpl(RequestUrlBuilder urlBuilder) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
urlBuilder.append("weather");
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName) {
|
||||
urlBuilder.addRequestParameter("q", cityName);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String countryCode) {
|
||||
urlBuilder.addRequestParameter("q", cityName + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityName(String cityName, String stateCode, String countryCode) {
|
||||
urlBuilder.addRequestParameter("q", cityName + "," + stateCode + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCityId(long cityId) {
|
||||
urlBuilder.addRequestParameter("id", cityId);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byCoordinate(Coordinate coordinate) {
|
||||
urlBuilder.addRequestParameter("lat", String.valueOf(coordinate.getLatitude()));
|
||||
urlBuilder.addRequestParameter("lon", String.valueOf(coordinate.getLongitude()));
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer byZipCodeAndCountry(String zipCode, String countryCode) {
|
||||
urlBuilder.addRequestParameter("zip", zipCode + "," + countryCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherRequestCustomizer byZipCodeInUSA(String zipCode) {
|
||||
urlBuilder.addRequestParameter("zip", zipCode);
|
||||
return new SingleResultCurrentWeatherRequestCustomizerImpl(urlBuilder);
|
||||
}
|
||||
}
|
||||
@ -22,26 +22,51 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.AsyncRequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The current weather async request terminator interface.
|
||||
* The type Single result current weather async request terminator.
|
||||
*/
|
||||
public interface SingleResultCurrentWeatherAsyncRequestTerminator extends AsyncRequestTerminator<Weather, String> {
|
||||
/**
|
||||
* XML response format.
|
||||
*
|
||||
* @return the completable future
|
||||
*/
|
||||
CompletableFuture<String> asXML();
|
||||
public class SingleResultCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* HTML response format.
|
||||
* Instantiates a new Single result current weather async request terminator.
|
||||
*
|
||||
* @return the completable future
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
CompletableFuture<String> asHTML();
|
||||
SingleResultCurrentWeatherAsyncRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public CompletableFuture<Weather> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getSingle(getRawResponse()));
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asXML() {
|
||||
requestSettings.setResponseType(ResponseType.XML);
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
public CompletableFuture<String> asHTML() {
|
||||
requestSettings.setResponseType(ResponseType.HTML);
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The type Single result current weather async request terminator.
|
||||
*/
|
||||
public class SingleResultCurrentWeatherAsyncRequestTerminatorImpl implements SingleResultCurrentWeatherAsyncRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Single result current weather async request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
SingleResultCurrentWeatherAsyncRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Weather> asJava() {
|
||||
return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getSingle(getRawResponse()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asJSON() {
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asXML() {
|
||||
urlBuilder.addRequestParameter("mode", "xml");
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> asHTML() {
|
||||
urlBuilder.addRequestParameter("mode", "html");
|
||||
return CompletableFuture.supplyAsync(this::getRawResponse);
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -22,24 +22,40 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestCustomizer;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
|
||||
/**
|
||||
* The current weather request customizer interface.
|
||||
* The type Single result current weather request customizer.
|
||||
*/
|
||||
public interface SingleResultCurrentWeatherRequestCustomizer extends RequestCustomizer<SingleResultCurrentWeatherRequestCustomizer> {
|
||||
/**
|
||||
* Retrieve current weather request terminator.
|
||||
*
|
||||
* @return the single result current weather request terminator
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestTerminator retrieve();
|
||||
public class SingleResultCurrentWeatherRequestCustomizer {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* Retrieve current weather async request terminator.
|
||||
* Instantiates a new Single result current weather request customizer.
|
||||
*
|
||||
* @return the single result current weather async request terminator
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
SingleResultCurrentWeatherAsyncRequestTerminator retrieveAsync();
|
||||
SingleResultCurrentWeatherRequestCustomizer(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
}
|
||||
public SingleResultCurrentWeatherRequestCustomizer language(Language language) {
|
||||
requestSettings.setLanguage(language);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
requestSettings.setUnitSystem(unitSystem);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherRequestTerminator retrieve() {
|
||||
return new SingleResultCurrentWeatherRequestTerminator(requestSettings);
|
||||
}
|
||||
|
||||
public SingleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
return new SingleResultCurrentWeatherAsyncRequestTerminator(requestSettings);
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.enums.Language;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
|
||||
/**
|
||||
* The type Single result current weather request customizer.
|
||||
*/
|
||||
public class SingleResultCurrentWeatherRequestCustomizerImpl implements SingleResultCurrentWeatherRequestCustomizer {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
|
||||
private Language language;
|
||||
private UnitSystem unitSystem = UnitSystem.STANDARD;
|
||||
|
||||
/**
|
||||
* Instantiates a new Single result current weather request customizer.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestCustomizerImpl(RequestUrlBuilder urlBuilder) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherRequestCustomizer language(Language language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherRequestCustomizer unitSystem(UnitSystem unitSystem) {
|
||||
this.unitSystem = unitSystem;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherRequestTerminator retrieve() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new SingleResultCurrentWeatherRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SingleResultCurrentWeatherAsyncRequestTerminator retrieveAsync() {
|
||||
urlBuilder.applyCustomization(language, unitSystem);
|
||||
return new SingleResultCurrentWeatherAsyncRequestTerminatorImpl(urlBuilder, unitSystem);
|
||||
}
|
||||
}
|
||||
@ -22,24 +22,46 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.request.RequestTerminator;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
/**
|
||||
* The current weather request terminator interface.
|
||||
* The type Single result current weather request terminator.
|
||||
*/
|
||||
public interface SingleResultCurrentWeatherRequestTerminator extends RequestTerminator<Weather, String> {
|
||||
/**
|
||||
* XML response format.
|
||||
*
|
||||
* @return the XML string
|
||||
*/
|
||||
String asXML();
|
||||
public class SingleResultCurrentWeatherRequestTerminator {
|
||||
private final RequestSettings requestSettings;
|
||||
|
||||
/**
|
||||
* HTML response format.
|
||||
* Instantiates a new Single result current weather request terminator.
|
||||
*
|
||||
* @return the HTML string
|
||||
* @param requestSettings request settings object.
|
||||
*/
|
||||
String asHTML();
|
||||
SingleResultCurrentWeatherRequestTerminator(RequestSettings requestSettings) {
|
||||
this.requestSettings = requestSettings;
|
||||
}
|
||||
|
||||
public Weather asJava() {
|
||||
return new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getSingle(asJSON());
|
||||
}
|
||||
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
public String asXML() {
|
||||
requestSettings.setResponseType(ResponseType.XML);
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
public String asHTML() {
|
||||
requestSettings.setResponseType(ResponseType.HTML);
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(requestSettings);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2021 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.request.weather.single;
|
||||
|
||||
import com.github.prominence.openweathermap.api.request.RequestUrlBuilder;
|
||||
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherResponseMapper;
|
||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
||||
|
||||
/**
|
||||
* The type Single result current weather request terminator.
|
||||
*/
|
||||
public class SingleResultCurrentWeatherRequestTerminatorImpl implements SingleResultCurrentWeatherRequestTerminator {
|
||||
private final RequestUrlBuilder urlBuilder;
|
||||
private final UnitSystem unitSystem;
|
||||
|
||||
/**
|
||||
* Instantiates a new Single result current weather request terminator.
|
||||
*
|
||||
* @param urlBuilder the url builder
|
||||
* @param unitSystem the unit system
|
||||
*/
|
||||
SingleResultCurrentWeatherRequestTerminatorImpl(RequestUrlBuilder urlBuilder, UnitSystem unitSystem) {
|
||||
this.urlBuilder = urlBuilder;
|
||||
this.unitSystem = unitSystem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Weather asJava() {
|
||||
return new CurrentWeatherResponseMapper(unitSystem).getSingle(asJSON());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asJSON() {
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asXML() {
|
||||
urlBuilder.addRequestParameter("mode", "xml");
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asHTML() {
|
||||
urlBuilder.addRequestParameter("mode", "html");
|
||||
return getRawResponse();
|
||||
}
|
||||
|
||||
private String getRawResponse() {
|
||||
return RequestUtils.getResponse(urlBuilder.buildUrl());
|
||||
}
|
||||
}
|
||||
@ -22,8 +22,10 @@
|
||||
|
||||
package com.github.prominence.openweathermap.api.utils;
|
||||
|
||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
||||
import com.github.prominence.openweathermap.api.exception.NoDataFoundException;
|
||||
import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException;
|
||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -35,17 +37,32 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Utility class for API calls execution.
|
||||
*/
|
||||
public final class RequestUtils {
|
||||
|
||||
private static final String OWM_URL_BASE = "http://api.openweathermap.org/data/2.5/";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RequestUtils.class);
|
||||
|
||||
private RequestUtils() {
|
||||
}
|
||||
|
||||
public static String getResponse(RequestSettings requestSettings) {
|
||||
StringBuilder requestUrlBuilder = new StringBuilder(OWM_URL_BASE);
|
||||
requestUrlBuilder.append(requestSettings.getUrlAppender());
|
||||
requestUrlBuilder.append('?');
|
||||
String parameters = requestSettings.getRequestParameters().entrySet().stream()
|
||||
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||||
.collect(Collectors.joining("&"));
|
||||
requestUrlBuilder.append(parameters);
|
||||
|
||||
return getResponse(requestUrlBuilder.toString(), requestSettings.getTimeoutSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes call to provided API url and retrieves response in <code>String</code> representation.
|
||||
*
|
||||
@ -54,6 +71,18 @@ public final class RequestUtils {
|
||||
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
|
||||
*/
|
||||
public static String getResponse(String url) {
|
||||
return getResponse(url, new TimeoutSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes call to provided API url and retrieves response in <code>String</code> representation.
|
||||
*
|
||||
* @param url the url to make API request.
|
||||
* @param timeoutSettings an object with timeout settings.
|
||||
* @return response from the request in <code>String</code> representation.
|
||||
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
|
||||
*/
|
||||
public static String getResponse(String url, TimeoutSettings timeoutSettings) {
|
||||
URL requestUrl;
|
||||
try {
|
||||
requestUrl = new URL(url);
|
||||
@ -62,7 +91,7 @@ public final class RequestUtils {
|
||||
throw new IllegalArgumentException(ex);
|
||||
}
|
||||
logger.debug("Executing OpenWeatherMap API request: " + url);
|
||||
final InputStream requestInputStream = executeRequest(requestUrl);
|
||||
final InputStream requestInputStream = executeRequest(requestUrl, timeoutSettings);
|
||||
|
||||
return convertInputStreamToString(requestInputStream);
|
||||
}
|
||||
@ -75,11 +104,20 @@ public final class RequestUtils {
|
||||
* @throws InvalidAuthTokenException in case if authentication token wasn't set or requested functionality is not permitted for its subscription plan.
|
||||
* @throws NoDataFoundException in case if there is no any data for requested location(s) or request is invalid.
|
||||
*/
|
||||
private static InputStream executeRequest(URL requestUrl) {
|
||||
private static InputStream executeRequest(URL requestUrl, TimeoutSettings timeoutSettings) {
|
||||
InputStream resultStream;
|
||||
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
||||
|
||||
if (timeoutSettings.getConnectionTimeout() != null) {
|
||||
connection.setConnectTimeout(timeoutSettings.getConnectionTimeout());
|
||||
}
|
||||
|
||||
if (timeoutSettings.getReadTimeout() != null) {
|
||||
connection.setReadTimeout(timeoutSettings.getReadTimeout());
|
||||
}
|
||||
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
switch (connection.getResponseCode()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user