parent
afcfe33442
commit
830ba8634b
@ -1,7 +1,12 @@ |
||||
library db_construction_site; |
||||
|
||||
/// A Calculator. |
||||
class Calculator { |
||||
/// Returns [value] plus 1. |
||||
int addOne(int value) => value + 1; |
||||
import 'package:db_construction_site/services/connection_service.dart'; |
||||
|
||||
export 'models/index.dart'; |
||||
|
||||
class DBConstructionSite { |
||||
final ConnectionService connections; |
||||
|
||||
DBConstructionSite({baseUrl = 'https://bauinfos.deutschebahn.com/app'}) |
||||
: connections = ConnectionService(baseUrl); |
||||
} |
||||
|
@ -0,0 +1,15 @@ |
||||
import 'package:json_annotation/json_annotation.dart'; |
||||
|
||||
import 'connection_type.dart'; |
||||
|
||||
part 'connection_type_converter.g.dart'; |
||||
|
||||
@JsonSerializable(createFactory: false) |
||||
class ConnectionTypeConverter { |
||||
final ConnectionType type; |
||||
|
||||
const ConnectionTypeConverter(this.type); |
||||
|
||||
@JsonKey(ignore: true) |
||||
String get value => _$ConnectionTypeConverterToJson(this)["type"]; |
||||
} |
@ -0,0 +1,19 @@ |
||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||
|
||||
part of 'connection_type_converter.dart'; |
||||
|
||||
// ************************************************************************** |
||||
// JsonSerializableGenerator |
||||
// ************************************************************************** |
||||
|
||||
Map<String, dynamic> _$ConnectionTypeConverterToJson( |
||||
ConnectionTypeConverter instance) => |
||||
<String, dynamic>{ |
||||
'type': _$ConnectionTypeEnumMap[instance.type], |
||||
}; |
||||
|
||||
const _$ConnectionTypeEnumMap = { |
||||
ConnectionType.longDistance: 'fern', |
||||
ConnectionType.regional: 'regional', |
||||
ConnectionType.sBahn: 'sbahn', |
||||
}; |
@ -0,0 +1,3 @@ |
||||
export 'connection.dart'; |
||||
export 'region.dart'; |
||||
export 'connection_type.dart'; |
@ -0,0 +1,15 @@ |
||||
import 'package:json_annotation/json_annotation.dart'; |
||||
|
||||
import 'region.dart'; |
||||
|
||||
part 'region_converter.g.dart'; |
||||
|
||||
@JsonSerializable(createFactory: false) |
||||
class RegionConverter { |
||||
final Region region; |
||||
|
||||
const RegionConverter(this.region); |
||||
|
||||
@JsonKey(ignore: true) |
||||
String get value => _$RegionConverterToJson(this)["region"]; |
||||
} |
@ -0,0 +1,28 @@ |
||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||
|
||||
part of 'region_converter.dart'; |
||||
|
||||
// ************************************************************************** |
||||
// JsonSerializableGenerator |
||||
// ************************************************************************** |
||||
|
||||
Map<String, dynamic> _$RegionConverterToJson(RegionConverter instance) => |
||||
<String, dynamic>{ |
||||
'region': _$RegionEnumMap[instance.region], |
||||
}; |
||||
|
||||
const _$RegionEnumMap = { |
||||
Region.lowerSaxonyBremen: 'niedersachen-bremen', |
||||
Region.north: 'norden', |
||||
Region.mecklenburgWesternPomerania: 'mvp', |
||||
Region.berlinBrandenburg: 'berlin-bb', |
||||
Region.saxonyAnhalt: 'sachsen-anhalt', |
||||
Region.saxony: 'sachsen', |
||||
Region.bavaria: 'bayern', |
||||
Region.thuringia: 'thueringen', |
||||
Region.badenWuerttemberg: 'baden-w', |
||||
Region.rhinelandPalatinate: 'rheinland-pfalz', |
||||
Region.hesse: 'hessen', |
||||
Region.northrhineWestphalia: 'nrw', |
||||
Region.longDistance: 'fernverkehr', |
||||
}; |
@ -0,0 +1,34 @@ |
||||
import 'dart:convert'; |
||||
import 'dart:io'; |
||||
|
||||
import 'package:db_construction_site/models/connection.dart'; |
||||
import 'package:db_construction_site/models/connection_type.dart'; |
||||
import 'package:db_construction_site/models/connection_type_converter.dart'; |
||||
import 'package:db_construction_site/models/region.dart'; |
||||
import 'package:db_construction_site/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 HttpException("unexpected status code ${result.statusCode}", |
||||
uri: result.request.url); |
||||
} |
||||
final List<dynamic> connections = JsonDecoder().convert(result.body); |
||||
return connections |
||||
.map((connection) => Connection.fromJson(connection)) |
||||
.toList(); |
||||
} |
||||
} |
Loading…
Reference in new issue