Skip to content

Commit 44454cd

Browse files
Merge pull request #20 from HBTGmbH/feature/add-query-for-stop
add query for stop by id
2 parents b0b1496 + e84e823 commit 44454cd

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

Diff for: src/main/java/org/opentripplanner/client/OtpApiClient.java

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.hc.core5.http.io.entity.StringEntity;
1919
import org.opentripplanner.client.model.Pattern;
2020
import org.opentripplanner.client.model.Route;
21+
import org.opentripplanner.client.model.Stop;
2122
import org.opentripplanner.client.model.TripPlan;
2223
import org.opentripplanner.client.model.VehicleRentalStation;
2324
import org.opentripplanner.client.parameters.TripPlanParameters;
@@ -114,6 +115,27 @@ public List<Pattern> patterns() throws IOException {
114115
return deserializeList(json, type, "/data/patterns");
115116
}
116117

118+
/**
119+
* Returns a Stop with limited information.
120+
*
121+
* @link <a href="https://docs.opentripplanner.org/api/dev-2.x/graphql-gtfs/queries/stop">OTP API
122+
* docs</a>
123+
*/
124+
public Stop stop(String gtfsId) throws IOException {
125+
126+
var stopQuery = GraphQLQueries.stop();
127+
var formattedQuery = stopQuery.formatted(gtfsId);
128+
129+
final var jsonNode = sendRequest(formattedQuery);
130+
try {
131+
var stop = jsonNode.at("/data/stop");
132+
return mapper.treeToValue(stop, Stop.class);
133+
} catch (IOException e) {
134+
LOG.error("Could not deserialize response: {}", jsonNode.toPrettyString());
135+
throw e;
136+
}
137+
}
138+
117139
private <T> List<T> deserializeList(JsonNode json, CollectionType type, String path)
118140
throws JsonProcessingException {
119141
return mapper.treeToValue(json.at(path), type);

Diff for: src/main/java/org/opentripplanner/client/query/GraphQLQueries.java

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public static String patterns() {
2121
return loadQuery("patterns");
2222
}
2323

24+
public static String stop() {
25+
return loadQuery("stop");
26+
}
27+
2428
private static String loadQuery(String name) {
2529
var is =
2630
GraphQLQueries.class

Diff for: src/main/resources/queries/stop.graphql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query {
2+
stop(id: "%s") {
3+
name
4+
gtfsId
5+
code
6+
}
7+
}

Diff for: src/test/java/org/opentripplanner/IntegrationTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.opentripplanner.StationParameters.OSLO_EAST;
77
import static org.opentripplanner.StationParameters.OSLO_LUFTHAVN_ID;
8+
import static org.opentripplanner.StationParameters.OSLO_LUFTHAVN_QUAY;
89
import static org.opentripplanner.StationParameters.OSLO_S_ID;
910
import static org.opentripplanner.StationParameters.OSLO_WEST;
1011

@@ -187,6 +188,18 @@ public void patterns() throws IOException {
187188
});
188189
}
189190

191+
@Test
192+
public void stop() throws IOException {
193+
194+
var result = client.stop(OSLO_LUFTHAVN_QUAY);
195+
196+
LOG.info("Received stop");
197+
198+
assertNotNull(result);
199+
assertNotNull(result.name());
200+
assertNotNull(result.id());
201+
}
202+
190203
@Disabled
191204
@Test
192205
public void seattleFares() throws IOException {

Diff for: src/test/java/org/opentripplanner/StationParameters.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface StationParameters {
88
Coordinate OSLO_WEST = new Coordinate(59.9203, 10.6823);
99
StopId OSLO_LUFTHAVN_ID = new StopId("RB:NSR:StopPlace:5357");
1010
StopId OSLO_S_ID = new StopId("RB:NSR:StopPlace:337");
11+
String OSLO_LUFTHAVN_QUAY = "RB:NSR:Quay:9786";
1112
}

0 commit comments

Comments
 (0)