Add onecall API 3 support to allow access to daily summary (#58)

* bump nexux-staging-maven-plugin version

to resolve the following maven build error:

[INFO] Scanning for projects...
[WARNING] ClassRealm[extension>org.sonatype.plugins:nexus-staging-maven-plugin:1.6.9, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@1d44bcfa]
com.google.inject.CreationException: Unable to create injector, see the following errors:

1) No implementation for com.fasterxml.jackson.databind.ObjectMapper annotated with interface org.eclipse.sisu.inject.TypeArguments$Implicit was bound.
  Did you mean?
    com.fasterxml.jackson.databind.ObjectMapper annotated with @com.google.inject.name.Named(value="org.sonatype.sisu.siesta.jackson.ObjectMapperProvider") bound  at ClassRealm[extension>org.sonatype.plugins:nexus-staging-maven-plugin:1.6.9, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@1d44bcfa] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)

    com.fasterxml.jackson.databind.ObjectMapper bound  at org.eclipse.sisu.wire.LocatorWiring

  at org.eclipse.sisu.wire.LocatorWiring

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist (Errors.java:543)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically (InternalInjectorCreator.java:159)
    at com.google.inject.internal.InternalInjectorCreator.build (InternalInjectorCreator.java:106)
    at com.google.inject.Guice.createInjector (Guice.java:87)
    at com.google.inject.Guice.createInjector (Guice.java:69)
    at com.google.inject.Guice.createInjector (Guice.java:59)
    at org.codehaus.plexus.DefaultPlexusContainer.addPlexusInjector (DefaultPlexusContainer.java:481)
    at org.codehaus.plexus.DefaultPlexusContainer.discoverComponents (DefaultPlexusContainer.java:460)
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.discoverPluginComponents (DefaultMavenPluginManager.java:436)
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.setupExtensionsRealm (DefaultMavenPluginManager.java:879)
    at org.apache.maven.project.DefaultProjectBuildingHelper.createProjectRealm (DefaultProjectBuildingHelper.java:196)
    at org.apache.maven.project.DefaultModelBuildingListener.buildExtensionsAssembled (DefaultModelBuildingListener.java:100)
    at org.apache.maven.model.building.ModelBuildingEventCatapult$1.fire (ModelBuildingEventCatapult.java:44)
    at org.apache.maven.model.building.DefaultModelBuilder.fireEvent (DefaultModelBuilder.java:1359)
    at org.apache.maven.model.building.DefaultModelBuilder.build (DefaultModelBuilder.java:452)
    at org.apache.maven.model.building.DefaultModelBuilder.build (DefaultModelBuilder.java:432)
    at org.apache.maven.project.DefaultProjectBuilder.build (DefaultProjectBuilder.java:583)
    at org.apache.maven.project.DefaultProjectBuilder.build (DefaultProjectBuilder.java:372)
    at org.apache.maven.graph.DefaultGraphBuilder.collectProjects (DefaultGraphBuilder.java:414)
    at org.apache.maven.graph.DefaultGraphBuilder.getProjectsForMavenReactor (DefaultGraphBuilder.java:405)
    at org.apache.maven.graph.DefaultGraphBuilder.build (DefaultGraphBuilder.java:82)
    at org.apache.maven.DefaultMaven.buildGraph (DefaultMaven.java:507)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:219)

* add onecall 3 API

* Changed subscription plan for oneCall3 requester, deprecated oneCall.

* Fixed query parameters encoding.

---------

Co-authored-by: Prominence <alexey.zinchenko@protonmail.com>
This commit is contained in:
dbadia 2024-11-14 03:52:04 -05:00 committed by GitHub
parent 7f60964886
commit 23462b6d77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 215 additions and 8 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.github.prominence</groupId>
<artifactId>openweathermap-api</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
<packaging>jar</packaging>
<name>Java OpenWeatherMap API</name>
@ -80,7 +80,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.9</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>

View File

