mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-10 11:56:44 +03:00
Reimplemented request executor. Added possibility to test requests with mocked http requests.
This commit is contained in:
parent
6c1a1642f1
commit
5e4b34632e
@ -12,7 +12,7 @@ Free:
|
|||||||
Paid:
|
Paid:
|
||||||
* Hourly Forecast 4 days
|
* Hourly Forecast 4 days
|
||||||
* Daily Forecast 16 days
|
* Daily Forecast 16 days
|
||||||
* Climatic 30 days
|
* Climatic Forecast 30 days
|
||||||
* Solar Radiation API
|
* Solar Radiation API
|
||||||
* Road Risk API
|
* Road Risk API
|
||||||
|
|
||||||
@ -26,11 +26,8 @@ Free:
|
|||||||
* Weather Triggers
|
* Weather Triggers
|
||||||
|
|
||||||
Paid:
|
Paid:
|
||||||
* Climatic Forecast 30 days
|
|
||||||
* Bulk Downloading
|
* Bulk Downloading
|
||||||
* Solar Radiation API
|
|
||||||
* Global Weather Alerts / Push notifications
|
* Global Weather Alerts / Push notifications
|
||||||
* Road Risk API
|
|
||||||
|
|
||||||
### Maven coordinates:
|
### Maven coordinates:
|
||||||
|
|
||||||
|
|||||||
@ -24,6 +24,8 @@ package com.github.prominence.openweathermap.api;
|
|||||||
|
|
||||||
import com.github.prominence.openweathermap.api.annotation.SubscriptionAvailability;
|
import com.github.prominence.openweathermap.api.annotation.SubscriptionAvailability;
|
||||||
import com.github.prominence.openweathermap.api.conf.TimeoutSettings;
|
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.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.request.air.pollution.AirPollutionRequester;
|
import com.github.prominence.openweathermap.api.request.air.pollution.AirPollutionRequester;
|
||||||
import com.github.prominence.openweathermap.api.request.forecast.climatic.ClimaticForecastRequester;
|
import com.github.prominence.openweathermap.api.request.forecast.climatic.ClimaticForecastRequester;
|
||||||
@ -46,6 +48,8 @@ public class OpenWeatherMapClient {
|
|||||||
private final String apiKey;
|
private final String apiKey;
|
||||||
private final TimeoutSettings timeoutSettings = new TimeoutSettings();
|
private final TimeoutSettings timeoutSettings = new TimeoutSettings();
|
||||||
|
|
||||||
|
private HttpClient httpClient = new HttpURLConnectionBasedHttpClient();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created OpenWeatherMap client object.
|
* Created OpenWeatherMap client object.
|
||||||
* @param apiKey API key obtained on <a href="https://home.openweathermap.org/api_keys">OpenWeatherMap site</a>.
|
* @param apiKey API key obtained on <a href="https://home.openweathermap.org/api_keys">OpenWeatherMap site</a>.
|
||||||
@ -62,13 +66,17 @@ public class OpenWeatherMapClient {
|
|||||||
timeoutSettings.setReadTimeout(readTimeout);
|
timeoutSettings.setReadTimeout(readTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHttpClient(HttpClient httpClient) {
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current Weather <a href="https://openweathermap.org/current">API</a>.
|
* Current Weather <a href="https://openweathermap.org/current">API</a>.
|
||||||
* @return requester for retrieving current weather information.
|
* @return requester for retrieving current weather information.
|
||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = ALL)
|
@SubscriptionAvailability(plans = ALL)
|
||||||
public CurrentWeatherRequester currentWeather() {
|
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 })
|
@SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE })
|
||||||
public FourDaysHourlyForecastRequester forecastHourly4Days() {
|
public FourDaysHourlyForecastRequester forecastHourly4Days() {
|
||||||
return new FourDaysHourlyForecastRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new FourDaysHourlyForecastRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,7 +95,7 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = ALL)
|
@SubscriptionAvailability(plans = ALL)
|
||||||
public OneCallWeatherRequester oneCall() {
|
public OneCallWeatherRequester oneCall() {
|
||||||
return new OneCallWeatherRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new OneCallWeatherRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,7 +104,7 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = PAID)
|
@SubscriptionAvailability(plans = PAID)
|
||||||
public DailyForecastRequester forecastDaily16Days() {
|
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 })
|
@SubscriptionAvailability(plans = { DEVELOPER, PROFESSIONAL, ENTERPRISE })
|
||||||
public ClimaticForecastRequester climaticForecast30Days() {
|
public ClimaticForecastRequester climaticForecast30Days() {
|
||||||
return new ClimaticForecastRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new ClimaticForecastRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,7 +122,7 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = SPECIAL)
|
@SubscriptionAvailability(plans = SPECIAL)
|
||||||
public SolarRadiationRequester solarRadiation() {
|
public SolarRadiationRequester solarRadiation() {
|
||||||
return new SolarRadiationRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new SolarRadiationRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,7 +131,7 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = ALL)
|
@SubscriptionAvailability(plans = ALL)
|
||||||
public FiveDayThreeHourStepForecastRequester forecast5Day3HourStep() {
|
public FiveDayThreeHourStepForecastRequester forecast5Day3HourStep() {
|
||||||
return new FiveDayThreeHourStepForecastRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new FiveDayThreeHourStepForecastRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,7 +140,7 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = SPECIAL)
|
@SubscriptionAvailability(plans = SPECIAL)
|
||||||
public RoadRiskRequester roadRisk() {
|
public RoadRiskRequester roadRisk() {
|
||||||
return new RoadRiskRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new RoadRiskRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -142,11 +150,17 @@ public class OpenWeatherMapClient {
|
|||||||
*/
|
*/
|
||||||
@SubscriptionAvailability(plans = ALL)
|
@SubscriptionAvailability(plans = ALL)
|
||||||
public AirPollutionRequester airPollution() {
|
public AirPollutionRequester airPollution() {
|
||||||
return new AirPollutionRequester(new RequestSettings(apiKey, timeoutSettings));
|
return new AirPollutionRequester(getRequestSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscriptionAvailability(plans = ALL)
|
@SubscriptionAvailability(plans = ALL)
|
||||||
public GeocodingRequester geocoding() {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
@ -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 <code>String</code> representation.
|
||||||
|
*
|
||||||
|
* @param inputStream input stream to convert.
|
||||||
|
* @return converted <code>InputStream</code> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 <code>String</code> representation.
|
||||||
|
*
|
||||||
|
* @param url the url to make API request.
|
||||||
|
* @param httpMethod HTTP method to execute.
|
||||||
|
* @return response from the request in <code>String</code> representation.
|
||||||
|
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -22,14 +22,14 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request;
|
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.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.Language;
|
||||||
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
import com.github.prominence.openweathermap.api.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
import com.github.prominence.openweathermap.api.enums.UnitSystem;
|
||||||
import com.github.prominence.openweathermap.api.model.roadrisk.TrackPoint;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class RequestSettings {
|
public class RequestSettings {
|
||||||
@ -43,10 +43,14 @@ public class RequestSettings {
|
|||||||
|
|
||||||
private final Map<String, String> requestParameters = new HashMap<>(8);
|
private final Map<String, String> requestParameters = new HashMap<>(8);
|
||||||
|
|
||||||
private final Map<String, Object> requestBody = new HashMap<>();
|
|
||||||
|
|
||||||
private final StringBuilder urlAppenderBuilder = new StringBuilder();
|
private final StringBuilder urlAppenderBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
private Object payloadObject;
|
||||||
|
private Class payloadClass;
|
||||||
|
private JsonSerializer payloadSerializer;
|
||||||
|
|
||||||
|
private HttpClient httpClient;
|
||||||
|
|
||||||
private String subdomain = "api";
|
private String subdomain = "api";
|
||||||
|
|
||||||
private Language language = Language.ENGLISH;
|
private Language language = Language.ENGLISH;
|
||||||
@ -62,6 +66,14 @@ public class RequestSettings {
|
|||||||
return timeoutSettings;
|
return timeoutSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpClient getHttpClient() {
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpClient(HttpClient httpClient) {
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSubdomain() {
|
public String getSubdomain() {
|
||||||
return subdomain;
|
return subdomain;
|
||||||
}
|
}
|
||||||
@ -112,7 +124,27 @@ public class RequestSettings {
|
|||||||
return urlAppenderBuilder;
|
return urlAppenderBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToRequestBody(String key, Object object) {
|
public void setPayloadObject(Object payloadObject) {
|
||||||
requestBody.put(key, object);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,10 +24,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.air.pollution;
|
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.mapper.AirPollutionResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails;
|
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -55,6 +55,6 @@ public class AirPollutionAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,10 +24,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.air.pollution;
|
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.mapper.AirPollutionResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails;
|
import com.github.prominence.openweathermap.api.model.air.pollution.AirPollutionDetails;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type Air pollution request terminator.
|
* The type Air pollution request terminator.
|
||||||
@ -53,6 +53,6 @@ public class AirPollutionRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.climatic;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -51,6 +51,6 @@ class ClimaticForecastAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.climatic;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.ClimaticForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.climatic.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
class ClimaticForecastRequestTerminator {
|
class ClimaticForecastRequestTerminator {
|
||||||
private final RequestSettings requestSettings;
|
private final RequestSettings requestSettings;
|
||||||
@ -49,6 +49,6 @@ class ClimaticForecastRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.daily;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -51,6 +51,6 @@ class DailyForecastAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.daily;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.DailyForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.daily.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
class DailyForecastRequestTerminator {
|
class DailyForecastRequestTerminator {
|
||||||
private final RequestSettings requestSettings;
|
private final RequestSettings requestSettings;
|
||||||
@ -49,6 +49,6 @@ class DailyForecastRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.free;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.free.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.free.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -59,6 +59,6 @@ class FiveDayThreeHourStepForecastAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.free;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.FiveDayThreeHourStepForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.free.Forecast;
|
import com.github.prominence.openweathermap.api.model.forecast.free.Forecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The forecast request terminator.
|
* The forecast request terminator.
|
||||||
@ -57,6 +57,6 @@ class FiveDayThreeHourStepForecastRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.hourly;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast;
|
import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -51,6 +51,6 @@ class FourDaysHourlyForecastAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.forecast.hourly;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.HourlyForecastResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast;
|
import com.github.prominence.openweathermap.api.model.forecast.hourly.HourlyForecast;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
class FourDaysHourlyForecastRequestTerminator {
|
class FourDaysHourlyForecastRequestTerminator {
|
||||||
private final RequestSettings requestSettings;
|
private final RequestSettings requestSettings;
|
||||||
@ -49,6 +49,6 @@ class FourDaysHourlyForecastRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,8 +22,8 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.geocoding.direct;
|
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.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@ -46,6 +46,6 @@ public class DirectGeocodingRequestAsyncTerminator<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,8 +22,8 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.geocoding.direct;
|
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.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@ -45,6 +45,6 @@ public class DirectGeocodingRequestTerminator<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.geocoding.reverse;
|
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.mapper.GeocodingResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
|
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@ -46,6 +46,6 @@ public class ReverseGeocodingRequestAsyncTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.geocoding.reverse;
|
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.mapper.GeocodingResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
|
import com.github.prominence.openweathermap.api.model.geocoding.GeocodingRecord;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -45,6 +45,6 @@ public class ReverseGeocodingRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.onecall.current;
|
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.mapper.OneCallWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData;
|
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -53,6 +53,6 @@ class OneCallCurrentWeatherAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.onecall.current;
|
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.mapper.OneCallWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData;
|
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type One call current weather request terminator.
|
* The type One call current weather request terminator.
|
||||||
@ -51,6 +51,6 @@ class OneCallCurrentWeatherRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.onecall.historical;
|
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.mapper.OneCallWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData;
|
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -53,6 +53,6 @@ class OneCallHistoricalWeatherAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.onecall.historical;
|
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.mapper.OneCallWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData;
|
import com.github.prominence.openweathermap.api.model.onecall.historical.HistoricalWeatherData;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type One call historical weather request terminator.
|
* The type One call historical weather request terminator.
|
||||||
@ -51,6 +51,6 @@ class OneCallHistoricalWeatherRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.radiation;
|
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.mapper.SolarRadiationResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation;
|
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -45,6 +45,6 @@ class SolarRadiationAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,10 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.radiation;
|
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.mapper.SolarRadiationResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation;
|
import com.github.prominence.openweathermap.api.model.radiation.SolarRadiation;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
class SolarRadiationRequestTerminator {
|
class SolarRadiationRequestTerminator {
|
||||||
private final RequestSettings requestSettings;
|
private final RequestSettings requestSettings;
|
||||||
@ -43,6 +43,6 @@ class SolarRadiationRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.roadrisk;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord;
|
import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@ -65,6 +65,6 @@ public class RoadRiskAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.roadrisk;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.RoadRiskResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord;
|
import com.github.prominence.openweathermap.api.model.roadrisk.RoadRiskRecord;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -56,6 +56,6 @@ public class RoadRiskRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse(RequestExecutor.Method.POST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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.model.roadrisk.TrackPoint;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
@ -14,7 +16,9 @@ public class RoadRiskRequester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public RoadRiskRequestCustomizer byTrackPoints(List<TrackPoint> trackPoints) {
|
public RoadRiskRequestCustomizer byTrackPoints(List<TrackPoint> trackPoints) {
|
||||||
requestSettings.addToRequestBody("track", trackPoints);
|
requestSettings.setPayloadObject(new RoadRiskRequestPayload(trackPoints));
|
||||||
|
requestSettings.setPayloadClass(RoadRiskRequestPayload.class);
|
||||||
|
requestSettings.setPayloadSerializer(new RoadRiskRequestSerializer());
|
||||||
return new RoadRiskRequestCustomizer(requestSettings);
|
return new RoadRiskRequestCustomizer(requestSettings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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<TrackPoint> payload;
|
||||||
|
|
||||||
|
public RoadRiskRequestPayload(List<TrackPoint> payload) {
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TrackPoint> getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.weather;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@ -64,6 +64,6 @@ public class CurrentWeatherAsyncRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,11 +22,11 @@
|
|||||||
|
|
||||||
package com.github.prominence.openweathermap.api.request.weather;
|
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.enums.ResponseType;
|
||||||
import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper;
|
import com.github.prominence.openweathermap.api.mapper.CurrentWeatherResponseMapper;
|
||||||
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
import com.github.prominence.openweathermap.api.model.weather.Weather;
|
||||||
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
import com.github.prominence.openweathermap.api.request.RequestSettings;
|
||||||
import com.github.prominence.openweathermap.api.utils.RequestUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type Single result current weather request terminator.
|
* The type Single result current weather request terminator.
|
||||||
@ -62,6 +62,6 @@ public class CurrentWeatherRequestTerminator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getRawResponse() {
|
private String getRawResponse() {
|
||||||
return RequestUtils.getResponse(requestSettings);
|
return new RequestExecutor(requestSettings).getResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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<RoadRiskRequestPayload> {
|
||||||
|
@Override
|
||||||
|
public void serialize(RoadRiskRequestPayload roadRiskRequestPayload, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||||
|
jsonGenerator.writeStartObject();
|
||||||
|
jsonGenerator.writeArrayFieldStart("track");
|
||||||
|
|
||||||
|
final List<TrackPoint> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 <code>String</code> representation.
|
|
||||||
*
|
|
||||||
* @param url the url to make API request.
|
|
||||||
* @return response from the request in <code>String</code> representation.
|
|
||||||
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static String getResponse(String url) {
|
|
||||||
return getResponse(url, new TimeoutSettings());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes call to provided API url and retrieves response in <code>String</code> representation.
|
|
||||||
*
|
|
||||||
* @param url the url to make API request.
|
|
||||||
* @param timeoutSettings an object with timeout settings.
|
|
||||||
* @return response from the request in <code>String</code> representation.
|
|
||||||
* @throws IllegalArgumentException in case if provided parameter isn't a valid url for {@link URL} instance.
|
|
||||||
*/
|
|
||||||
public static String getResponse(String url, TimeoutSettings timeoutSettings) {
|
|
||||||
URL requestUrl;
|
|
||||||
try {
|
|
||||||
requestUrl = new URL(url);
|
|
||||||
} 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 <code>InputStream</code> instance.
|
|
||||||
*
|
|
||||||
* @param requestUrl url for API call execution.
|
|
||||||
* @return <code>InputStream</code> 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 <code>String</code> representation.
|
|
||||||
*
|
|
||||||
* @param inputStream input stream to convert.
|
|
||||||
* @return converted <code>InputStream</code> 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test;
|
|||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.util.List;
|
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 {
|
class FiveDayThreeHourStepForecastResponseMapperTest {
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
class RoadRiskResponseMapperTest {
|
class RoadRiskResponseMapperTest {
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,8 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import java.util.List;
|
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 {
|
class SolarRadiationResponseMapperTest {
|
||||||
|
|
||||||
|
|||||||
@ -56,7 +56,7 @@ public class FiveDayThreeHourStepForecastResponseMapperUnitTest {
|
|||||||
assertNotNull(forecast);
|
assertNotNull(forecast);
|
||||||
assertNotNull(forecast.getLocation());
|
assertNotNull(forecast.getLocation());
|
||||||
assertNotNull(forecast.getWeatherForecasts());
|
assertNotNull(forecast.getWeatherForecasts());
|
||||||
forecast.getWeatherForecasts().forEach(weatherForecast -> assertNull(weatherForecast.getWeatherStates()));
|
forecast.getWeatherForecasts().forEach(weatherForecast -> assertEquals(0, weatherForecast.getWeatherStates().size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -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<RoadRiskRecord> roadRiskRecords = getClient()
|
||||||
|
.roadRisk()
|
||||||
|
.byTrackPoints(List.of(trackPoint))
|
||||||
|
.retrieve()
|
||||||
|
.asJava();
|
||||||
|
|
||||||
|
System.out.println(roadRiskRecords);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user