From 1a476dfce87cdd3a710f937e78041b8b35182c41 Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Wed, 26 Jun 2024 21:14:12 +0200 Subject: [PATCH 1/2] #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 { From 4c538ede2bbb39247bcdbed5e5276fab8290724f Mon Sep 17 00:00:00 2001 From: Josef Petrak Date: Mon, 1 Jul 2024 11:24:48 +0300 Subject: [PATCH 2/2] #52 Code review --- .../opentripplanner/client/OtpApiClient.java | 12 ++------ .../opentripplanner/client/model/Stop.java | 2 +- .../client/model/VehicleMode.java | 28 +++++++++++++++++++ src/main/resources/queries/stop.graphql | 4 ++- src/main/resources/queries/stops.graphql | 4 ++- 5 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/opentripplanner/client/model/VehicleMode.java diff --git a/src/main/java/org/opentripplanner/client/OtpApiClient.java b/src/main/java/org/opentripplanner/client/OtpApiClient.java index 574353b..e9963cd 100644 --- a/src/main/java/org/opentripplanner/client/OtpApiClient.java +++ b/src/main/java/org/opentripplanner/client/OtpApiClient.java @@ -143,18 +143,12 @@ public Stop stop(String gtfsId) throws IOException { return deserialize(jsonNode, "/data/stop", Stop.class); } - public List stops(String nameMask) { + public List stops(String nameMask) throws IOException { 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(); - } + final var jsonNode = sendRequest(formattedQuery); + return deserializeList(jsonNode, listType(Stop.class), "/data/stops"); } private T deserialize(JsonNode jsonNode, String path, Class clazz) throws IOException { diff --git a/src/main/java/org/opentripplanner/client/model/Stop.java b/src/main/java/org/opentripplanner/client/model/Stop.java index 7946d7b..5a28166 100644 --- a/src/main/java/org/opentripplanner/client/model/Stop.java +++ b/src/main/java/org/opentripplanner/client/model/Stop.java @@ -8,7 +8,7 @@ public record Stop( String name, @JsonProperty("gtfsId") String id, Optional code, - Optional vehicleMode, + Optional vehicleMode, Optional zoneId, ParentStation parentStation) { public Stop { diff --git a/src/main/java/org/opentripplanner/client/model/VehicleMode.java b/src/main/java/org/opentripplanner/client/model/VehicleMode.java new file mode 100644 index 0000000..b006bbf --- /dev/null +++ b/src/main/java/org/opentripplanner/client/model/VehicleMode.java @@ -0,0 +1,28 @@ +package org.opentripplanner.client.model; + +public enum VehicleMode { + AIRPLANE, + BICYCLE, + BUS, + CABLE_CAR, + CAR, + COACH, + FERRY, + FLEX, + @Deprecated + FLEXIBLE, + FUNICULAR, + GONDOLA, + @Deprecated + LEG_SWITCH, + RAIL, + SCOOTER, + SUBWAY, + TRAM, + CARPOOL, + TAXI, + TRANSIT, + WALK, + TROLLEYBUS, + MONORAIL +} diff --git a/src/main/resources/queries/stop.graphql b/src/main/resources/queries/stop.graphql index c8eae85..d5e5b1c 100644 --- a/src/main/resources/queries/stop.graphql +++ b/src/main/resources/queries/stop.graphql @@ -6,7 +6,9 @@ query { vehicleMode zoneId parentStation { - gtfsId name code + 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 index 69f23ae..bef349c 100644 --- a/src/main/resources/queries/stops.graphql +++ b/src/main/resources/queries/stops.graphql @@ -6,7 +6,9 @@ query { vehicleMode zoneId parentStation { - gtfsId name code + gtfsId + name + code } } }