pmu/lib/repositories/potter_repository.dart
2024-10-27 19:38:53 +04:00

36 lines
1.1 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_app/data/mappers/characters_mapper.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../data/dtos/characters_dto.dart';
import '../domain/models/card.dart';
import 'api_interface.dart';
class PotterRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://api.potterdb.com';
@override
Future<List<CardData>?> loadData(String? q) async {
try {
const String url = '$_baseUrl/v1/characters';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {"filter[name_cont]": q} : null,
);
final CharactersDto dto = CharactersDto.fromJson(
response.data as Map<String, dynamic>);
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e) {
return null;
}
}
}