@ -31,6 +31,7 @@ import com.github.prominence.openweathermap.api.request.onecall.OneCallWeatherRe
import com.github.prominence.openweathermap.api.request.weather.CurrentWeatherRequester;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.ALL;
import static com.github.prominence.openweathermap.api.enums.SubscriptionPlan.SPECIAL;
/**
* The main public API client to communicate with OpenWeatherMap services.
@ -80,10 +81,29 @@ public class OpenWeatherMapClient {
* @return requester for retrieving one call weather information.
*/
@SubscriptionAvailability(plans = ALL)
@Deprecated
public OneCallWeatherRequester oneCall() {
return new OneCallWeatherRequester(new RequestSettings(apiKey, timeoutSettings));
}
/**
* One Call 3 API <a href="https://openweathermap.org/api/one-call-3">API</a>.
* Includes a weather summary statement in addition to the information provided by {@link #oneCall()}
*
* Please note, that One Call API 3.0 is included in the "One Call by Call" subscription only.
* This separate subscription includes 1,000 calls/day for free and allows you to pay only for the number of API calls made to this product.
* Please note, that you do not need to subscribe to any other OpenWeather subscription plans to get access to the One Call API 3.0.
* Please find more details on the pricing page and FAQ or ask Ulla, OpenWeather AI assistant.
*
* @return requester for retrieving one call weather information for the OneCall 3 API.
*/
@SubscriptionAvailability(plans = SPECIAL)
public OneCallWeatherRequester oneCall3() {
RequestSettings requestSettings = new RequestSettings(apiKey, timeoutSettings);
requestSettings.setUseApi3();
return new OneCallWeatherRequester(requestSettings);
}
/**
* Air Pollution <a href="https://openweathermap.org/api/air-pollution">API</a>.
* Air Pollution API provides current, forecast and historical air pollution data for any coordinates on the globe.

View File

@ -56,4 +56,9 @@ public enum SubscriptionPlan {
* All existing subscription plans.
*/
ALL,
/**
* Special subscription cases.
*/
SPECIAL,
}

View File

@ -213,6 +213,7 @@ public class OneCallWeatherResponseMapper {
daily.setMoonPhase(new MoonPhase(moonPhaseNode.asDouble()));
}
daily.setSummary(parseSummary(dailyNode));
daily.setWeatherState(parseWeatherState(dailyNode.get("weather").get(0)));
daily.setTemperature(parseDailyTemperature(dailyNode));
daily.setAtmosphericPressure(parsePressure(dailyNode));
@ -439,4 +440,12 @@ public class OneCallWeatherResponseMapper {
return null;
}
private String parseSummary(JsonNode dailyNode) {
final JsonNode summaryNode = dailyNode.get("summary");
if(summaryNode != null) {
return summaryNode.asText();
}
return null;
}
}

View File

