PIbd-31_IevlewaMD_PMD/lib/data/repositories/album_repository.dart
2024-11-08 13:07:43 +04:00

63 lines
2.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:pmd_labs/data/dto/album_dto.dart';
import 'package:pmd_labs/data/mapper/album_mapper.dart'; // Путь к вашему мапперу
import 'package:dio/dio.dart';
import 'package:pmd_labs/card_data.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
abstract class ApiInterface {
Future<List<CardData>?> loadData();
}
class AlbumRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String apiKey = '535d9d508a785fae99bfb492d8f15a58';
static const String _baseUrl = 'https://api.your_album_api.com'; // Замените на ваш базовый URL
// Метод для загрузки списка альбомов по названию
@override
Future<List<CardData>?> loadData({String? albumName}) async {
albumName ??= 'a';
final String url =
'https://ws.audioscrobbler.com/2.0/?method=album.search&album=$albumName&api_key=$apiKey&format=json';
try {
final response = await _dio.get(url);
if (response.statusCode == 200) {
final List<AlbumDataDto> albumsData =
AlbumDataDto().fetchAlbums(
response.data['results']['albummatches']['album']);
return albumsData.map((album) => album.toDomain()).toList();
} else {
throw Exception('Failed to load albums: ${response.statusCode}');
}
} catch (e) {
throw Exception('Error during fetching albums: $e');
}
}
// Получение информации об альбоме
Future<AlbumDataDto?> getAlbumInfo(AlbumDataDto album) async {
final String url =
'https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=$apiKey&artist=${album
.artist}&album=${album.title}&format=json';
try {
final response = await _dio.get(url);
if (response.statusCode == 200) {
return await AlbumDataDto().fetchAlbumDetails(response.data, album);
} else {
throw Exception('Failed to load album info: ${response.statusCode}');
}
} catch (e) {
throw Exception('Error during fetching album info: $e');
}
}
}