loadChaptersByUrl static method

Future<Chapters> loadChaptersByUrl({
  1. required String url,
  2. dynamic timeout = const Duration(seconds: 20),
})

Podcasts that support the newer podcast namespace can include chapter markers. Typically this is in the form of a Url in the RSS feed pointing to a JSON file. This method takes the Url and loads the chapters, return a populated Chapters object.

Implementation

static Future<Chapters> loadChaptersByUrl({
  required String url,
  final timeout = const Duration(seconds: 20),
}) async {
  final client = Dio(
    BaseOptions(
      connectTimeout: timeout,
      receiveTimeout: timeout,
    ),
  );

  var chapters = Chapters();

  try {
    final response = await client.get(url);

    if (response.statusCode == 200) {
      _loadChapters(response, chapters);
    } else {
      throw PodcastFailedException('Failed to download chapters file');
    }
  } on DioException catch (e) {
    switch (e.type) {
      case DioExceptionType.connectionTimeout:
      case DioExceptionType.sendTimeout:
      case DioExceptionType.receiveTimeout:
        throw PodcastTimeoutException(e.message ?? '');
      case DioExceptionType.connectionError:
      case DioExceptionType.badResponse:
        throw PodcastFailedException(e.message ?? '');
      case DioExceptionType.badCertificate:
        throw PodcastCertificateException(e.message ?? '');
      case DioExceptionType.cancel:
        throw PodcastCancelledException(e.message ?? '');
      case DioExceptionType.unknown:
        throw PodcastUnknownException(e.message ?? '');
    }
  }

  return chapters;
}