Skip to content

Commit

Permalink
opentripplanner#52 Stop fulltext search API
Browse files Browse the repository at this point in the history
  • Loading branch information
jspetrak committed Jun 26, 2024
1 parent 7caabbc commit 70945f8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/org/opentripplanner/client/OtpApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ public Stop stop(String gtfsId) throws IOException {
return deserialize(jsonNode, "/data/stop", Stop.class);
}

public List<Stop> 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> T deserialize(JsonNode jsonNode, String path, Class<T> clazz) throws IOException {
try {
var plan = jsonNode.at(path);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/opentripplanner/client/model/Stop.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record Stop(
String name,
@JsonProperty("gtfsId") String id,
Optional<String> code,
Optional<String> vehicleMode,
Optional<String> zoneId,
ParentStation parentStation) {
public Stop {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/queries/stop.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ query {
name
gtfsId
code
vehicleMode
zoneId
parentStation {
gtfsId
gtfsId name code
}
}
}
12 changes: 12 additions & 0 deletions src/main/resources/queries/stops.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query {
stops(name: "%s") {
name
gtfsId
code
vehicleMode
zoneId
parentStation {
gtfsId name code
}
}
}
14 changes: 14 additions & 0 deletions src/test/java/org/opentripplanner/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 70945f8

Please sign in to comment.