Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Oct 19, 2023
1 parent 4220c72 commit a477801
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This library provides an easy way to access OTP's GTFS GraphQL API with Java.

### Compatible OTP version

You need at least version 2.4.0 of OpenTripPlanner to use this client.

### Installation

[![Maven Central](https://img.shields.io/maven-central/v/org.opentripplanner/otp-client.svg)](https://mvnrepository.com/artifact/org.opentripplanner/otp-client)
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/org/opentripplanner/client/OtpApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public OtpApiClient(ZoneId timezone, String baseUrl) {
this.mapper = ObjectMappers.withTimezone(timezone);
this.graphQlUri = URI.create(baseUrl + DEFAULT_GRAPHQL_PATH);
}
;

public TripPlan plan(TripPlanParameters req) throws IOException, InterruptedException {
public TripPlan plan(TripPlanParameters req) throws IOException {

var planQuery = GraphQLQueries.plan();
var formattedModes =
Expand All @@ -60,11 +59,16 @@ public TripPlan plan(TripPlanParameters req) throws IOException, InterruptedExce
req.walkReluctance());

final var jsonNode = sendRequest(formattedQuery);
var plan = jsonNode.at("/data/plan");
return mapper.treeToValue(plan, TripPlan.class);
try {
var plan = jsonNode.at("/data/plan");
return mapper.treeToValue(plan, TripPlan.class);
} catch (IOException e) {
LOG.error("Could not deserialize response: {}", jsonNode.toPrettyString());
throw e;
}
}

public List<Route> routes() throws IOException, InterruptedException {
public List<Route> routes() throws IOException {
var json = sendRequest(GraphQLQueries.routes());
var type = listType(Route.class);
return mapper.treeToValue(json.at("/data/routes"), type);
Expand All @@ -77,7 +81,7 @@ public List<VehicleRentalStation> vehicleRentalStations()
return mapper.treeToValue(json.at("/data/vehicleRentalStations"), type);
}

public List<Pattern> patterns() throws IOException, InterruptedException {
public List<Pattern> patterns() throws IOException {
var json = sendRequest(GraphQLQueries.patterns());
var type = listType(Pattern.class);
return mapper.treeToValue(json.at("/data/patterns"), type);
Expand All @@ -87,7 +91,7 @@ private static CollectionType listType(Class<?> clazz) {
return TypeFactory.defaultInstance().constructCollectionType(List.class, clazz);
}

private JsonNode sendRequest(String formattedQuery) throws IOException, InterruptedException {
private JsonNode sendRequest(String formattedQuery) throws IOException {
LOG.debug("Sending GraphQL query to {}: {}", graphQlUri, formattedQuery);

var body = mapper.createObjectNode();
Expand All @@ -99,6 +103,10 @@ private JsonNode sendRequest(String formattedQuery) throws IOException, Interrup
var stringEntity = new StringEntity(bodyString, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
var response = httpClient.execute(httpPost);
if (response.getCode() != 200) {
throw new IOException(
"HTTP request to '%s' returned status code %s".formatted(graphQlUri, response.getCode()));
}
var jsonNode = mapper.readTree(response.getEntity().getContent());

LOG.debug("Received the following JSON: {}", jsonNode.toPrettyString());
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/opentripplanner/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class IntegrationTest {
new OtpApiClient(ZoneId.of("Europe/Oslo"), "https://otp2debug.entur.org");

@Test
public void route() throws IOException, InterruptedException {
public void route() throws IOException {

var result =
client.plan(
Expand All @@ -51,7 +51,7 @@ public void route() throws IOException, InterruptedException {
}

@Test
public void arriveBy() throws IOException, InterruptedException {
public void arriveBy() throws IOException {

var result =
client.plan(
Expand All @@ -69,7 +69,7 @@ public void arriveBy() throws IOException, InterruptedException {
}

@Test
public void bikeRoute() throws IOException, InterruptedException {
public void bikeRoute() throws IOException {

var result =
client.plan(
Expand All @@ -96,7 +96,7 @@ public void rentalStations() throws IOException, InterruptedException {
}

@Test
public void routes() throws IOException, InterruptedException {
public void routes() throws IOException {

var result = client.routes();

Expand All @@ -106,7 +106,7 @@ public void routes() throws IOException, InterruptedException {
}

@Test
public void patterns() throws IOException, InterruptedException {
public void patterns() throws IOException {

var result = client.patterns();

Expand All @@ -123,7 +123,7 @@ public void patterns() throws IOException, InterruptedException {

@Disabled
@Test
public void seattleFares() throws IOException, InterruptedException {
public void seattleFares() throws IOException {

var southSeattle = new Coordinate(47.5634, -122.3155);
var northSeattle = new Coordinate(47.6225, -122.3312);
Expand Down

0 comments on commit a477801

Please sign in to comment.