From 5e4b34632ef1684ac720b41f3123a78029c7352b Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Mon, 2 May 2022 20:12:34 +0300 Subject: [PATCH] Reimplemented request executor. Added possibility to test requests with mocked http requests. --- README.md | 5 +- .../api/OpenWeatherMapClient.java | 34 ++-- .../api/core/net/HttpClient.java | 10 ++ .../net/HttpURLConnectionBasedHttpClient.java | 126 ++++++++++++++ .../api/core/net/RequestExecutor.java | 97 +++++++++++ .../api/request/RequestSettings.java | 44 ++++- .../AirPollutionAsyncRequestTerminator.java | 4 +- .../AirPollutionRequestTerminator.java | 4 +- ...limaticForecastAsyncRequestTerminator.java | 4 +- .../ClimaticForecastRequestTerminator.java | 4 +- .../DailyForecastAsyncRequestTerminator.java | 4 +- .../daily/DailyForecastRequestTerminator.java | 4 +- ...ourStepForecastAsyncRequestTerminator.java | 4 +- ...hreeHourStepForecastRequestTerminator.java | 4 +- ...sHourlyForecastAsyncRequestTerminator.java | 4 +- ...urDaysHourlyForecastRequestTerminator.java | 4 +- ...DirectGeocodingRequestAsyncTerminator.java | 4 +- .../DirectGeocodingRequestTerminator.java | 4 +- ...everseGeocodingRequestAsyncTerminator.java | 4 +- .../ReverseGeocodingRequestTerminator.java | 4 +- ...lCurrentWeatherAsyncRequestTerminator.java | 4 +- ...neCallCurrentWeatherRequestTerminator.java | 4 +- ...storicalWeatherAsyncRequestTerminator.java | 4 +- ...allHistoricalWeatherRequestTerminator.java | 4 +- .../SolarRadiationAsyncRequestTerminator.java | 4 +- .../SolarRadiationRequestTerminator.java | 4 +- .../RoadRiskAsyncRequestTerminator.java | 4 +- .../roadrisk/RoadRiskRequestTerminator.java | 4 +- .../request/roadrisk/RoadRiskRequester.java | 6 +- .../model/RoadRiskRequestPayload.java | 17 ++ .../CurrentWeatherAsyncRequestTerminator.java | 4 +- .../CurrentWeatherRequestTerminator.java | 4 +- .../serializer/RoadRiskRequestSerializer.java | 35 ++++ .../api/utils/RequestUtils.java | 161 ------------------ .../api/core/net/MockHttpClient.java | 31 ++++ ...reeHourStepForecastResponseMapperTest.java | 3 +- .../mapper/RoadRiskResponseMapperTest.java | 2 +- .../SolarRadiationResponseMapperTest.java | 3 +- ...ourStepForecastResponseMapperUnitTest.java | 2 +- .../roadrisk/RoadRiskIntegrationTest.java | 87 ++++++++++ .../api/utils/RequestUtilsUnitTest.java | 40 ----- 41 files changed, 525 insertions(+), 274 deletions(-) create mode 100644 src/main/java/com/github/prominence/openweathermap/api/core/net/HttpClient.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/core/net/HttpURLConnectionBasedHttpClient.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/core/net/RequestExecutor.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/model/RoadRiskRequestPayload.java create mode 100644 src/main/java/com/github/prominence/openweathermap/api/serializer/RoadRiskRequestSerializer.java delete mode 100644 src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java create mode 100644 src/test/java/com/github/prominence/openweathermap/api/core/net/MockHttpClient.java create mode 100644 src/test/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskIntegrationTest.java delete mode 100644 src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java diff --git a/README.md b/README.md index b23ea83..b00f99d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Free: Paid: * Hourly Forecast 4 days * Daily Forecast 16 days -* Climatic 30 days +* Climatic Forecast 30 days * Solar Radiation API * Road Risk API @@ -26,11 +26,8 @@ Free: * Weather Triggers Paid: -* Climatic Forecast 30 days * Bulk Downloading -* Solar Radiation API * Global Weather Alerts / Push notifications -* Road Risk API ### Maven coordinates: 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 78e050b..deb2965 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java +++ b/src/main/java/com/github/prominence/openweathermap/api/OpenWeatherMapClient.java @@ -24,6 +24,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.core.net.HttpClient; +import com.github.prominence.openweathermap.api.core.net.HttpURLConnectionBasedHttpClient; 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.forecast.climatic.ClimaticForecastRequester; @@ -46,6 +48,8 @@ public class OpenWeatherMapClient { private final String apiKey; private final TimeoutSettings timeoutSettings = new TimeoutSettings(); + private HttpClient httpClient = new HttpURLConnectionBasedHttpClient(); + /** * Created OpenWeatherMap client object. * @param apiKey API key obtained on OpenWeatherMap site. @@ -62,13 +66,17 @@ public class OpenWeatherMapClient { timeoutSettings.setReadTimeout(readTimeout); } + public void setHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + } + /** * Current Weather API. * @return requester for retrieving current weather information. */ @SubscriptionAvailability(plans = ALL) public CurrentWeatherRequester currentWeather() { - return new CurrentWeatherRequester(new RequestSettings(apiKey, timeoutSettings)); + return new CurrentWeatherRequester(getRequestSettings()); } /** @@ -77,7 +85,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE }) public FourDaysHourlyForecastRequester forecastHourly4Days() { - return new FourDaysHourlyForecastRequester(new RequestSettings(apiKey, timeoutSettings)); + return new FourDaysHourlyForecastRequester(getRequestSettings()); } /** @@ -87,7 +95,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = ALL) public OneCallWeatherRequester oneCall() { - return new OneCallWeatherRequester(new RequestSettings(apiKey, timeoutSettings)); + return new OneCallWeatherRequester(getRequestSettings()); } /** @@ -96,7 +104,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = PAID) public DailyForecastRequester forecastDaily16Days() { - return new DailyForecastRequester(new RequestSettings(apiKey, timeoutSettings)); + return new DailyForecastRequester(getRequestSettings()); } /** @@ -105,7 +113,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE }) public ClimaticForecastRequester climaticForecast30Days() { - return new ClimaticForecastRequester(new RequestSettings(apiKey, timeoutSettings)); + return new ClimaticForecastRequester(getRequestSettings()); } /** @@ -114,7 +122,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = SPECIAL) public SolarRadiationRequester solarRadiation() { - return new SolarRadiationRequester(new RequestSettings(apiKey, timeoutSettings)); + return new SolarRadiationRequester(getRequestSettings()); } /** @@ -123,7 +131,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = ALL) public FiveDayThreeHourStepForecastRequester forecast5Day3HourStep() { - return new FiveDayThreeHourStepForecastRequester(new RequestSettings(apiKey, timeoutSettings)); + return new FiveDayThreeHourStepForecastRequester(getRequestSettings()); } /** @@ -132,7 +140,7 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = SPECIAL) public RoadRiskRequester roadRisk() { - return new RoadRiskRequester(new RequestSettings(apiKey, timeoutSettings)); + return new RoadRiskRequester(getRequestSettings()); } /** @@ -142,11 +150,17 @@ public class OpenWeatherMapClient { */ @SubscriptionAvailability(plans = ALL) public AirPollutionRequester airPollution() { - return new AirPollutionRequester(new RequestSettings(apiKey, timeoutSettings)); + return new AirPollutionRequester(getRequestSettings()); } @SubscriptionAvailability(plans = ALL) public GeocodingRequester geocoding() { - return new GeocodingRequester(new RequestSettings(apiKey, timeoutSettings)); + return new GeocodingRequester(getRequestSettings()); + } + + private RequestSettings getRequestSettings() { + final RequestSettings requestSettings = new RequestSettings(apiKey, timeoutSettings); + requestSettings.setHttpClient(httpClient); + return requestSettings; } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpClient.java b/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpClient.java new file mode 100644 index 0000000..096ff73 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpClient.java @@ -0,0 +1,10 @@ +package com.github.prominence.openweathermap.api.core.net; + +import com.github.prominence.openweathermap.api.conf.TimeoutSettings; + +public interface HttpClient { + void setTimeoutSettings(TimeoutSettings timeoutSettings); + + String executeGetRequest(String url); + String executePostRequest(String url, String body); +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpURLConnectionBasedHttpClient.java b/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpURLConnectionBasedHttpClient.java new file mode 100644 index 0000000..579e67f --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/core/net/HttpURLConnectionBasedHttpClient.java @@ -0,0 +1,126 @@ +package com.github.prominence.openweathermap.api.core.net; + +import com.github.prominence.openweathermap.api.conf.TimeoutSettings; +import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; +import com.github.prominence.openweathermap.api.exception.NoDataFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +public class HttpURLConnectionBasedHttpClient implements HttpClient { + private static final Logger logger = LoggerFactory.getLogger(HttpURLConnectionBasedHttpClient.class); + + private TimeoutSettings timeoutSettings; + + @Override + public void setTimeoutSettings(TimeoutSettings timeoutSettings) { + this.timeoutSettings = timeoutSettings; + } + + @Override + public String executeGetRequest(String url) { + InputStream resultStream; + + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + + if (timeoutSettings != null) { + if (timeoutSettings.getConnectionTimeout() != null) { + connection.setConnectTimeout(timeoutSettings.getConnectionTimeout()); + } + + if (timeoutSettings.getReadTimeout() != null) { + connection.setReadTimeout(timeoutSettings.getReadTimeout()); + } + } + + connection.setRequestMethod("GET"); + connection.setRequestProperty("Content-Type", "application/json; utf-8"); + connection.setRequestProperty("Accept", "application/json"); + + resultStream = switch (connection.getResponseCode()) { + case HttpURLConnection.HTTP_OK -> connection.getInputStream(); + case HttpURLConnection.HTTP_UNAUTHORIZED -> throw new InvalidAuthTokenException(); + case HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST -> + throw new NoDataFoundException(); + default -> throw new IllegalStateException("Unexpected value: " + connection.getResponseCode()); + }; + } catch (IllegalStateException | IOException ex) { + logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex); + throw new NoDataFoundException(ex); + } + logger.debug("Executing OpenWeatherMap API request: " + url); + + return convertInputStreamToString(resultStream); + } + + @Override + public String executePostRequest(String url, String body) { + InputStream resultStream; + + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + + if (timeoutSettings != null) { + if (timeoutSettings.getConnectionTimeout() != null) { + connection.setConnectTimeout(timeoutSettings.getConnectionTimeout()); + } + + if (timeoutSettings.getReadTimeout() != null) { + connection.setReadTimeout(timeoutSettings.getReadTimeout()); + } + } + + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json; utf-8"); + connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); + + try(OutputStream os = connection.getOutputStream()) { + byte[] input = body.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + resultStream = switch (connection.getResponseCode()) { + case HttpURLConnection.HTTP_OK -> connection.getInputStream(); + case HttpURLConnection.HTTP_UNAUTHORIZED -> throw new InvalidAuthTokenException(); + case HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST -> + throw new NoDataFoundException(); + default -> throw new IllegalStateException("Unexpected value: " + connection.getResponseCode()); + }; + } catch (IllegalStateException | IOException ex) { + logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex); + throw new NoDataFoundException(ex); + } + logger.debug("Executing OpenWeatherMap API request: " + url); + + return convertInputStreamToString(resultStream); + } + + /** + * Reads the input stream line-by-line and returns its content in String representation. + * + * @param inputStream input stream to convert. + * @return converted InputStream content. + * @throws IllegalArgumentException in case if input stream is unable to be read. + */ + private static String convertInputStreamToString(InputStream inputStream) { + StringBuilder result = new StringBuilder(); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + } + } catch (IOException ex) { + logger.error("Error during response reading: ", ex); + throw new IllegalArgumentException(ex); + } + + return result.toString(); + } +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/core/net/RequestExecutor.java b/src/main/java/com/github/prominence/openweathermap/api/core/net/RequestExecutor.java new file mode 100644 index 0000000..6ce4947 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/core/net/RequestExecutor.java @@ -0,0 +1,97 @@ +/* + * 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.core.net; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.github.prominence.openweathermap.api.request.RequestSettings; + +import java.net.URL; +import java.util.stream.Collectors; + +public final class RequestExecutor { + private static final String OWM_URL_BASE = "https://SUBDOMAIN.openweathermap.org/"; + + private final RequestSettings requestSettings; + + public RequestExecutor(RequestSettings requestSettings) { + this.requestSettings = requestSettings; + } + + public String getResponse() { + return getResponse(Method.GET); + } + + public String getResponse(Method httpMethod) { + return getResponse(buildRequestUrl(), httpMethod); + } + + /** + * Executes call to provided API url and retrieves response in String representation. + * + * @param url the url to make API request. + * @param httpMethod HTTP method to execute. + * @return response from the request in String representation. + * @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance. + */ + private String getResponse(String url, Method httpMethod) { + final HttpClient httpClient = requestSettings.getHttpClient(); + httpClient.setTimeoutSettings(requestSettings.getTimeoutSettings()); + + if (httpMethod == Method.GET) { + return httpClient.executeGetRequest(url); + } else { + return httpClient.executePostRequest(url, getSerializedPayload()); + } + } + + private String buildRequestUrl() { + StringBuilder requestUrlBuilder = new StringBuilder(OWM_URL_BASE.replace("SUBDOMAIN", requestSettings.getSubdomain())); + 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 requestUrlBuilder.toString(); + } + + private String getSerializedPayload() { + final ObjectMapper objectMapper = new ObjectMapper(); + final SimpleModule module = new SimpleModule(); + module.addSerializer(requestSettings.getPayloadClass(), requestSettings.getPayloadSerializer()); + objectMapper.registerModule(module); + + try { + return objectMapper.writeValueAsString(requestSettings.getPayloadObject()); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + public enum Method { + GET, + POST + } +} 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 index f4599e8..388b5a3 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/RequestSettings.java @@ -22,14 +22,14 @@ package com.github.prominence.openweathermap.api.request; +import com.fasterxml.jackson.databind.JsonSerializer; import com.github.prominence.openweathermap.api.conf.TimeoutSettings; +import com.github.prominence.openweathermap.api.core.net.HttpClient; 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 com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint; import java.util.HashMap; -import java.util.List; import java.util.Map; public class RequestSettings { @@ -43,10 +43,14 @@ public class RequestSettings { private final Map requestParameters = new HashMap<>(8); - private final Map requestBody = new HashMap<>(); - private final StringBuilder urlAppenderBuilder = new StringBuilder(); + private Object payloadObject; + private Class payloadClass; + private JsonSerializer payloadSerializer; + + private HttpClient httpClient; + private String subdomain = "api"; private Language language = Language.ENGLISH; @@ -62,6 +66,14 @@ public class RequestSettings { return timeoutSettings; } + public HttpClient getHttpClient() { + return httpClient; + } + + public void setHttpClient(HttpClient httpClient) { + this.httpClient = httpClient; + } + public String getSubdomain() { return subdomain; } @@ -112,7 +124,27 @@ public class RequestSettings { return urlAppenderBuilder; } - public void addToRequestBody(String key, Object object) { - requestBody.put(key, object); + public void setPayloadObject(Object payloadObject) { + this.payloadObject = payloadObject; + } + + public Object getPayloadObject() { + return payloadObject; + } + + public Class getPayloadClass() { + return payloadClass; + } + + public void setPayloadClass(Class payloadClass) { + this.payloadClass = payloadClass; + } + + public JsonSerializer getPayloadSerializer() { + return payloadSerializer; + } + + public void setPayloadSerializer(JsonSerializer payloadSerializer) { + this.payloadSerializer = payloadSerializer; } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionAsyncRequestTerminator.java index e09a9e4..d1fcfc6 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionAsyncRequestTerminator.java @@ -24,10 +24,10 @@ package com.github.prominence.openweathermap.api.request.air.pollution; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.AirPollutionResponseMapper; import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -55,6 +55,6 @@ public class AirPollutionAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequestTerminator.java index 5582f9d..9dc7b95 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/air/pollution/AirPollutionRequestTerminator.java @@ -24,10 +24,10 @@ package com.github.prominence.openweathermap.api.request.air.pollution; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.AirPollutionResponseMapper; import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; /** * The type Air pollution request terminator. @@ -53,6 +53,6 @@ public class AirPollutionRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastAsyncRequestTerminator.java index 12f4f2c..8fc0b6e 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.climatic; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -51,6 +51,6 @@ class ClimaticForecastAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastRequestTerminator.java index bfd8197..1076676 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/climatic/ClimaticForecastRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.climatic; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; class ClimaticForecastRequestTerminator { private final RequestSettings requestSettings; @@ -49,6 +49,6 @@ class ClimaticForecastRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastAsyncRequestTerminator.java index 7d873ad..da2b56f 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.daily; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -51,6 +51,6 @@ class DailyForecastAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastRequestTerminator.java index 627780f..8c53237 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/daily/DailyForecastRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.daily; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; class DailyForecastRequestTerminator { private final RequestSettings requestSettings; @@ -49,6 +49,6 @@ class DailyForecastRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastAsyncRequestTerminator.java index c9e42dc..75e49b0 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.free; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.free.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -59,6 +59,6 @@ class FiveDayThreeHourStepForecastAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequestTerminator.java index 3f6f37a..20ccb5b 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.free; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.free.Forecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; /** * The forecast request terminator. @@ -57,6 +57,6 @@ class FiveDayThreeHourStepForecastRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastAsyncRequestTerminator.java index f0e7cf8..6b98031 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.hourly; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -51,6 +51,6 @@ class FourDaysHourlyForecastAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastRequestTerminator.java index af68410..7e13fa4 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/forecast/hourly/FourDaysHourlyForecastRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.forecast.hourly; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper; import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; class FourDaysHourlyForecastRequestTerminator { private final RequestSettings requestSettings; @@ -49,6 +49,6 @@ class FourDaysHourlyForecastRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestAsyncTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestAsyncTerminator.java index efcf8a2..b346738 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestAsyncTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestAsyncTerminator.java @@ -22,8 +22,8 @@ package com.github.prominence.openweathermap.api.request.geocoding.direct; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -46,6 +46,6 @@ public class DirectGeocodingRequestAsyncTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestTerminator.java index a7388f4..e74e052 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/direct/DirectGeocodingRequestTerminator.java @@ -22,8 +22,8 @@ package com.github.prominence.openweathermap.api.request.geocoding.direct; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.function.Function; @@ -45,6 +45,6 @@ public class DirectGeocodingRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestAsyncTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestAsyncTerminator.java index 09e530b..7f0b7ff 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestAsyncTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestAsyncTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.geocoding.reverse; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.GeocodingResponseMapper; import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -46,6 +46,6 @@ public class ReverseGeocodingRequestAsyncTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestTerminator.java index 6e61b5a..e692a24 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/geocoding/reverse/ReverseGeocodingRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.geocoding.reverse; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.GeocodingResponseMapper; import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.List; @@ -45,6 +45,6 @@ public class ReverseGeocodingRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherAsyncRequestTerminator.java index d0ecc70..30edf24 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherAsyncRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.onecall.current; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.OneCallWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -53,6 +53,6 @@ class OneCallCurrentWeatherAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherRequestTerminator.java index 81b7f27..60d8df9 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/current/OneCallCurrentWeatherRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.onecall.current; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.OneCallWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; /** * The type One call current weather request terminator. @@ -51,6 +51,6 @@ class OneCallCurrentWeatherRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherAsyncRequestTerminator.java index 6be81d7..0a5041e 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherAsyncRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.onecall.historical; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.OneCallWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -53,6 +53,6 @@ class OneCallHistoricalWeatherAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherRequestTerminator.java index 7aa2461..45381d8 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/onecall/historical/OneCallHistoricalWeatherRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.onecall.historical; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.OneCallWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; /** * The type One call historical weather request terminator. @@ -51,6 +51,6 @@ class OneCallHistoricalWeatherRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationAsyncRequestTerminator.java index 97fde0f..5210338 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationAsyncRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.radiation; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.SolarRadiationResponseMapper; import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -45,6 +45,6 @@ class SolarRadiationAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationRequestTerminator.java index 55a1699..ab28ce5 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/radiation/SolarRadiationRequestTerminator.java @@ -22,10 +22,10 @@ package com.github.prominence.openweathermap.api.request.radiation; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.mapper.SolarRadiationResponseMapper; import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; class SolarRadiationRequestTerminator { private final RequestSettings requestSettings; @@ -43,6 +43,6 @@ class SolarRadiationRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskAsyncRequestTerminator.java index 7ff68b7..55bb327 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.roadrisk; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper; import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -65,6 +65,6 @@ public class RoadRiskAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequestTerminator.java index 1cc869a..2394c63 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.roadrisk; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper; import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.List; @@ -56,6 +56,6 @@ public class RoadRiskRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(RequestExecutor.Method.POST); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequester.java b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequester.java index 0789c17..29a28ca 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequester.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskRequester.java @@ -2,6 +2,8 @@ package com.github.prominence.openweathermap.api.request.roadrisk; import com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint; import com.github.prominence.openweathermap.api.request.RequestSettings; +import com.github.prominence.openweathermap.api.request.roadrisk.model.RoadRiskRequestPayload; +import com.github.prominence.openweathermap.api.serializer.RoadRiskRequestSerializer; import java.util.List; @@ -14,7 +16,9 @@ public class RoadRiskRequester { } public RoadRiskRequestCustomizer byTrackPoints(List trackPoints) { - requestSettings.addToRequestBody("track", trackPoints); + requestSettings.setPayloadObject(new RoadRiskRequestPayload(trackPoints)); + requestSettings.setPayloadClass(RoadRiskRequestPayload.class); + requestSettings.setPayloadSerializer(new RoadRiskRequestSerializer()); return new RoadRiskRequestCustomizer(requestSettings); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/model/RoadRiskRequestPayload.java b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/model/RoadRiskRequestPayload.java new file mode 100644 index 0000000..77093c2 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/request/roadrisk/model/RoadRiskRequestPayload.java @@ -0,0 +1,17 @@ +package com.github.prominence.openweathermap.api.request.roadrisk.model; + +import com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint; + +import java.util.List; + +public class RoadRiskRequestPayload { + private final List payload; + + public RoadRiskRequestPayload(List payload) { + this.payload = payload; + } + + public List getPayload() { + return payload; + } +} diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherAsyncRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherAsyncRequestTerminator.java index 2c3bfef..694de21 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherAsyncRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherAsyncRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.weather; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.weather.Weather; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; import java.util.concurrent.CompletableFuture; @@ -64,6 +64,6 @@ public class CurrentWeatherAsyncRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequestTerminator.java b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequestTerminator.java index 639779f..3e7e45f 100644 --- a/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequestTerminator.java +++ b/src/main/java/com/github/prominence/openweathermap/api/request/weather/CurrentWeatherRequestTerminator.java @@ -22,11 +22,11 @@ package com.github.prominence.openweathermap.api.request.weather; +import com.github.prominence.openweathermap.api.core.net.RequestExecutor; import com.github.prominence.openweathermap.api.enums.ResponseType; import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper; import com.github.prominence.openweathermap.api.model.weather.Weather; import com.github.prominence.openweathermap.api.request.RequestSettings; -import com.github.prominence.openweathermap.api.utils.RequestUtils; /** * The type Single result current weather request terminator. @@ -62,6 +62,6 @@ public class CurrentWeatherRequestTerminator { } private String getRawResponse() { - return RequestUtils.getResponse(requestSettings); + return new RequestExecutor(requestSettings).getResponse(); } } diff --git a/src/main/java/com/github/prominence/openweathermap/api/serializer/RoadRiskRequestSerializer.java b/src/main/java/com/github/prominence/openweathermap/api/serializer/RoadRiskRequestSerializer.java new file mode 100644 index 0000000..0dd9990 --- /dev/null +++ b/src/main/java/com/github/prominence/openweathermap/api/serializer/RoadRiskRequestSerializer.java @@ -0,0 +1,35 @@ +package com.github.prominence.openweathermap.api.serializer; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.github.prominence.openweathermap.api.model.Coordinates; +import com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint; +import com.github.prominence.openweathermap.api.request.roadrisk.model.RoadRiskRequestPayload; + +import java.io.IOException; +import java.time.ZoneId; +import java.util.List; + +public class RoadRiskRequestSerializer extends JsonSerializer { + @Override + public void serialize(RoadRiskRequestPayload roadRiskRequestPayload, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeStartObject(); + jsonGenerator.writeArrayFieldStart("track"); + + final List trackPoints = roadRiskRequestPayload.getPayload(); + for (TrackPoint point : trackPoints) { + jsonGenerator.writeStartObject(); + jsonGenerator.writeNumberField("dt", point.getRequestedTime().atZone(ZoneId.systemDefault()).toEpochSecond()); + + final Coordinates coordinates = point.getCoordinates(); + jsonGenerator.writeNumberField("lat", coordinates.getLatitude()); + jsonGenerator.writeNumberField("lon", coordinates.getLongitude()); + + jsonGenerator.writeEndObject(); + } + + jsonGenerator.writeEndArray(); + jsonGenerator.writeEndObject(); + } +} 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 deleted file mode 100644 index 8563403..0000000 --- a/src/main/java/com/github/prominence/openweathermap/api/utils/RequestUtils.java +++ /dev/null @@ -1,161 +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.utils; - -import com.github.prominence.openweathermap.api.conf.TimeoutSettings; -import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException; -import com.github.prominence.openweathermap.api.exception.NoDataFoundException; -import com.github.prominence.openweathermap.api.request.RequestSettings; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -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 = "https://SUBDOMAIN.openweathermap.org/"; - - private static final Logger logger = LoggerFactory.getLogger(RequestUtils.class); - - private RequestUtils() { - } - - public static String getResponse(RequestSettings requestSettings) { - StringBuilder requestUrlBuilder = new StringBuilder(OWM_URL_BASE.replace("SUBDOMAIN", requestSettings.getSubdomain())); - 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. - * - * @param url the url to make API request. - * @return response from the request in String representation. - * @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance. - */ - @Deprecated - 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); - } catch (MalformedURLException ex) { - logger.error("Invalid URL: ", ex); - throw new IllegalArgumentException(ex); - } - logger.debug("Executing OpenWeatherMap API request: " + url); - final InputStream requestInputStream = executeRequest(requestUrl, timeoutSettings); - - return convertInputStreamToString(requestInputStream); - } - - /** - * Executes call to provided API url and retrieves response as an InputStream instance. - * - * @param requestUrl url for API call execution. - * @return InputStream instance containing http response body. - * @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, 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"); - - resultStream = switch (connection.getResponseCode()) { - case HttpURLConnection.HTTP_OK -> connection.getInputStream(); - case HttpURLConnection.HTTP_UNAUTHORIZED -> throw new InvalidAuthTokenException(); - case HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST -> - throw new NoDataFoundException(); - default -> throw new IllegalStateException("Unexpected value: " + connection.getResponseCode()); - }; - } catch (IllegalStateException | IOException ex) { - logger.error("An error occurred during OpenWeatherMap API response parsing: ", ex); - throw new NoDataFoundException(ex); - } - - return resultStream; - } - - /** - * Reads the input stream line-by-line and returns its content in String representation. - * - * @param inputStream input stream to convert. - * @return converted InputStream content. - * @throws IllegalArgumentException in case if input stream is unable to be read. - */ - private static String convertInputStreamToString(InputStream inputStream) { - StringBuilder result = new StringBuilder(); - - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - } - } catch (IOException ex) { - logger.error("Error during response reading: ", ex); - throw new IllegalArgumentException(ex); - } - - return result.toString(); - } -} diff --git a/src/test/java/com/github/prominence/openweathermap/api/core/net/MockHttpClient.java b/src/test/java/com/github/prominence/openweathermap/api/core/net/MockHttpClient.java new file mode 100644 index 0000000..45c6407 --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/core/net/MockHttpClient.java @@ -0,0 +1,31 @@ +package com.github.prominence.openweathermap.api.core.net; + +import com.github.prominence.openweathermap.api.conf.TimeoutSettings; + +public class MockHttpClient implements HttpClient { + private String responseOutput; + + @Override + public void setTimeoutSettings(TimeoutSettings timeoutSettings) { + + } + + @Override + public String executeGetRequest(String url) { + System.out.println("Executing request to " + url); + + return responseOutput; + } + + @Override + public String executePostRequest(String url, String body) { + System.out.println("Executing request to " + url); + System.out.println("Request body: " + body); + + return responseOutput; + } + + public void setResponseOutput(String responseOutput) { + this.responseOutput = responseOutput; + } +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/mapper/FiveDayThreeHourStepForecastResponseMapperTest.java b/src/test/java/com/github/prominence/openweathermap/api/mapper/FiveDayThreeHourStepForecastResponseMapperTest.java index 8869056..b37e57b 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/mapper/FiveDayThreeHourStepForecastResponseMapperTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/mapper/FiveDayThreeHourStepForecastResponseMapperTest.java @@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test; import java.time.ZoneOffset; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; class FiveDayThreeHourStepForecastResponseMapperTest { diff --git a/src/test/java/com/github/prominence/openweathermap/api/mapper/RoadRiskResponseMapperTest.java b/src/test/java/com/github/prominence/openweathermap/api/mapper/RoadRiskResponseMapperTest.java index 0ff37c0..0817a8c 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/mapper/RoadRiskResponseMapperTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/mapper/RoadRiskResponseMapperTest.java @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; class RoadRiskResponseMapperTest { diff --git a/src/test/java/com/github/prominence/openweathermap/api/mapper/SolarRadiationResponseMapperTest.java b/src/test/java/com/github/prominence/openweathermap/api/mapper/SolarRadiationResponseMapperTest.java index 83fd3c6..22bbb66 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/mapper/SolarRadiationResponseMapperTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/mapper/SolarRadiationResponseMapperTest.java @@ -8,7 +8,8 @@ import org.junit.jupiter.api.Test; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; class SolarRadiationResponseMapperTest { diff --git a/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastResponseMapperUnitTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastResponseMapperUnitTest.java index 0f1fb6f..f29a53b 100644 --- a/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastResponseMapperUnitTest.java +++ b/src/test/java/com/github/prominence/openweathermap/api/request/forecast/free/FiveDayThreeHourStepForecastResponseMapperUnitTest.java @@ -56,7 +56,7 @@ public class FiveDayThreeHourStepForecastResponseMapperUnitTest { assertNotNull(forecast); assertNotNull(forecast.getLocation()); assertNotNull(forecast.getWeatherForecasts()); - forecast.getWeatherForecasts().forEach(weatherForecast -> assertNull(weatherForecast.getWeatherStates())); + forecast.getWeatherForecasts().forEach(weatherForecast -> assertEquals(0, weatherForecast.getWeatherStates().size())); } @Test diff --git a/src/test/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskIntegrationTest.java b/src/test/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskIntegrationTest.java new file mode 100644 index 0000000..1f09e0a --- /dev/null +++ b/src/test/java/com/github/prominence/openweathermap/api/request/roadrisk/RoadRiskIntegrationTest.java @@ -0,0 +1,87 @@ +package com.github.prominence.openweathermap.api.request.roadrisk; + +import com.github.prominence.openweathermap.api.ApiTest; +import com.github.prominence.openweathermap.api.core.net.MockHttpClient; +import com.github.prominence.openweathermap.api.model.Coordinates; +import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord; +import com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.util.List; + +public class RoadRiskIntegrationTest extends ApiTest { + private static final MockHttpClient httpClient = new MockHttpClient(); + + @BeforeAll + public static void setup() { + getClient().setHttpClient(httpClient); + } + + @Test + public void whenGetSingleCurrentWeatherByCoordinateRequestAsJava_thenReturnNotNull() { + final TrackPoint trackPoint = new TrackPoint(); + trackPoint.setCoordinates(Coordinates.of(5, 5)); + trackPoint.setRequestedTime(LocalDateTime.now()); + + final String responseOutput = """ + [ + { + "dt": 1602702000, + "coord": [ + 7.27, + 44.04 + ], + "weather": { + "temp": 278.44, + "wind_speed": 2.27, + "wind_deg": 7, + "precipitation_intensity": 0.38, + "dew_point": 276.13 + }, + "road": { + "state": 2, + "temp": 293.85 + }, + "alerts": [ + { + "sender_name": "METEO-FRANCE", + "event": "Moderate thunderstorm warning", + "event_level": 2 + } + ] + }, + { + "dt": 1602702400, + "coord": [ + 7.37, + 45.04 + ], + "weather": { + "temp": 282.44, + "wind_speed": 1.84, + "wind_deg": 316, + "dew_point": 275.99 + }, + "road": { + "state": 1, + "temp": 293.85 + }, + "alerts": [ + ] + } + ] + """; + httpClient.setResponseOutput(responseOutput); + + final List roadRiskRecords = getClient() + .roadRisk() + .byTrackPoints(List.of(trackPoint)) + .retrieve() + .asJava(); + + System.out.println(roadRiskRecords); + } + +} diff --git a/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java b/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java deleted file mode 100644 index 41264c4..0000000 --- a/src/test/java/com/github/prominence/openweathermap/api/utils/RequestUtilsUnitTest.java +++ /dev/null @@ -1,40 +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.utils; - -import com.github.prominence.openweathermap.api.exception.NoDataFoundException; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class RequestUtilsUnitTest { - @Test - public void whenPassInvalidUrl_thenThrowAnException() { - assertThrows(IllegalArgumentException.class, () -> RequestUtils.getResponse("wrongUrl")); - } - - @Test - public void whenPassUrlToNonExistingPage_thenThrowAnException() { - assertThrows(NoDataFoundException.class, () -> RequestUtils.getResponse("https://openweathermap.org/somePage")); - } -}