Initial commit

This commit is contained in:
2016-12-24 16:52:29 +01:00
commit f12e359e2a
4 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,95 @@
package de.stklcode.pubtrans.ura;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.stklcode.pubtrans.ura.model.Stop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Client for URA based public transport API.
*
* @author Stefan Kalscheuer <stefan@stklcode.de>
*/
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 FILTER_LINE = "LineID";
private static final String FILTER_STOP = "StopID";
private static final String REQUEST_STOP_ID = "StopID";
private static final String REQUEST_STOP_NAME = "StopPointName";
private static final String REQUEST_STOP_STATE = "StopPointState";
private static final String REQUEST_STOP_INDICATOR = "StopPointIndicator";
private static final String REQUEST_GEOLOCATION = "Latitude,Longitude";
private final String baseURL;
private final String instantURL;
private final String streamURL;
private final ObjectMapper mapper;
/**
* Constructor with base URL and default API paths.
*
* @param baseURL the base URL (with protocol, without trailing slash)
*/
public UraClient(String baseURL) {
this(baseURL, DEFAULT_INSTANT_URL, DEFAULT_STREAM_URL);
}
/**
* Constructor with base URL and custom API paths
*
* @param baseURL the base URL (including protocol)
* @param instantURL the path for instant requests
* @param streamURL the path for stream requests
*/
public UraClient(String baseURL, String instantURL, String streamURL) {
this.baseURL = baseURL;
this.instantURL = instantURL;
this.streamURL = streamURL;
this.mapper = new ObjectMapper();
}
/**
* List available stops.
*
* @return the list
*/
public List<Stop> listStops() {
List<Stop> stops = new ArrayList<>();
try (InputStream is = requestInstant(REQUEST_STOP_NAME, REQUEST_STOP_ID, REQUEST_GEOLOCATION);
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
boolean first = false;
while ((line = br.readLine()) != null) {
if (!first) {
first = true;
continue;
}
stops.add(new Stop(mapper.readValue(line, List.class)));
}
} catch (IOException e) {
e.printStackTrace();
}
return stops;
}
/**
* Issue request to instant endpoint and return input stream.
*
* @param returnList fields to fetch
* @return Input stream of the URL
* @throws IOException on errors
*/
private InputStream requestInstant(String...returnList) throws IOException {
URL url = new URL(baseURL + instantURL + "?ReturnList=" + String.join(",", returnList));
return url.openStream();
}
}

View File

@ -0,0 +1,64 @@
package de.stklcode.pubtrans.ura.model;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import java.util.List;
import java.util.StringJoiner;
/**
* Created by stefan on 24.12.16.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Stop {
private final String id;
private final String name;
private final Double latitude;
private final Double longitude;
public Stop(String id, String name, Double latitude, Double longitude) {
this.id = id;
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
}
public Stop(List raw) {
if (raw.get(1) instanceof String)
name = (String)raw.get(1);
else
throw new UnsupportedOperationException("Field 1 not of expected Type String");
if (raw.get(2) instanceof String)
id = (String)raw.get(2);
else
throw new UnsupportedOperationException("Field 2 not of expected Type String");
if (raw.get(3) instanceof Double)
latitude = (Double)raw.get(3);
else
throw new UnsupportedOperationException("Field 3 not of expected Type Double");
if (raw.get(4) instanceof Double)
longitude = (Double)raw.get(4);
else
throw new UnsupportedOperationException("Field 4 not of expected Type Double");
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Double getLatitude() {
return latitude;
}
public Double getLongitude() {
return longitude;
}
}

View File

@ -0,0 +1,15 @@
package de.stklcode.pubtrans.ura;
import org.junit.Test;
/**
* Created by stefan on 24.12.16.
*/
public class UraClientTest {
private static final String BASE_ASEAG = "http://ivu.aseag.de";
@Test
public void listStopsTest() {
new UraClient(BASE_ASEAG).listStops();
}
}