Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
* Java 17 or later required (#91)
* Updated JUnit to 6.0.3 (#92)
* Updated Jackson dependency to 3.1.3 (#93)
* Use Java _record_ classes for `Message`, `Stop` and `Trip` models (#94)


## 2.0.11 - 2026-05-14
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/de/stklcode/pubtrans/ura/UraClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 Stefan Kalscheuer
* Copyright 2016-2026 Stefan Kalscheuer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -276,7 +276,7 @@ public List<Trip> getTrips(final Query query, final Integer limit) throws UraCli
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
version = l.get(1).toString();
} else if (l.get(0).equals(RES_TYPE_PREDICTION)) {
trips.add(new Trip(l, version));
trips.add(Trip.of(l, version));
}
}
line = br.readLine();
Expand Down Expand Up @@ -315,7 +315,7 @@ public AsyncUraTripReader getTripsStream(final Query query, final List<Consumer<
// Create the reader.
try {
AsyncUraTripReader reader = new AsyncUraTripReader(
URI.create(requestURL(config.getBaseURL() + config.getStreamPath(), REQUEST_TRIP, query)),
URI.create(requestURL(config.baseURL() + config.streamPath(), REQUEST_TRIP, query)),
config,
consumers
);
Expand Down Expand Up @@ -360,7 +360,7 @@ public List<Stop> getStops(final Query query) throws UraClientException {
List<Serializable> l = mapper.readValue(line, mapper.getTypeFactory().constructCollectionType(List.class, Serializable.class));
/* Check if result exists and has correct response type */
if (l != null && !l.isEmpty() && l.get(0).equals(RES_TYPE_STOP)) {
stops.add(new Stop(l));
stops.add(Stop.of(l));
}
}
} catch (IOException | JacksonException e) {
Expand Down Expand Up @@ -433,7 +433,7 @@ public List<Message> getMessages(final Query query, final Integer limit) throws
if (l.get(0).equals(RES_TYPE_URA_VERSION)) {
version = l.get(1).toString();
} else if (l.get(0).equals(RES_TYPE_FLEX_MESSAGE)) {
messages.add(new Message(l, version));
messages.add(Message.of(l, version));
}
}
line = br.readLine();
Expand All @@ -453,7 +453,7 @@ public List<Message> getMessages(final Query query, final Integer limit) throws
* @throws IOException on errors
*/
private InputStream requestInstant(final String[] returnList, final Query query) throws IOException {
return request(requestURL(config.getBaseURL() + config.getInstantPath(), returnList, query));
return request(requestURL(config.baseURL() + config.instantPath(), returnList, query));
}

/**
Expand Down Expand Up @@ -497,13 +497,13 @@ private String requestURL(final String endpointURL, final String[] returnList, f
private InputStream request(String url) throws IOException {
try {
var clientBuilder = HttpClient.newBuilder();
if (config.getConnectTimeout() != null) {
clientBuilder.connectTimeout(config.getConnectTimeout());
if (config.connectTimeout() != null) {
clientBuilder.connectTimeout(config.connectTimeout());
}

var reqBuilder = HttpRequest.newBuilder(URI.create(url)).GET();
if (config.getTimeout() != null) {
reqBuilder.timeout(config.getTimeout());
if (config.timeout() != null) {
reqBuilder.timeout(config.timeout());
}

return clientBuilder.build().send(reqBuilder.build(), HttpResponse.BodyHandlers.ofInputStream()).body();
Expand Down
101 changes: 15 additions & 86 deletions src/main/java/de/stklcode/pubtrans/ura/UraClientConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 Stefan Kalscheuer
* Copyright 2016-2026 Stefan Kalscheuer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,40 +22,26 @@
/**
* Configuration Object for the {@link UraClient}.
*
* @param baseURL API base URL.
* @param instantPath Path to instant API endpoint.
* @param streamPath Path to stream API endpoint.
* @param connectTimeout Optional connection timeout.
* @param timeout Optional read timeout.
* @author Stefan Kalscheuer
* @since 2.0
* @since 3.0 record
*/
public class UraClientConfiguration implements Serializable {
private static final long serialVersionUID = 1L;
public record UraClientConfiguration(
String baseURL,
String instantPath,
String streamPath,
Duration connectTimeout,
Duration timeout
) implements Serializable {

private static final String DEFAULT_INSTANT_PATH = "/interfaces/ura/instant_V1";
private static final String DEFAULT_STREAM_PATH = "/interfaces/ura/stream_V1";

/**
* API base URL.
*/
private final String baseURL;

/**
* Path to instant API endpoint.
*/
private final String instantPath;

/**
* Path to stream API endpoint.
*/
private final String streamPath;

/**
* Optional connection timeout.
*/
private final Duration connectTimeout;

/**
* Optional read timeout.
*/
private final Duration timeout;

/**
* Get new configuration {@link Builder} for given base URL.
* This URL is the only option required.
Expand All @@ -67,63 +53,6 @@ public static Builder forBaseURL(final String baseURL) {
return new Builder(baseURL);
}

/**
* Construct new configuration object from Builder.
*
* @param builder The builder instance.
*/
private UraClientConfiguration(Builder builder) {
this.baseURL = builder.baseURL;
this.instantPath = builder.instantPath;
this.streamPath = builder.streamPath;
this.connectTimeout = builder.connectTimeout;
this.timeout = builder.timeout;
}

/**
* Get the API base URL.
*
* @return Base URL.
*/
public String getBaseURL() {
return baseURL;
}

/**
* Get the API instant endpoint path.
*
* @return Instant endpoint path.
*/
public String getInstantPath() {
return this.instantPath;
}

/**
* Get the API stream endpoint path.
*
* @return Stream endpoint path.
*/
public String getStreamPath() {
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.
Expand Down Expand Up @@ -199,7 +128,7 @@ public Builder withTimeout(Duration timeout) {
* @return The configuration.
*/
public UraClientConfiguration build() {
return new UraClientConfiguration(this);
return new UraClientConfiguration(baseURL, instantPath, streamPath, connectTimeout, timeout);
}
}
}
Loading