downloadBytes method

Future<PlexApiResponse> downloadBytes(
  1. String url, {
  2. Map<String, dynamic>? query,
  3. Map<String, String>? headers,
})

Downloads binary content from url and returns it in memory as Uint8List.

Mirrors get for URL/query/headers handling but uses http.Response.bodyBytes instead of decoding the response as JSON or text.

Implementation

Future<PlexApiResponse> downloadBytes(
  String url, {
  Map<String, dynamic>? query,
  Map<String, String>? headers,
}) async {
  if (await isNetworkAvailable() == false) {
    return _noNetwork;
  }

  if (query != null && query.isNotEmpty) {
    url += "?";
    query.forEach((key, value) {
      url += "$key=$value&";
    });
    url = url.substring(0, url.length - 1);
  }

  var currentHeaders = <String, String>{};

  if (addHeaders != null) {
    var constHeaders = await addHeaders!.call();
    currentHeaders.addAll(constHeaders);
  }

  if (headers != null) {
    currentHeaders.addAll(headers);
  }

  try {
    var startTime = DateTime.now();
    var uri = Uri.parse(_isValidUrl(url) ? url : _apiUrl() + url);
    if (kDebugMode) print("Started: ${uri.toString()}");

    var data = await http.get(uri, headers: currentHeaders);
    var diffInMillis = DateTime.now().difference(startTime).inMilliseconds;
    if (kDebugMode) print("Completed: ${data.statusCode}: ${uri.toString()} in ${diffInMillis}ms");
    if (data.statusCode.toString().startsWith("2")) {
      return PlexSuccess.bytes(data.bodyBytes, data.statusCode);
    } else {
      if (data.body.isEmpty) {
        return PlexError(data.statusCode, data.reasonPhrase ?? data.body);
      }
      return PlexError(data.statusCode, data.body);
    }
  } catch (e) {
    if (e is SocketException) {
      return _connectionFailed;
    }
    if (kDebugMode) print("Error: ${e.toString()}");
    return PlexError(400, e.toString());
  }
}