implement connection and read timeouts for synchronous requests
This commit is contained in:
@ -475,10 +475,17 @@ public class UraClient implements Serializable {
|
||||
*/
|
||||
private InputStream request(String url) throws IOException {
|
||||
try {
|
||||
return HttpClient.newHttpClient().send(
|
||||
HttpRequest.newBuilder(URI.create(url)).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofInputStream()
|
||||
).body();
|
||||
var clientBuilder = HttpClient.newBuilder();
|
||||
if (config.getConnectTimeout() != null) {
|
||||
clientBuilder.connectTimeout(config.getConnectTimeout());
|
||||
}
|
||||
|
||||
var reqBuilder = HttpRequest.newBuilder(URI.create(url)).GET();
|
||||
if (config.getTimeout() != null) {
|
||||
reqBuilder.timeout(config.getTimeout());
|
||||
}
|
||||
|
||||
return clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream()).body();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IOException("API request interrupted", e);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.stklcode.pubtrans.ura;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* Configurstion Object for the {@link UraClient}.
|
||||
@ -17,6 +18,8 @@ public class UraClientConfiguration implements Serializable {
|
||||
private final String baseURL;
|
||||
private final String instantPath;
|
||||
private final String streamPath;
|
||||
private final Duration connectTimeout;
|
||||
private final Duration timeout;
|
||||
|
||||
/**
|
||||
* Get new configuration {@link Builder} for given base URL.
|
||||
@ -38,6 +41,8 @@ public class UraClientConfiguration implements Serializable {
|
||||
this.baseURL = builder.baseURL;
|
||||
this.instantPath = builder.instantPath;
|
||||
this.streamPath = builder.streamPath;
|
||||
this.connectTimeout = builder.connectTimeout;
|
||||
this.timeout = builder.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,6 +72,24 @@ public class UraClientConfiguration implements Serializable {
|
||||
return this.streamPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the connection timeout, if any.
|
||||
*
|
||||
* @return Timeout duration or {@code null} if none specified.
|
||||
*/
|
||||
public Duration getConnectTimeout() {
|
||||
return this.connectTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the response timeout, if any.
|
||||
*
|
||||
* @return Timeout duration or {@code null} if none specified.
|
||||
*/
|
||||
public Duration getTimeout() {
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link UraClientConfiguration} objects.
|
||||
*/
|
||||
@ -74,6 +97,8 @@ public class UraClientConfiguration implements Serializable {
|
||||
private final String baseURL;
|
||||
private String instantPath;
|
||||
private String streamPath;
|
||||
private Duration connectTimeout;
|
||||
private Duration timeout;
|
||||
|
||||
/**
|
||||
* Initialize the builder with mandatory base URL.
|
||||
@ -85,6 +110,8 @@ public class UraClientConfiguration implements Serializable {
|
||||
this.baseURL = baseURL;
|
||||
this.instantPath = DEFAULT_INSTANT_PATH;
|
||||
this.streamPath = DEFAULT_STREAM_PATH;
|
||||
this.connectTimeout = null;
|
||||
this.timeout = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,6 +136,28 @@ public class UraClientConfiguration implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a custom connection timeout duration.
|
||||
*
|
||||
* @param connectTimeout Timeout duration.
|
||||
* @return The builder.
|
||||
*/
|
||||
public Builder withConnectTimeout(Duration connectTimeout) {
|
||||
this.connectTimeout = connectTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a custom timeout duration.
|
||||
*
|
||||
* @param timeout Timeout duration.
|
||||
* @return The builder.
|
||||
*/
|
||||
public Builder withTimeout(Duration timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finally build the configuration object.
|
||||
*
|
||||
|
Reference in New Issue
Block a user