initial commit

This commit is contained in:
Minecon724 2024-05-28 17:26:38 +02:00
commit e3e8259523
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
14 changed files with 333 additions and 0 deletions

40
.classpath Normal file
View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target/

23
.project Normal file
View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>wtapi</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View file

@ -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

30
pom.xml Normal file
View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.m724</groupId>
<artifactId>wtapi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,5 @@
package eu.m724.wtapi.object;
public enum WeatherState {
CLEAR, RAIN, THUNDER
}

View file

@ -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<Weather> 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<Weather> completableFuture =
new CompletableFuture<>();
completableFuture.complete(new Weather(WeatherState.CLEAR, 0));
return completableFuture;
}
@Override
public CompletableFuture<Weather[]> getWeatherBulk(Coordinates[] coordinateses) {
if (coordinateses.length == 0)
throw new IllegalArgumentException("no coordinates passed");
System.out.printf("mock getting weather for multiple coords");
CompletableFuture<Weather[]> completableFuture =
new CompletableFuture<>();
int pairs = coordinateses.length / 2;
Weather[] weathers = new Weather[pairs];
for (int i=0; i<pairs; i++) {
weathers[i] = new Weather(WeatherState.CLEAR, 1);
}
completableFuture.complete(weathers);
return completableFuture;
}
@Override
public int getQuotaHourly() {
return 69;
}
@Override
public int getLastRequestQuota() {
return 1;
}
}

View file

@ -0,0 +1,54 @@
package eu.m724.wtapi.provider.api;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Weather;
import eu.m724.wtapi.provider.exception.ProviderException;
public abstract class WeatherAPIWrapper {
/**
* initialize the api
* @throws ProviderException if initialization failed
*/
public abstract void init() throws ProviderException;
/**
* get weather for a single point
* @param coordinates
* @throws NullPointerException if coordinates is null
* @return a future that CAN throw {@link ProviderException}
*/
public abstract CompletableFuture<Weather> 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<Weather[]> 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);
}
}

View file

@ -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
}
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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
}
}