added optional request timeout to http repositories
This commit is contained in:
1 parent
ddcbdbaec5
commit
882bacf57b
2 files changed
+23
No files matched your search
@@ -10,6 +10,7 @@ import java.net.URISyntaxException;
|
|||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -74,6 +75,19 @@ public abstract class HttpRepository extends Repository {
|
|||||||
return this.execute(request, outputType, null);
|
return this.execute(request, outputType, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like {@link #get(String, Class)}, but the request fails with an exception once the timeout has passed.
|
||||||
|
*/
|
||||||
|
protected <TOutput> ReqResp<TOutput> get(String command, Duration timeout, Class<TOutput> outputType) {
|
||||||
|
HttpRequest request = this.getRequestBuilder(this.getUri(command, parameters -> {
|
||||||
|
}))
|
||||||
|
.timeout(timeout)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return this.execute(request, outputType, null);
|
||||||
|
}
|
||||||
|
|
||||||
private URI getUri(String command, Consumer<URIBuilder> parameters) {
|
private URI getUri(String command, Consumer<URIBuilder> parameters) {
|
||||||
try {
|
try {
|
||||||
URIBuilder builder = new URIBuilder(this.basePath + "/" + command);
|
URIBuilder builder = new URIBuilder(this.basePath + "/" + command);
|
||||||
|
|||||||
+9
@@ -4,6 +4,7 @@ import eu.mhsl.craftattack.spawn.core.api.client.HttpRepository;
|
|||||||
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
import eu.mhsl.craftattack.spawn.core.api.client.ReqResp;
|
||||||
import eu.mhsl.craftattack.spawn.common.api.CraftAttackApi;
|
import eu.mhsl.craftattack.spawn.common.api.CraftAttackApi;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -29,4 +30,12 @@ public class WhitelistRepository extends HttpRepository {
|
|||||||
UserData.class
|
UserData.class
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReqResp<UserData> getUserData(UUID userId, Duration timeout) {
|
||||||
|
return this.get(
|
||||||
|
"users/%s".formatted(userId.toString()),
|
||||||
|
timeout,
|
||||||
|
UserData.class
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user