MusicPlayer/lib/api.dart

58 lines
1.3 KiB
Dart

import 'package:dio/dio.dart';
import 'package:music_web_player/config.dart';
class MusicEntry implements Comparable<MusicEntry> {
final int id;
final String artist;
final String title;
const MusicEntry({
required this.id,
required this.artist,
required this.title,
});
String get coverCacheKey => "cover-$id";
String get fullName => "$artist - $title";
@override
String toString() => "MusicEntry($id, $artist, $title)";
@override
int compareTo(MusicEntry other) {
return fullName.compareTo(other.fullName);
}
}
typedef MusicsList = List<MusicEntry>;
class API {
/// Get the list of music
static Future<MusicsList> getList() async {
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} !");
}
final MusicsList list = response.data
.map((r) =>
MusicEntry(id: r["id"], artist: r["artist"], title: r["title"]))
.toList()
.cast<MusicEntry>();
list.sort();
return list;
}
}
extension MusicEntryAPIExt on MusicEntry {
String get coverURL => "${config.apiURL}/cover/$id";
String get musicURL =>
"${config.apiURL}/download/$id?token=${config.apiToken}";
}