PIbd-32_Stroev_V_M_PMD/lib/repo/potter_repo.dart

47 lines
1.2 KiB
Dart
Raw Normal View History

2024-10-22 08:55:54 +04:00
import 'package:dio/dio.dart';
import 'package:labs/data/mapper/CharacterDataDtoModel.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../data/dtos/characters_dto.dart';
import '../domain/models/Student.dart';
2024-10-30 22:39:56 +04:00
import '../domain/models/home.dart';
2024-10-22 08:55:54 +04:00
import 'api_interface.dart';
2024-10-30 22:39:56 +04:00
class PotterRepo extends ApiInterface {
2024-10-22 08:55:54 +04:00
static final Dio _dio = Dio()
2024-10-30 22:39:56 +04:00
..interceptors.add(PrettyDioLogger(
2024-10-22 08:55:54 +04:00
requestHeader: true,
requestBody: true,
2024-10-30 22:39:56 +04:00
));
2024-10-22 08:55:54 +04:00
static const String _baseUrl = 'https://api.potterdb.com';
@override
2024-10-30 22:39:56 +04:00
Future<HomeData?> loadData(
2024-11-04 13:21:57 +04:00
{OnErrorCallback? onError,
String? q,
int page = 1,
int pageSize = 25
}) async {
2024-10-30 22:39:56 +04:00
try {
2024-10-22 08:55:54 +04:00
const String url = '$_baseUrl/v1/characters';
2024-10-30 22:39:56 +04:00
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: {
2024-11-04 13:21:57 +04:00
'filter[name_cont]': q,
2024-10-30 22:39:56 +04:00
'page[number]': page,
'page[size]': pageSize,
},
);
final CharactersDto dto = CharactersDto.fromJson(
response.data as Map<String, dynamic>);
final HomeData data = dto.toDomain();
2024-10-22 08:55:54 +04:00
return data;
2024-10-30 22:39:56 +04:00
} on DioException catch (e) {
onError?.call(e.error?.toString());
return null;
}
2024-10-22 08:55:54 +04:00
}
}