From 70945f82057aee2cdaad24e139623327f5a201c1 Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Wed, 26 Jun 2024 21:14:12 +0200 Subject: [PATCH] #52 Stop fulltext search API --- .../org/opentripplanner/client/OtpApiClient.java | 14 ++++++++++++++ .../org/opentripplanner/client/model/Stop.java | 1 + .../client/query/GraphQLQueries.java | 4 ++++ src/main/resources/queries/stop.graphql | 3 ++- src/main/resources/queries/stops.graphql | 12 ++++++++++++ .../java/org/opentripplanner/IntegrationTest.java | 14 ++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/queries/stops.graphql diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index 0ae664b..574353b 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -143,6 +143,20 @@ public Stop stop(String gtfsId) throws IOException { return deserialize(jsonNode, "/data/stop", Stop.class); } + public List stops(String nameMask) { + var stopQuery = GraphQLQueries.stops(); + var formattedQuery = stopQuery.formatted(nameMask); + + try { + final var jsonNode = sendRequest(formattedQuery); + var type = listType(Stop.class); + return deserializeList(jsonNode, type, "/data/stops"); + } catch (IOException e) { + LOG.error("Could not fetch stops with name mask '{}'", nameMask, e); + return List.of(); + } + } + private T deserialize(JsonNode jsonNode, String path, Class clazz) throws IOException { try { var plan = jsonNode.at(path); diff --git a/src/main/java/org/opentripplanner/client/model/Stop.java b/src/main/java/org/opentripplanner/client/model/Stop.java index 59ad5af..7946d7b 100644 --- a/src/main/java/org/opentripplanner/client/model/Stop.java +++ b/src/main/java/org/opentripplanner/client/model/Stop.java @@ -8,6 +8,7 @@ public record Stop( String name, @JsonProperty("gtfsId") String id, Optional code, + Optional vehicleMode, Optional zoneId, ParentStation parentStation) { public Stop { diff --git a/src/main/java/org/opentripplanner/client/query/GraphQLQueries.java b/src/main/java/org/opentripplanner/client/query/GraphQLQueries.java index 87d3b42..d1a4e03 100644 --- a/src/main/java/org/opentripplanner/client/query/GraphQLQueries.java +++ b/src/main/java/org/opentripplanner/client/query/GraphQLQueries.java @@ -29,6 +29,10 @@ public static String stop() { return loadQuery("stop"); } + public static String stops() { + return loadQuery("stops"); + } + private static String loadQuery(String name) { var is = GraphQLQueries.class diff --git a/src/main/resources/queries/stop.graphql b/src/main/resources/queries/stop.graphql index b58aa32..c8eae85 100644 --- a/src/main/resources/queries/stop.graphql +++ b/src/main/resources/queries/stop.graphql @@ -3,9 +3,10 @@ query { name gtfsId code + vehicleMode zoneId parentStation { - gtfsId + gtfsId name code } } } \ No newline at end of file diff --git a/src/main/resources/queries/stops.graphql b/src/main/resources/queries/stops.graphql new file mode 100644 index 0000000..69f23ae --- /dev/null +++ b/src/main/resources/queries/stops.graphql @@ -0,0 +1,12 @@ +query { + stops(name: "%s") { + name + gtfsId + code + vehicleMode + zoneId + parentStation { + gtfsId name code + } + } +} diff --git a/src/test/java/org/opentripplanner/IntegrationTest.java b/src/test/java/org/opentripplanner/IntegrationTest.java index 9ccde6f..c28a4c8 100644 --- a/src/test/java/org/opentripplanner/IntegrationTest.java +++ b/src/test/java/org/opentripplanner/IntegrationTest.java @@ -325,6 +325,20 @@ public void stop() throws IOException { assertNotNull(result.id()); } + @Test + public void stops() throws IOException { + var result = client.stops("Oslo"); + + LOG.info("Received stops"); + + assertNotNull(result); + assertFalse(result.isEmpty()); + + var stop = result.get(0); + + assertNotNull(stop.id()); + } + @Disabled @Test public void seattleFares() throws IOException {