@ -43,6 +43,7 @@ public class Daily {
private LocalDateTime moonsetTime;
private MoonPhase moonPhase;
private String summary;
private WeatherState weatherState;
private DailyTemperature temperature;
private AtmosphericPressure atmosphericPressure;
@ -162,6 +163,24 @@ public class Daily {
this.moonPhase = moonPhase;
}
/**
* Gets summary.
*
* @return the summary
*/
public String getSummary() {
return summary;
}
/**
* Sets summary.
*
* @param summary the summary
*/
public void setSummary(String summary) {
this.summary = summary;
}
/**
* Gets weather state.
*
@ -435,4 +454,5 @@ public class Daily {
}
return stringBuilder.toString();
}
}

View File

@ -45,6 +45,7 @@ public class RequestSettings {
private Language language = Language.ENGLISH;
private UnitSystem unitSystem = UnitSystem.STANDARD;
private boolean useApi3 = false;
public RequestSettings(String apiKey, TimeoutSettings timeoutSettings) {
this.putRequestParameter(API_KEY_PARAM_NAME, apiKey);
@ -94,6 +95,14 @@ public class RequestSettings {
urlAppenderBuilder.append(appendix);
}
public void setUseApi3() {
this.useApi3 = true;
}
public boolean getUseApi3() {
return this.useApi3;
}
public StringBuilder getUrlAppender() {
return urlAppenderBuilder;
}

View File

@ -29,13 +29,11 @@ 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.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
@ -44,7 +42,8 @@ import java.util.stream.Collectors;
*/
public final class RequestUtils {
private static final String OWM_URL_BASE = "http://api.openweathermap.org/data/2.5/";
private static final String OWM_URL_BASE = "https://api.openweathermap.org/data/2.5/";
private static final String OWM_URL_BASE_3_0 = "https://api.openweathermap.org/data/3.0/";
private static final Logger logger = LoggerFactory.getLogger(RequestUtils.class);
@ -53,10 +52,19 @@ public final class RequestUtils {
public static String getResponse(RequestSettings requestSettings) {
StringBuilder requestUrlBuilder = new StringBuilder(OWM_URL_BASE);
if(requestSettings.getUseApi3()) {
requestUrlBuilder = new StringBuilder(OWM_URL_BASE_3_0);
}
requestUrlBuilder.append(requestSettings.getUrlAppender());
requestUrlBuilder.append('?');
String parameters = requestSettings.getRequestParameters().entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.joining("&"));
requestUrlBuilder.append(parameters);

View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2021 Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.prominence.openweathermap.api.request.onecall.current;
import com.github.prominence.openweathermap.api.ApiTest;
import com.github.prominence.openweathermap.api.OpenWeatherMapClient;
import com.github.prominence.openweathermap.api.enums.Language;
import com.github.prominence.openweathermap.api.enums.OneCallResultOptions;
import com.github.prominence.openweathermap.api.enums.UnitSystem;
import com.github.prominence.openweathermap.api.exception.InvalidAuthTokenException;
import com.github.prominence.openweathermap.api.exception.NoDataFoundException;
import com.github.prominence.openweathermap.api.model.Coordinate;
import com.github.prominence.openweathermap.api.model.onecall.current.CurrentWeatherData;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.*;
public class CurrentWeatherOneCallApi3IntegrationTest extends ApiTest {
@Test
public void whenRetrieveCurrentOneCallResponseAsJava_thenOk() {
final CurrentWeatherData currentWeatherData = getClient()
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asJava();
assertNotNull(currentWeatherData);
assertNotNull(currentWeatherData.getDailyList().get(0).getSummary());
}
@Test
public void whenRetrieveCurrentOneCallResponseAsJSON_thenOk() {
final String responseJson = getClient()
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asJSON();
assertNotNull(responseJson);
assertNotEquals("", responseJson);
}
@Test
public void whenRetrieveCurrentOneCallResponseWithExclusionAsJava_thenOk() {
final CurrentWeatherData currentWeatherData = getClient()
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.exclude(OneCallResultOptions.CURRENT, OneCallResultOptions.MINUTELY)
.retrieve()
.asJava();
assertNotNull(currentWeatherData);
assertNull(currentWeatherData.getCurrent());
assertNull(currentWeatherData.getMinutelyList());
assertNotNull(currentWeatherData.getDailyList().get(0).getSummary());
}
@Test
public void whenRetrieveCurrentOneCallAsyncResponseAsJava_thenOk() throws ExecutionException, InterruptedException {
final CompletableFuture<CurrentWeatherData> currentWeatherDataFuture = getClient()
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieveAsync()
.asJava();
assertNotNull(currentWeatherDataFuture);
assertNotNull(currentWeatherDataFuture.get());
assertNotNull(currentWeatherDataFuture.get().getDailyList().get(0).getSummary());
}
@Test
public void whenRetrieveCurrentOneCallAsyncResponseAsJSON_thenOk() throws ExecutionException, InterruptedException {
final CompletableFuture<String> responseJsonFuture = getClient()
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieveAsync()
.asJSON();
assertNotNull(responseJsonFuture);
final String responseJson = responseJsonFuture.get();
assertNotNull(responseJson);
}
@Test
public void whenRequestOnecallWithInvalidApiKey_thenThrowAnException() {
OpenWeatherMapClient client = new OpenWeatherMapClient("invalidKey");
assertThrows(InvalidAuthTokenException.class, () ->
client
.oneCall3()
.current()
.byCoordinate(Coordinate.of(53.54, 27.34))
.language(Language.ENGLISH)
.unitSystem(UnitSystem.METRIC)
.retrieve()
.asJSON()
);
}
}