feat(api_handler): return proper objects using transport_helper

many "changes" due to formatter :/
This commit is contained in:
fbachus
2026-07-21 03:11:56 +02:00
parent 25d5e241eb
commit 3850cea0e8
+99 -63
View File
@@ -20,50 +20,66 @@ class ApiHandler {
// - [ ] journeyDetails // - [ ] journeyDetails
//static Future<Map<String, dynamic>> findStations(String locationName) async { //static Future<Map<String, dynamic>> findStations(String locationName) async {
static Future<List<String>> findStations(String locationName, {int? maxStations=5}) async { static Future<List<Station>> findStations(
String locationName, {
int? maxStations = 5,
}) async {
String urlPath = '/api/fahrinfo/latest/location.name'; String urlPath = '/api/fahrinfo/latest/location.name';
Uri url = Uri.https(baseUrl, urlPath, { Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID, 'accessId': accessID,
'input': locationName, 'input': locationName,
'maxNo': maxStations.toString(), 'maxNo': maxStations.toString(),
'type': 'S' // Station/Stops only 'type': 'S', // Station/Stops only
}); });
//print(url); //print(url);
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
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 idList = httpPackageJson["stopLocationOrCoordLocation"].map( (x) => x["StopLocation"]["id"]); List<Station> stationList = httpPackageJson["stopLocationOrCoordLocation"]
//print("and now just ids: "); .map(
//print(id); (x) => Station.fromJson(x["StopLocation"]),
return idList; )
.toList()
.cast<Station>();
return stationList;
} }
static Future<List<String>> nearbyStations(String latitude, String longitude, static Future<List<Station>> nearbyStations(
{int? maxStations=5}) async { String latitude,
String longitude, {
int? maxStations = 5,
}) async {
String urlPath = '/api/fahrinfo/latest/location.nearbystops'; String urlPath = '/api/fahrinfo/latest/location.nearbystops';
Uri url = Uri.https(baseUrl, urlPath, { Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID, 'accessId': accessID,
'originCoordLat': latitude, 'originCoordLat': latitude,
'originCoordLong': longitude, 'originCoordLong': longitude,
'maxNo': maxStations.toString(), 'maxNo': maxStations.toString(),
'type': 'S' // Station/Stops only 'type': 'S', // Station/Stops only
}); });
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson =
final idList = httpPackageJson["stopLocationOrCoordLocation"].map( (x) => x["StopLocation"]["id"]); json.decode(jsonResponse.body) as Map<String, dynamic>;
return idList; final stationList = httpPackageJson["stopLocationOrCoordLocation"].map(
(x) => Station.fromJson(x["StopLocation"]),
);
return stationList;
} }
static Future<List<String>> departures(String station, static Future<List<Departure>> departures(
{int? maxDepartures=-1, int? duration=-1}) async { String stationId, {
int? maxDepartures = -1,
int? duration = 0,
}) async {
String urlPath = '/api/fahrinfo/latest/departureBoard'; String urlPath = '/api/fahrinfo/latest/departureBoard';
String stationString = station.replaceAll(' ', '%20'); // reform for URL String stationString = stationId.replaceAll(' ', '%20'); // reform for URL
Uri url = Uri.https(baseUrl, urlPath, { Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID, 'accessId': accessID,
@@ -71,19 +87,39 @@ class ApiHandler {
////'time': /current time //time from which departures will be shown ////'time': /current time //time from which departures will be shown
'duration': duration.toString(), //interval time in minutes 'duration': duration.toString(), //interval time in minutes
'maxJourneys': maxDepartures.toString(), 'maxJourneys': maxDepartures.toString(),
'type': 'S' // Station/Stops only //'type': 'S', // Station/Stops only
}); });
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson =
final idList = httpPackageJson["Departure"].map( (x) => x["ProductAtStop"]["num"]); json.decode(jsonResponse.body) as Map<String, dynamic>;
return idList;
// try {
List<Departure> departureList = httpPackageJson["Departure"]
.map(
(x) => Departure.fromJson(x),
)
.toList()
.cast<Departure>();
return departureList;
// } catch (e) {
// // TODO: logging
// // empty response
// print('Error: $e');
// // print(
// // 'Response for Station-id "$stationString" contains no field "Departure": $httpPackageJson',
// // );
// return [];
// }
} }
static Future<List<String>> departuresAggregate(List<String> stationList, static Future<List<Departure>> departuresAggregate(
{int? maxDepartures=-1, int? duration=-1}) async { List<String> stationList, {
int? maxDepartures = -1,
int? duration = -1,
}) async {
String urlPath = '/api/fahrinfo/latest/multiDepartureBoard'; String urlPath = '/api/fahrinfo/latest/multiDepartureBoard';
String stationListString = stationList String stationListString = stationList
.reduce((value, element) => value + '|' + element) .reduce((value, element) => value + '|' + element)
@@ -95,21 +131,23 @@ class ApiHandler {
////'time': /current time //time from which departures will be shown ////'time': /current time //time from which departures will be shown
'duration': duration.toString(), //interval time in minutes 'duration': duration.toString(), //interval time in minutes
'maxJourneys': maxDepartures.toString(), 'maxJourneys': maxDepartures.toString(),
'type': 'S' // Station/Stops only 'type': 'S', // Station/Stops only
}); });
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson =
final idList = httpPackageJson["Departure"].map( (x) => x["ProductAtStop"]["num"]); json.decode(jsonResponse.body) as Map<String, dynamic>;
return idList; List<Departure> departureList = httpPackageJson["Departure"].map(
(x) => Departure.fromJson(x),
);
return departureList;
} }
static Future<List<String>> trip( static Future<List<String>> trip(
String originId, String originId,
String destinationId, String destinationId, {
{
String? via, String? via,
String? viaID, String? viaID,
String? date, String? date,
@@ -133,31 +171,28 @@ class ApiHandler {
////'time': /current time //time from which departures will be shown ////'time': /current time //time from which departures will be shown
'time': time.toString(), //interval time in minutes 'time': time.toString(), //interval time in minutes
'maxChange': maxChange.toString(), 'maxChange': maxChange.toString(),
'type': 'S' // Station/Stops only 'type': 'S', // Station/Stops only
}); });
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson =
json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Trip"].map( final idList = httpPackageJson["Trip"].map(
(x) => x["LegList"]["Leg"].map( (x) => x["LegList"]["Leg"].map((y) => y["JourneyDetail"]["ref"]),
(y) => y["JourneyDetail"]["ref"])
); );
return idList; return idList;
} }
static Future<List<String>> tripDetail( static Future<List<String>> tripDetail(
String id, String id, {
{
String? date, String? date,
String? fromId, String? fromId,
int? fromIdx, int? fromIdx,
String? toId, String? toId,
int? toIdx int? toIdx,
}) async { }) async {
String urlPath = '/api/fahrinfo/latest/journeyDetail'; String urlPath = '/api/fahrinfo/latest/journeyDetail';
Uri url = Uri.https(baseUrl, urlPath, { Uri url = Uri.https(baseUrl, urlPath, {
'accessId': accessID, 'accessId': accessID,
@@ -165,12 +200,13 @@ class ApiHandler {
'fromId': fromId, 'fromId': fromId,
'toId': toId, 'toId': toId,
}); });
var jsonResponse = await http.get(url, var jsonResponse = await http.get(
headers: { url,
'Accept': 'application/json' headers: {'Accept': 'application/json'},
}); );
final httpPackageJson = json.decode(jsonResponse.body) as Map<String, dynamic>; final httpPackageJson =
final idList = httpPackageJson["Stops"]["Stop"].map( (x) => x["id"]); json.decode(jsonResponse.body) as Map<String, dynamic>;
final idList = httpPackageJson["Stops"]["Stop"].map((x) => x["id"]);
return idList; return idList;
} }
} }