Skip to content

Commit f783aae

Browse files
Merge pull request #98 from opentripplanner/alerts
Add query for fetching alerts
2 parents 6b3749e + af435f8 commit f783aae

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

src/main/java/org/opentripplanner/client/OtpApiClient.java

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.hc.core5.http.ContentType;
1818
import org.apache.hc.core5.http.io.entity.StringEntity;
1919
import org.opentripplanner.client.model.Agency;
20+
import org.opentripplanner.client.model.Alert;
2021
import org.opentripplanner.client.model.Pattern;
2122
import org.opentripplanner.client.model.Route;
2223
import org.opentripplanner.client.model.Stop;
@@ -152,6 +153,13 @@ public List<Stop> stopSearch(String nameMask) throws IOException {
152153
return deserializeList(jsonNode, listType(Stop.class), "/data/stops");
153154
}
154155

156+
/** Get all alerts. */
157+
public List<Alert> alerts() throws IOException {
158+
var query = GraphQLQueries.alerts();
159+
final var jsonNode = sendRequest(query);
160+
return deserializeList(jsonNode, listType(Alert.class), "/data/alerts");
161+
}
162+
155163
private <T> T deserialize(JsonNode jsonNode, String path, Class<T> clazz) throws IOException {
156164
try {
157165
var plan = jsonNode.at(path);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.opentripplanner.client.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import java.util.Objects;
5+
6+
public record Alert(
7+
@JsonProperty("alertHeaderText") String header,
8+
@JsonProperty("alertDescriptionText") String description) {
9+
public Alert {
10+
Objects.requireNonNull(header);
11+
Objects.requireNonNull(description);
12+
}
13+
}

src/main/java/org/opentripplanner/client/query/GraphQLQueries.java

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public static String stops() {
3333
return loadQuery("stops");
3434
}
3535

36+
public static String alerts() {
37+
return loadQuery("alerts");
38+
}
39+
3640
private static String loadQuery(String name) {
3741
var is =
3842
GraphQLQueries.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
alerts {
3+
alertHeaderText
4+
alertDescriptionText
5+
alertSeverityLevel
6+
alertCause
7+
alertSeverityLevel
8+
}
9+
}

src/test/java/org/opentripplanner/IntegrationTest.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ public void plan() throws IOException {
6464

6565
var leg = result.itineraries().get(0).legs().get(0);
6666

67-
var transitLeg = result.transitItineraries().get(0).transitLegs().get(0);
67+
var transitLeg =
68+
result.transitItineraries().stream()
69+
.filter(i -> i.legs().stream().anyMatch(l -> !l.intermediatePlaces().isEmpty()))
70+
.findFirst()
71+
.get()
72+
.transitLegs()
73+
.get(0);
6874
assertFalse(transitLeg.from().stop().isEmpty());
6975
assertNotNull(transitLeg.from().coordinate());
7076
assertNotNull(transitLeg.from().point());
@@ -365,6 +371,16 @@ public void stops() throws IOException {
365371
assertNotNull(stop.id());
366372
}
367373

374+
@Test
375+
public void alerts() throws IOException {
376+
var result = client.alerts();
377+
378+
LOG.info("Received {} alerts", result.size());
379+
380+
assertNotNull(result);
381+
assertFalse(result.isEmpty());
382+
}
383+
368384
@Disabled
369385
@Test
370386
public void seattleFares() throws IOException {

0 commit comments

Comments
 (0)