feat(api_requests): add basic api requests that hand back useless ids

This commit is contained in:
fbachus
2026-05-18 23:49:02 +02:00
parent 1730232450
commit 385600dd06
+145 -5
View File
@@ -11,14 +11,21 @@ class ApiHandler {
static final String baseUrl = dotenv.get("VBB_API_URL"); static final String baseUrl = dotenv.get("VBB_API_URL");
static final String accessID = dotenv.get("VBB_API_KEY"); static final String accessID = dotenv.get("VBB_API_KEY");
// TODO: function calls for the following API calls
// - [x] location.name: findStations
// - [ ] location.nearbystops
// - [ ] location.details
// - [ ] departureBoard
// - [ ] multiDepartureBoard
// - [ ] journeyDetails
//static Future<Map<String, dynamic>> findStations(String locationName) async { //static Future<Map<String, dynamic>> findStations(String locationName) async {
static Future<String> findStations(String locationName) async { static Future<List<String>> findStations(String locationName, {int? maxStations=5}) async {
String urlPath = '/api/fahrinfo/latest/location.name'; String urlPath = '/api/fahrinfo/latest/location.name';
String urlParams = '?input=$locationName';
Uri url = Uri.https(baseUrl, urlPath, { Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID, 'accessId': accessID,
'input': locationName, 'input': locationName,
'maxNo': '2', 'maxNo': maxStations.toString(),
'type': 'S' // Station/Stops only 'type': 'S' // Station/Stops only
}); });
//print(url); //print(url);
@@ -28,9 +35,142 @@ class ApiHandler {
}); });
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
//print(httpPackageJson["stopLocationOrCoordLocation"]); //print(httpPackageJson["stopLocationOrCoordLocation"]);
final id = httpPackageJson["stopLocationOrCoordLocation"][0]["StopLocation"]["id"]; final idList = httpPackageJson["stopLocationOrCoordLocation"].map( (x) => x["StopLocation"]["id"]);
//print("and now just ids: "); //print("and now just ids: ");
//print(id); //print(id);
return id; return idList;
}
static Future<List<String>> nearbyStations(String latitude, String longitude,
{int? maxStations=5}) async {
String urlPath = '/api/fahrinfo/latest/location.nearbystops';
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'originCoordLat': latitude,
'originCoordLong': longitude,
'maxNo': maxStations.toString(),
'type': 'S' // Station/Stops only
});
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["stopLocationOrCoordLocation"].map( (x) => x["StopLocation"]["id"]);
return idList;
}
static Future<List<String>> departures(String station,
{int? maxDepartures=-1, int? duration=-1}) async {
String urlPath = '/api/fahrinfo/latest/departureBoard';
String stationString = station.replaceAll(' ', '%20'); // reform for URL
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'id': stationString,
////'time': /current time //time from which departures will be shown
'duration': duration.toString(), //interval time in minutes
'maxJourneys': maxDepartures.toString(),
'type': 'S' // Station/Stops only
});
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Departure"].map( (x) => x["ProductAtStop"]["num"]);
return idList;
}
static Future<List<String>> departuresAggregate(List<String> stationList,
{int? maxDepartures=-1, int? duration=-1}) async {
String urlPath = '/api/fahrinfo/latest/multiDepartureBoard';
String stationListString = stationList
.reduce((value, element) => value + '|' + element)
.replaceAll(' ', '%20'); // reform for URL
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'id': stationListString,
////'time': /current time //time from which departures will be shown
'duration': duration.toString(), //interval time in minutes
'maxJourneys': maxDepartures.toString(),
'type': 'S' // Station/Stops only
});
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Departure"].map( (x) => x["ProductAtStop"]["num"]);
return idList;
}
static Future<List<String>> trip(
String originId,
String destinationId,
{
String? via,
String? viaID,
String? date,
String? time,
int? maxChange,
int? minChangeTime,
int? maxChangeTime,
bool? arrivalTime,
// int? products, // which transport modi to use - ignore for now
// String rtMode, // wherether to use real time data
// String trainFilter,
}) async {
String urlPath = '/api/fahrinfo/latest/trip';
String startId = originId.replaceAll(' ', '%20'); // reform for URL
String endId = destinationId.replaceAll(' ', '%20'); // reform for URL
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'originId': startId,
'destId': endId,
////'time': /current time //time from which departures will be shown
'time': time.toString(), //interval time in minutes
'maxChange': maxChange.toString(),
'type': 'S' // Station/Stops only
});
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Trip"].map(
(x) => x["LegList"]["Leg"].map(
(y) => y["JourneyDetail"]["ref"])
);
return idList;
}
static Future<List<String>> tripDetail(
String id,
{
String? date,
String? fromId,
int? fromIdx,
String? toId,
int? toIdx
}) async {
String urlPath = '/api/fahrinfo/latest/journeyDetail';
Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID,
'id': id,
'fromId': fromId,
'toId': toId,
});
var jsonResponse = await http.get(url,
headers: {
'Accept': 'application/json'
});
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Stops"]["Stop"].map( (x) => x["id"]);
return idList;
} }
} }