getDevice method

Future<Device?> getDevice()

Implementation

Future<Device?> getDevice() async {
  Uri uri;

  try {
    uri = Uri.parse(location!);
  } catch (e) {
    return null;
  }

  final request = await UpnpCommon.httpClient
      .getUrl(uri)
      .timeout(const Duration(seconds: 10));

  final response = await request.close();

  if (response.statusCode != 200) {
    throw Exception('ERROR: Failed to fetch device description.'
        ' Status Code: ${response.statusCode}');
  }

  XmlDocument doc;

  try {
    final content =
        await response.cast<List<int>>().transform(utf8.decoder).join();
    doc = XmlDocument.parse(content);
  } on Exception catch (e) {
    throw FormatException('ERROR: Failed to parse device'
        ' description. $e');
  }

  return Device()..loadFromXml(location, doc.rootElement);
}