From e3e825952372bc52a7fe0b505f80e3c52da500ce Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 28 May 2024 17:26:38 +0200 Subject: [PATCH] initial commit --- .classpath | 40 +++++++++++ .gitignore | 1 + .project | 23 ++++++ .settings/org.eclipse.jdt.core.prefs | 8 +++ pom.xml | 30 ++++++++ .../eu/m724/wtapi/object/Coordinates.java | 14 ++++ .../java/eu/m724/wtapi/object/Weather.java | 14 ++++ .../eu/m724/wtapi/object/WeatherState.java | 5 ++ .../provider/api/MockWeatherAPIWrapper.java | 70 +++++++++++++++++++ .../wtapi/provider/api/WeatherAPIWrapper.java | 54 ++++++++++++++ .../exception/AuthorizationException.java | 15 ++++ .../provider/exception/ProviderException.java | 13 ++++ .../exception/QuotaExceededException.java | 30 ++++++++ .../exception/ServerProviderException.java | 16 +++++ 14 files changed, 333 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/eu/m724/wtapi/object/Coordinates.java create mode 100644 src/main/java/eu/m724/wtapi/object/Weather.java create mode 100644 src/main/java/eu/m724/wtapi/object/WeatherState.java create mode 100644 src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java create mode 100644 src/main/java/eu/m724/wtapi/provider/api/WeatherAPIWrapper.java create mode 100644 src/main/java/eu/m724/wtapi/provider/exception/AuthorizationException.java create mode 100644 src/main/java/eu/m724/wtapi/provider/exception/ProviderException.java create mode 100644 src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java create mode 100644 src/main/java/eu/m724/wtapi/provider/exception/ServerProviderException.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..7a9a105 --- /dev/null +++ b/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..5f80782 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + wtapi + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..cf2cd45 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d35d3e3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + eu.m724 + wtapi + 1.0-SNAPSHOT + + + 17 + 17 + + + + + com.google.code.gson + gson + 2.11.0 + + + junit + junit + 4.13.2 + test + + + + \ No newline at end of file diff --git a/src/main/java/eu/m724/wtapi/object/Coordinates.java b/src/main/java/eu/m724/wtapi/object/Coordinates.java new file mode 100644 index 0000000..005fedb --- /dev/null +++ b/src/main/java/eu/m724/wtapi/object/Coordinates.java @@ -0,0 +1,14 @@ +package eu.m724.wtapi.object; + +/** + * represents geographic coordinates + * contains fields latitude and longitude + */ +public class Coordinates { + public double latitude, longitude; + + public Coordinates(double latitude, double longitude) { + this.latitude = latitude; + this.longitude = longitude; + } +} diff --git a/src/main/java/eu/m724/wtapi/object/Weather.java b/src/main/java/eu/m724/wtapi/object/Weather.java new file mode 100644 index 0000000..d0ec63e --- /dev/null +++ b/src/main/java/eu/m724/wtapi/object/Weather.java @@ -0,0 +1,14 @@ +package eu.m724.wtapi.object; + +/** + * contains weather conditions + */ +public class Weather { + public WeatherState weatherState; + public float level = 1; + + public Weather(WeatherState weatherState, float level) { + this.weatherState = weatherState; + this.level = level; + } +} diff --git a/src/main/java/eu/m724/wtapi/object/WeatherState.java b/src/main/java/eu/m724/wtapi/object/WeatherState.java new file mode 100644 index 0000000..066f372 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/object/WeatherState.java @@ -0,0 +1,5 @@ +package eu.m724.wtapi.object; + +public enum WeatherState { + CLEAR, RAIN, THUNDER +} diff --git a/src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java b/src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java new file mode 100644 index 0000000..01148c6 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/api/MockWeatherAPIWrapper.java @@ -0,0 +1,70 @@ +package eu.m724.wtapi.provider.api; + +import java.time.Duration; +import java.time.temporal.TemporalUnit; +import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; + +import eu.m724.wtapi.object.Coordinates; +import eu.m724.wtapi.object.Weather; +import eu.m724.wtapi.object.WeatherState; +import eu.m724.wtapi.provider.exception.ProviderException; + +public class MockWeatherAPIWrapper extends WeatherAPIWrapper { + + @Override + public void init() throws ProviderException { + System.out.println("hello from mock provider"); + + } + + @Override + public CompletableFuture getWeather(Coordinates coordinates) { + if (coordinates == null) + throw new NullPointerException("no coordinates passed"); + + System.out.printf("mock getting weather for %f, %f\n", + coordinates.latitude, coordinates.longitude); + + CompletableFuture completableFuture = + new CompletableFuture<>(); + + completableFuture.complete(new Weather(WeatherState.CLEAR, 0)); + + return completableFuture; + } + + @Override + public CompletableFuture getWeatherBulk(Coordinates[] coordinateses) { + if (coordinateses.length == 0) + throw new IllegalArgumentException("no coordinates passed"); + + System.out.printf("mock getting weather for multiple coords"); + + CompletableFuture completableFuture = + new CompletableFuture<>(); + + int pairs = coordinateses.length / 2; + + Weather[] weathers = new Weather[pairs]; + + for (int i=0; i getWeather(Coordinates coordinates); + + /** + * get weather for multiple points at bulk + * it can be called one at a time if the api doesn't support this or if there's too many + * @param coordinateses array of coordinates + * @throws IllegalArgumentException if array is empty + * @return a future that CAN throw {@link ProviderException} + */ + public abstract CompletableFuture getWeatherBulk(Coordinates[] coordinateses); + + /** + * get hourly quota that is max requests per hour + * @return hourly quota + */ + public abstract int getQuotaHourly(); + + /** + * how many requests actually took place last time you called a method + * @return amount of requests, 0 if no calls happened + */ + public abstract int getLastRequestQuota(); + + /** + * estimates minimum delay between calls given last request + * @return milliseconds + */ + public long estimateDelay() { + return (long) Math.ceil(this.getQuotaHourly() / 2.0 / 60 / 60 / 1000); + } +} diff --git a/src/main/java/eu/m724/wtapi/provider/exception/AuthorizationException.java b/src/main/java/eu/m724/wtapi/provider/exception/AuthorizationException.java new file mode 100644 index 0000000..18d56b2 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/exception/AuthorizationException.java @@ -0,0 +1,15 @@ +package eu.m724.wtapi.provider.exception; + +/** + * when you specified an incorrect api key + */ +public class AuthorizationException extends ProviderException { + + private static final long serialVersionUID = -2258293509429607176L; + + public AuthorizationException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/main/java/eu/m724/wtapi/provider/exception/ProviderException.java b/src/main/java/eu/m724/wtapi/provider/exception/ProviderException.java new file mode 100644 index 0000000..0802ec0 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/exception/ProviderException.java @@ -0,0 +1,13 @@ +package eu.m724.wtapi.provider.exception; + +import java.util.concurrent.ExecutionException; + +public class ProviderException extends ExecutionException { + + private static final long serialVersionUID = -841882181122537157L; + + public ProviderException(String message) { + super(message); + } + +} diff --git a/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java b/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java new file mode 100644 index 0000000..90f6e42 --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/exception/QuotaExceededException.java @@ -0,0 +1,30 @@ +package eu.m724.wtapi.provider.exception; + +/** + * too many api requests, or ratelimited + */ +public class QuotaExceededException extends ProviderException { + + private static final long serialVersionUID = 7550042052176034614L; + + private Long retryMs; + + /** + * + * @param message + * @param retryMs SUGGESTION after how many ms to retry. it can be null + */ + public QuotaExceededException(String message, Long retryMs) { + super(message); + this.retryMs = retryMs; + } + + /** + * + * @return SUGGESTION after how many ms to retry. it can be null + */ + public Long getRetryIn() { + return this.retryMs; + } + +} diff --git a/src/main/java/eu/m724/wtapi/provider/exception/ServerProviderException.java b/src/main/java/eu/m724/wtapi/provider/exception/ServerProviderException.java new file mode 100644 index 0000000..4aaa9fe --- /dev/null +++ b/src/main/java/eu/m724/wtapi/provider/exception/ServerProviderException.java @@ -0,0 +1,16 @@ +package eu.m724.wtapi.provider.exception; + +/** + * when the provider is at fault + */ +public class ServerProviderException extends ProviderException { + + private static final long serialVersionUID = 4461164912391786673L; + + public ServerProviderException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + +}