API V1 as default; Tests against ASEAG and TFL
This commit is contained in:
@ -31,11 +31,11 @@ import java.util.List;
|
||||
/**
|
||||
* Client for URA based public transport API.
|
||||
*
|
||||
* @author Stefan Kalscheuer [stefan@stklcode.de]
|
||||
* @author Stefan Kalscheuer
|
||||
*/
|
||||
public class UraClient {
|
||||
private static final String DEFAULT_INSTANT_URL = "/interfaces/ura/instant_V2";
|
||||
private static final String DEFAULT_STREAM_URL = "/interfaces/ura/stream_V2";
|
||||
private static final String DEFAULT_INSTANT_URL = "/interfaces/ura/instant_V1";
|
||||
private static final String DEFAULT_STREAM_URL = "/interfaces/ura/stream_V1";
|
||||
|
||||
private static final String PAR_STOP_ID = "StopID";
|
||||
private static final String PAR_STOP_NAME = "StopPointName";
|
||||
@ -54,7 +54,7 @@ public class UraClient {
|
||||
|
||||
private static final Integer RES_TYPE_STOP = 0;
|
||||
private static final Integer RES_TYPE_PREDICTION = 1;
|
||||
|
||||
private static final Integer RES_TYPE_URA_VERSION = 4;
|
||||
|
||||
private static final String[] REQUEST_STOP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION};
|
||||
private static final String[] REQUEST_TRIP = {PAR_STOP_NAME, PAR_STOP_ID, PAR_STOP_INDICATOR, PAR_STOP_STATE, PAR_GEOLOCATION,
|
||||
@ -181,12 +181,17 @@ public class UraClient {
|
||||
List<Trip> trips = new ArrayList<>();
|
||||
try (InputStream is = requestInstant(REQUEST_TRIP, query.stopIDs, query.stopNames, query.lineIDs, query.lineNames, query.direction);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||
String version = null;
|
||||
String line;
|
||||
while ((line = br.readLine()) != null && (limit == null || trips.size() < limit)) {
|
||||
List l = mapper.readValue(line, List.class);
|
||||
/* Check if result exists and has correct response type */
|
||||
if (l != null && l.size() > 0 && l.get(0).equals(RES_TYPE_PREDICTION))
|
||||
trips.add(new Trip(l));
|
||||
if (l != null && l.size() > 0) {
|
||||
if (l.get(0).equals(RES_TYPE_URA_VERSION))
|
||||
version = l.get(1).toString();
|
||||
else if (l.get(0).equals(RES_TYPE_PREDICTION))
|
||||
trips.add(new Trip(l, version));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -215,6 +220,7 @@ public class UraClient {
|
||||
try (InputStream is = requestInstant(REQUEST_STOP, query.stopIDs, query.stopNames, query.lineIDs, query.lineNames, query.direction);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
||||
String line;
|
||||
String version;
|
||||
while ((line = br.readLine()) != null) {
|
||||
List l = mapper.readValue(line, List.class);
|
||||
/* Check if result exists and has correct response type */
|
||||
|
@ -22,7 +22,7 @@ import java.util.List;
|
||||
/**
|
||||
* Entity for a single stop.
|
||||
*
|
||||
* @author Stefan Kalscheuer [stefan@stklcode.de]
|
||||
* @author Stefan Kalscheuer
|
||||
*/
|
||||
public class Stop {
|
||||
private final String id;
|
||||
@ -55,6 +55,8 @@ public class Stop {
|
||||
throw new IOException("Field 2 not of expected type String, found " + raw.get(2).getClass().getSimpleName());
|
||||
if (raw.get(3) instanceof String)
|
||||
indicator = (String)raw.get(3);
|
||||
else if (raw.get(3) == null)
|
||||
indicator = null;
|
||||
else
|
||||
throw new IOException("Field 3 not of expected type String, found " + raw.get(3).getClass().getSimpleName());
|
||||
if (raw.get(4) instanceof Integer)
|
||||
|
@ -22,7 +22,7 @@ import java.util.List;
|
||||
/**
|
||||
* Entity for a single trip.
|
||||
*
|
||||
* @author Stefan Kalscheuer [stefan@stklcode.de]
|
||||
* @author Stefan Kalscheuer
|
||||
*/
|
||||
public class Trip {
|
||||
private final Stop stop;
|
||||
@ -56,6 +56,10 @@ public class Trip {
|
||||
}
|
||||
|
||||
public Trip(List raw) throws IOException {
|
||||
this(raw, null);
|
||||
}
|
||||
|
||||
public Trip(List raw, String version) throws IOException {
|
||||
if (raw == null || raw.size() < 16)
|
||||
throw new IOException("Invalid number of fields");
|
||||
|
||||
@ -73,8 +77,11 @@ public class Trip {
|
||||
lineName = (String)raw.get(9);
|
||||
else
|
||||
throw new IOException("Field 9 not of expected type String, found " + raw.get(9).getClass().getSimpleName());
|
||||
if (raw.get(10) instanceof Integer)
|
||||
directionID = (Integer)raw.get(10);
|
||||
if (raw.get(10) instanceof Integer) {
|
||||
directionID = (Integer) raw.get(10);
|
||||
if (directionID < 0 || directionID > 2)
|
||||
throw new IOException("Direction out of range. Expected 1 or 2, found " + directionID);
|
||||
}
|
||||
else
|
||||
throw new IOException("Field 10 not of expected type Integer, found " + raw.get(10).getClass().getSimpleName());
|
||||
if (raw.get(11) instanceof String)
|
||||
@ -85,14 +92,15 @@ public class Trip {
|
||||
destinationText = (String)raw.get(12);
|
||||
else
|
||||
throw new IOException("Field 12 not of expected type String, found " + raw.get(12).getClass().getSimpleName());
|
||||
if (raw.get(13) instanceof String)
|
||||
vehicleID = (String)raw.get(13);
|
||||
/* TFL and ASEAG deliver different types with the same API version, so this field is a little more tolerant */
|
||||
if (raw.get(13) instanceof String || raw.get(13) instanceof Integer || raw.get(13) instanceof Long)
|
||||
vehicleID = raw.get(13).toString();
|
||||
else
|
||||
throw new IOException("Field 13 not of expected type String, found " + raw.get(13).getClass().getSimpleName());
|
||||
if (raw.get(14) instanceof String)
|
||||
id = (String)raw.get(14);
|
||||
throw new IOException("Field 13 not of expected type String/Integer/Long, found " + raw.get(13).getClass().getSimpleName());
|
||||
if (raw.get(14) instanceof String || raw.get(14) instanceof Integer || raw.get(14) instanceof Long)
|
||||
id = raw.get(14).toString();
|
||||
else
|
||||
throw new IOException("Field 14 not of expected type String, found " + raw.get(14).getClass().getSimpleName());
|
||||
throw new IOException("Field 14 not of expected type String/Integer/Long, found " + raw.get(14).getClass().getSimpleName());
|
||||
if (raw.get(15) instanceof Long)
|
||||
estimatedTime = (Long)raw.get(15);
|
||||
else
|
||||
|
Reference in New Issue
Block a user