pmu_flutter/lec6/lib/data/repository/potter_repository.dart

45 lines
1.2 KiB
Dart
Raw Normal View History

2024-10-18 01:15:10 +04:00
import 'package:dio/dio.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import 'package:test_app/data/dto/characters_dto.dart';
import 'package:test_app/data/mapper/characters_mapper.dart';
import 'package:test_app/data/repository/api_interface.dart';
import 'package:test_app/domain/home.dart';
class PotterRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: false,
));
static const String _baseUrl = 'https://api.potterdb.com';
@override
Future<HomeData?> loadData({
String? q,
int page = 1,
int size = 10,
OnErrorCallback? onError,
}) async {
try {
const String url = '$_baseUrl/v1/characters';
final Response<dynamic> response = await _dio.get(
url,
queryParameters: {
'filter[name_cont]': q,
'page[number]': page,
'page[size]': size,
},
);
final CharactersDto dto =
CharactersDto.fromJson(response.data as Map<String, dynamic>);
return dto.toDomain();
} on DioException catch (e) {
onError?.call(e.error?.toString());
return null;
}
}
}