get method

Future<List<DSBMobileNode>> get({
  1. DateTime? requestTime,
})

Get's a list of root nodes with various data from the DSB data server.

May throw DSBException if login has not been called or if the server returned unreadable data.

Implementation

Future<List<DSBMobileNode>> get({DateTime? requestTime}) async {
  if (_args.isEmpty) throw DSBException();

  requestTime ??= DateTime.now();
  var date = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(DateTime.now()) +
      '+0000'; // For some reason their API wants this.

  var argCopy = _args;
  argCopy.putIfAbsent('Date', () => date);
  argCopy.putIfAbsent('LastUpdate', () => date);

  final requestBody = <String, dynamic>{
    'req': {
      'DataType': 1,
      'Data': base64.encode(gzip.encode(utf8.encode(json.encode(argCopy)))),
    }
  };

  final response = await http.post(
    Uri.parse('https://www.dsbmobile.de/JsonHandler.ashx/GetData'),
    headers: {'content-type': 'application/json;charset=utf-8'},
    body: json.encode(requestBody),
  );
  dynamic d = json.decode(response.body)['d'];
  if (d == null) throw DSBException();
  final result = json.decode(utf8.decode(gzip.decode(base64.decode(d))))
      as Map<String, dynamic>;
  if (result.isEmpty || result['Resultcode'] != 0) throw DSBException();

  if (result['ResultMenuItems']! is Map) return [];
  return <DSBMobileNode>[
    ...result['ResultMenuItems'].map((f) => DSBMobileNode.parse(f))
  ];
}