From 2a531dd683845fd0b95eb18cdb26ca8c2596b6d4 Mon Sep 17 00:00:00 2001 From: Prominence Date: Wed, 13 Apr 2022 00:13:27 +0300 Subject: [PATCH] Started global refactoring. Added timeout parameters for requests. --- .../api/OpenWeatherMapClient.java | 16 +++- .../api/conf/TimeoutSettings.java | 58 +++++++++++ .../api/enums/ResponseType.java | 42 ++++++++ .../api/request/RequestSettings.java | 96 +++++++++++++++++++ .../api/request/RequestUrlBuilder.java | 1 + .../pollution/AirPollutionRequesterImpl.java | 1 + ...DayThreeHourStepForecastRequesterImpl.java | 1 + .../onecall/OneCallWeatherRequesterImpl.java | 1 + .../weather/CurrentWeatherRequester.java | 27 +++--- .../weather/CurrentWeatherRequesterImpl.java | 53 ---------- ...tipleLocationsCurrentWeatherRequester.java | 53 +++++----- ...eLocationsCurrentWeatherRequesterImpl.java | 71 -------------- ...eCurrentWeatherAsyncRequestTerminator.java | 36 +++++-- ...rentWeatherAsyncRequestTerminatorImpl.java | 71 -------------- ...CircleCurrentWeatherRequestCustomizer.java | 43 ++++++--- ...leCurrentWeatherRequestCustomizerImpl.java | 70 -------------- ...CircleCurrentWeatherRequestTerminator.java | 36 +++++-- ...leCurrentWeatherRequestTerminatorImpl.java | 70 -------------- ...tCurrentWeatherAsyncRequestTerminator.java | 31 +++++- ...rentWeatherAsyncRequestTerminatorImpl.java | 65 ------------- ...ResultCurrentWeatherRequestCustomizer.java | 45 ++++++--- ...ltCurrentWeatherRequestCustomizerImpl.java | 70 -------------- ...ResultCurrentWeatherRequestTerminator.java | 30 +++++- ...ltCurrentWeatherRequestTerminatorImpl.java | 64 ------------- ...SingleLocationCurrentWeatherRequester.java | 92 ++++++++---------- ...leLocationCurrentWeatherRequesterImpl.java | 81 ---------------- ...tCurrentWeatherAsyncRequestTerminator.java | 49 +++++++--- ...rentWeatherAsyncRequestTerminatorImpl.java | 76 --------------- ...ResultCurrentWeatherRequestCustomizer.java | 42 +++++--- ...ltCurrentWeatherRequestCustomizerImpl.java | 70 -------------- ...ResultCurrentWeatherRequestTerminator.java | 46 ++++++--- ...ltCurrentWeatherRequestTerminatorImpl.java | 74 -------------- .../api/utils/RequestUtils.java | 42 +++++++- 33 files changed, 617 insertions(+), 1006 deletions(-) create mode 100644 src/main/java/com/github/prominence/openweathermap/api/conf/TimeoutSettings.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/enums/ResponseType.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequesterImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminatorImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminatorImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminatorImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminatorImpl.java diff --git a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java index 145be6d..7dc8465 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java @@ -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 API. * @return requester for retrieving current weather information. */ @SubscriptionAvailability(plans = ALL) public CurrentWeatherRequester currentWeather() { - return new CurrentWeatherRequesterImpl(apiKey); + return new CurrentWeatherRequester(new RequestSettings(apiKey, timeoutSettings)); } /** diff --git a/src/main/java/com/github/prominence/openweathermap/api/conf/TimeoutSettings.java b/src/main/java/com/github/prominence/openweathermap/api/conf/TimeoutSettings.java new file mode 100644 index 0000000..45dbd10 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/conf/TimeoutSettings.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/enums/ResponseType.java b/src/main/java/com/github/prominence/openweathermap/api/enums/ResponseType.java new file mode 100644 index 0000000..4c70a54 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/enums/ResponseType.java @@ -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; + } +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java b/src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java new file mode 100644 index 0000000..3ce73ed --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java @@ -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 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 getRequestParameters() { + return requestParameters; + } + + public void appendToURL(String appendix) { + urlAppenderBuilder.append(appendix); + } + + public StringBuilder getUrlAppender() { + return urlAppenderBuilder; + } +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java b/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java index 378bfca..5d4240d 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/RequestUrlBuilder.java @@ -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"; diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequesterImpl.java index 0faa6b3..ebc7a6c 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequesterImpl.java @@ -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; diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java index b5a87f2..76c3a92 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequesterImpl.java @@ -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; diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/OneCallWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/OneCallWeatherRequesterImpl.java index d0dddb1..ab04420 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/OneCallWeatherRequesterImpl.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/OneCallWeatherRequesterImpl.java @@ -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; diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java index 9c459d4..c56babd 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequester.java @@ -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 API 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); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequesterImpl.java deleted file mode 100644 index 0b393d6..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequesterImpl.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequester.java index 291674e..724ddbc 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequester.java @@ -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); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java deleted file mode 100644 index ba105cd..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleLocationsCurrentWeatherRequesterImpl.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator.java index 31905d1..14404b2 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator.java @@ -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, 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 asXML(); + MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminator(RequestSettings requestSettings) { + this.requestSettings = requestSettings; + } + + public CompletableFuture> asJava() { + return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse())); + } + + public CompletableFuture asJSON() { + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + public CompletableFuture asXML() { + requestSettings.setResponseType(ResponseType.XML); + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + private String getRawResponse() { + return RequestUtils.getResponse(requestSettings); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl.java deleted file mode 100644 index 189cfb3..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherAsyncRequestTerminatorImpl.java +++ /dev/null @@ -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> asJava() { - return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse())); - } - - @Override - public CompletableFuture asJSON() { - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - @Override - public CompletableFuture asXML() { - urlBuilder.addRequestParameter("mode", "xml"); - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - private String getRawResponse() { - return RequestUtils.getResponse(urlBuilder.buildUrl()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer.java index 336b04c..d9663c9 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizer.java @@ -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 { - /** - * 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); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl.java deleted file mode 100644 index b753f5d..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestCustomizerImpl.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminator.java index dcd1905..352208e 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminator.java @@ -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, 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 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); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl.java deleted file mode 100644 index 7e482a2..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCitiesInCircleCurrentWeatherRequestTerminatorImpl.java +++ /dev/null @@ -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 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()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminator.java index 6374a96..5ec39b1 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminator.java @@ -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, 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> asJava() { + return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse())); + } + + public CompletableFuture asJSON() { + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + private String getRawResponse() { + return RequestUtils.getResponse(requestSettings); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminatorImpl.java deleted file mode 100644 index e34c195..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherAsyncRequestTerminatorImpl.java +++ /dev/null @@ -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> asJava() { - return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse())); - } - - @Override - public CompletableFuture asJSON() { - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - private String getRawResponse() { - return RequestUtils.getResponse(urlBuilder.buildUrl()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizer.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizer.java index 651e413..90b33a0 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizer.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizer.java @@ -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 { - /** - * 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); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java deleted file mode 100644 index 78d14f8..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestCustomizerImpl.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminator.java index aa916f6..49ce6c3 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminator.java @@ -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, 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 asJava() { + return new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getList(getRawResponse()); + } + + public String asJSON() { + return getRawResponse(); + } + + private String getRawResponse() { + return RequestUtils.getResponse(requestSettings); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminatorImpl.java deleted file mode 100644 index 3cf256a..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/multiple/MultipleResultCurrentWeatherRequestTerminatorImpl.java +++ /dev/null @@ -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 asJava() { - return new CurrentWeatherResponseMapper(unitSystem).getList(getRawResponse()); - } - - @Override - public String asJSON() { - return getRawResponse(); - } - - private String getRawResponse() { - return RequestUtils.getResponse(urlBuilder.buildUrl()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequester.java index 0dc887a..199a980 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequester.java @@ -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); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java deleted file mode 100644 index e26038b..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleLocationCurrentWeatherRequesterImpl.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminator.java index 3c4cd61..f425d71 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminator.java @@ -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 { - /** - * XML response format. - * - * @return the completable future - */ - CompletableFuture 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 asHTML(); + SingleResultCurrentWeatherAsyncRequestTerminator(RequestSettings requestSettings) { + this.requestSettings = requestSettings; + } + + public CompletableFuture asJava() { + return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(requestSettings.getUnitSystem()).getSingle(getRawResponse())); + } + + public CompletableFuture asJSON() { + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + public CompletableFuture asXML() { + requestSettings.setResponseType(ResponseType.XML); + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + public CompletableFuture asHTML() { + requestSettings.setResponseType(ResponseType.HTML); + return CompletableFuture.supplyAsync(this::getRawResponse); + } + + private String getRawResponse() { + return RequestUtils.getResponse(requestSettings); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminatorImpl.java deleted file mode 100644 index 88f39b2..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherAsyncRequestTerminatorImpl.java +++ /dev/null @@ -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 asJava() { - return CompletableFuture.supplyAsync(() -> new CurrentWeatherResponseMapper(unitSystem).getSingle(getRawResponse())); - } - - @Override - public CompletableFuture asJSON() { - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - @Override - public CompletableFuture asXML() { - urlBuilder.addRequestParameter("mode", "xml"); - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - @Override - public CompletableFuture asHTML() { - urlBuilder.addRequestParameter("mode", "html"); - return CompletableFuture.supplyAsync(this::getRawResponse); - } - - private String getRawResponse() { - return RequestUtils.getResponse(urlBuilder.buildUrl()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizer.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizer.java index 9661cf9..982ffe2 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizer.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizer.java @@ -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 { - /** - * 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); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java deleted file mode 100644 index 2734252..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestCustomizerImpl.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminator.java index 499091d..75cf51a 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminator.java @@ -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 { - /** - * 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); + } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminatorImpl.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminatorImpl.java deleted file mode 100644 index 65be3ac..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/single/SingleResultCurrentWeatherRequestTerminatorImpl.java +++ /dev/null @@ -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()); - } -} diff --git a/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java b/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java index ea8cfe5..c20ec6d 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java +++ b/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java @@ -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 String 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 String representation. + * + * @param url the url to make API request. + * @param timeoutSettings an object with timeout settings. + * @return response from the request in String 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()) {