MusicPlayer/lib/api.dart

41 lines
959 B
Dart

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,
});
String get coverCacheKey => "cover-$id";
}
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} !");
}
return response.data
.map((r) =>
MusicEntry(id: r["id"], artist: r["artist"], title: r["title"]))
.toList()
.cast<MusicEntry>();
}
}
extension MusicEntryAPIExt on MusicEntry {
String get coverURL => "${config.apiURL}/cover/$id";
}