MusicPlayer/lib/api.dart

41 lines
959 B
Dart
Raw Normal View History

2022-03-23 18:38:58 +00:00
import 'package:dio/dio.dart';
import 'package:music_web_player/config.dart';
class MusicEntry {
final int id;
final String artist;
final String title;
const MusicEntry({
required this.id,
required this.artist,
required this.title,
});
2022-03-23 20:15:29 +00:00
String get coverCacheKey => "cover-$id";
2022-03-23 18:38:58 +00:00
}
2022-03-23 20:15:29 +00:00
typedef MusicsList = List<MusicEntry>;
2022-03-23 18:38:58 +00:00
class API {
/// Get the list of music
2022-03-23 20:15:29 +00:00
static Future<MusicsList> getList() async {
2022-03-23 18:38:58 +00:00
final response = await Dio().get(config.apiURL + "/list",
options: Options(headers: {"Token": config.apiToken}));
if (response.statusCode != 200) {
throw Exception("Request failed with status ${response.statusCode} !");
}
return response.data
.map((r) =>
MusicEntry(id: r["id"], artist: r["artist"], title: r["title"]))
.toList()
.cast<MusicEntry>();
}
}
2022-03-23 20:15:29 +00:00
extension MusicEntryAPIExt on MusicEntry {
String get coverURL => "${config.apiURL}/cover/$id";
}