* Start script to generate BC data * Finished BC data * moved file slightly to account for future addition of OCCT and added /stops endpoint to server --------- Co-authored-by: Pagwin <git@pagwin.xyz>
58 lines
1.4 KiB
Dart
58 lines
1.4 KiB
Dart
import "dart:convert";
|
|
|
|
import "package:http/http.dart" as http;
|
|
|
|
import "package:client/data.dart";
|
|
|
|
import "service.dart";
|
|
|
|
class ApiService extends Service {
|
|
static const bool usingDocker = true;
|
|
final client = http.Client();
|
|
|
|
@override
|
|
Future<void> init() async { }
|
|
|
|
Uri get _base => usingDocker
|
|
? Uri(path: "api/")
|
|
: Uri(scheme: "http", host: "localhost", port: 80);
|
|
|
|
Future<List<T>?> _getAll<T>(Uri uri, FromJson<T> fromJson) async {
|
|
try {
|
|
final response = await client.get(uri);
|
|
if (response.statusCode != 200) return null;
|
|
final json = jsonDecode(response.body)["path"] as List;
|
|
return [
|
|
for (final obj in json.cast<Json>())
|
|
fromJson(obj),
|
|
];
|
|
} catch (error, stackTrace) {
|
|
// ignore: avoid_print
|
|
print("Error: $error\n$stackTrace");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<List<Trip>?> getTrips() => _getAll(
|
|
_base.resolve("trips"),
|
|
Trip.fromJson,
|
|
);
|
|
|
|
Future<Path?> getPath({
|
|
required Coordinates start,
|
|
required Coordinates end,
|
|
}) => _getAll(
|
|
_base.resolve("path").replace(
|
|
queryParameters: {
|
|
"start_lat": start.lat.toString(),
|
|
"start_lon": start.long.toString(),
|
|
"end_lat": end.lat.toString(),
|
|
"end_lon": end.long.toString(),
|
|
"time": DateTime.now().millisecondsSinceEpoch.toString(),
|
|
},
|
|
),
|
|
PathStep.fromJson,
|
|
);
|
|
|
|
|
|
}
|