Unofficial Dart implementation of Deutsche Bahn’s construction site API
https://pub.dev/packages/db_construction_site
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.2 KiB
32 lines
1.2 KiB
import 'dart:convert';
|
|
|
|
import 'package:db_construction_site/src/models/connection.dart';
|
|
import 'package:db_construction_site/src/models/connection_type.dart';
|
|
import 'package:db_construction_site/src/models/connection_type_converter.dart';
|
|
import 'package:db_construction_site/src/models/region.dart';
|
|
import 'package:db_construction_site/src/models/region_converter.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class ConnectionService {
|
|
final String baseUrl;
|
|
|
|
const ConnectionService(this.baseUrl);
|
|
|
|
Future<List<Connection>> findConnections(Region region,
|
|
{ConnectionType connectionType}) async {
|
|
final regionValue = RegionConverter(region).value;
|
|
var url = '$baseUrl/list/${regionValue}';
|
|
if (connectionType != null) {
|
|
final connectionTypeValue = ConnectionTypeConverter(connectionType).value;
|
|
url += ',${connectionTypeValue}';
|
|
}
|
|
final result = await http.get(url);
|
|
if (result.statusCode ~/ 100 != 2) {
|
|
throw Exception('unexpected status code ${result.statusCode}');
|
|
}
|
|
final List<dynamic> connections = JsonDecoder().convert(result.body);
|
|
return connections
|
|
.map((connection) => Connection.fromJson(connection))
|
|
.toList();
|
|
}
|
|
}
|
|
|