PIbd31_Razubaev.S.M._PMU/lib/data/repositories/manga_repository.dart
2024-10-16 12:08:15 +04:00

33 lines
1.1 KiB
Dart

import 'package:flutter_app/data/dtos/mangas_dto.dart';
import 'package:flutter_app/data/mappers/mangas_mapper.dart';
import 'package:flutter_app/data/repositories/api_interface.dart';
import 'package:dio/dio.dart';
import 'package:flutter_app/domain/models/carddata.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class MangaRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://api.jikan.moe';
@override
Future<List<CardData>?> loadData({String? q}) async {
try {
const String url = '$_baseUrl/v4/manga';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {'q': q} : null);
final MangasDto dto =
MangasDto.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;
}
}
}