diff --git a/lib/services/cloud_service.dart b/lib/services/cloud_service.dart index 427046c..d62baf9 100644 --- a/lib/services/cloud_service.dart +++ b/lib/services/cloud_service.dart @@ -8,6 +8,8 @@ import '../services/melo_logger.dart'; /// Cloud-Sync Service für Melo Registry (v3 — vollständiges Sync-System) /// Authentifizierung via Baka-Auth JWT-Token class CloudService { + static final http.Client _client = http.Client(); + static String get _base => AppConfig.cloudUrl; String _user = ''; @@ -15,7 +17,7 @@ class CloudService { Future login(String user) async { _user = user; try { - final r = await http + final r = await _client .get(Uri.parse('$_base/api/cloud/status'), headers: _authHeader) .timeout(const Duration(seconds: 5)); @@ -63,7 +65,7 @@ class CloudService { req.files.add( await http.MultipartFile.fromPath('file', filepath, filename: filename)); - final resp = await req.send().timeout(const Duration(seconds: 120)); + final resp = await _client.send(req).timeout(const Duration(seconds: 120)); final body = jsonDecode(await resp.stream.bytesToString()); return body['song_id'] as String?; } catch (e) { @@ -74,7 +76,7 @@ class CloudService { Future download(String songId, String destPath) async { try { - final r = await http + final r = await _client .get(Uri.parse('$_base/api/cloud/download/$songId'), headers: _authHeader) .timeout(const Duration(seconds: 120)); @@ -91,7 +93,7 @@ class CloudService { Future delete(String songId) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base/api/cloud/delete'), headers: _jsonHeader, body: jsonEncode({'song_id': songId})) @@ -105,7 +107,7 @@ class CloudService { /// Sync-Änderungen seit einem Zeitstempel abrufen Future> syncChanges(String since) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base/api/cloud/sync'), headers: _jsonHeader, body: jsonEncode({'since': since})) @@ -153,7 +155,7 @@ class CloudService { /// Song aus Playlist entfernen Future removeSongFromPlaylist(int playlistId, String songId) async { try { - final r = await http + final r = await _client .delete( Uri.parse( '$_base/api/cloud/playlists/$playlistId/songs/$songId'), @@ -169,7 +171,7 @@ class CloudService { Future updatePlaylistPositions( int playlistId, List> positions) async { try { - final r = await http + final r = await _client .put( Uri.parse( '$_base/api/cloud/playlists/$playlistId/positions'), @@ -231,7 +233,7 @@ class CloudService { Future> globalList() async { try { - final r = await http + final r = await _client .get(Uri.parse('$_base/api/cloud/global'), headers: _authHeader) .timeout(const Duration(seconds: 10)); if (r.statusCode == 200) { @@ -243,7 +245,7 @@ class CloudService { Future toggleGlobal(String songId) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base/api/cloud/toggle-global'), headers: _jsonHeader, body: jsonEncode({'song_id': songId})) @@ -256,7 +258,7 @@ class CloudService { Future share(List songIds) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base/api/cloud/share'), headers: _jsonHeader, body: jsonEncode({'song_ids': songIds})) @@ -271,7 +273,7 @@ class CloudService { Future importCode(String code) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base/api/cloud/import'), headers: _jsonHeader, body: jsonEncode({'code': code})) @@ -283,7 +285,7 @@ class CloudService { Future> getCorrupted() async { try { - final r = await http + final r = await _client .get(Uri.parse('$_base/api/cloud/corrupted'), headers: _authHeader) .timeout(const Duration(seconds: 15)); if (r.statusCode == 200) { @@ -300,7 +302,7 @@ class CloudService { Future _get(String path) async { try { - final r = await http + final r = await _client .get(Uri.parse('$_base$path'), headers: _authHeader) .timeout(const Duration(seconds: 10)); if (r.statusCode == 200) return jsonDecode(r.body); @@ -313,7 +315,7 @@ class CloudService { Future _post(String path, Map body) async { try { - final r = await http + final r = await _client .post(Uri.parse('$_base$path'), headers: _jsonHeader, body: jsonEncode(body)) .timeout(const Duration(seconds: 10)); diff --git a/lib/services/download_service.dart b/lib/services/download_service.dart index c13966d..7eef276 100644 --- a/lib/services/download_service.dart +++ b/lib/services/download_service.dart @@ -13,6 +13,8 @@ import '../config/app_config.dart'; /// Download-Service: Lädt YouTube-Audio über den yt-proxy herunter. /// Nutzt ChangeNotifier für UI-Updates via ListenableBuilder. class DownloadService extends ChangeNotifier { + static final http.Client _client = http.Client(); + static final DownloadService _instanz = DownloadService._(); factory DownloadService() => _instanz; DownloadService._(); @@ -349,7 +351,7 @@ class DownloadService extends ChangeNotifier { Object? lastError; for (int versuch = 0; versuch < maxVersuche; versuch++) { try { - final response = await http + final response = await _client .post(url, headers: headers, body: body) .timeout(timeout); return response; @@ -386,7 +388,7 @@ class DownloadService extends ChangeNotifier { Object? lastError; for (int versuch = 0; versuch < maxVersuche; versuch++) { try { - final response = await http + final response = await _client .get(url, headers: headers) .timeout(timeout); return response;