PIbd31_Razubaev.S.M._PMU/lib/data/repositories/manga_repository.dart

44 lines
1.3 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter_app/components/utils/error_callback.dart';
2024-10-16 12:08:15 +04:00
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/presentation/home_page/home_page.dart';
2024-10-16 12:08:15 +04:00
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<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
int page = 1,
int pageSize = 25,
}) async {
2024-10-16 12:08:15 +04:00
try {
const String url = '$_baseUrl/v4/manga';
final Response<dynamic> response = await _dio
.get<Map<dynamic, dynamic>>(url, queryParameters: {
'q': q,
'page': page,
'limit': !(pageSize > 25) ? pageSize : 25
});
2024-10-16 12:08:15 +04:00
final MangasDto dto =
MangasDto.fromJson(response.data as Map<String, dynamic>);
final HomeData? data = dto.toDomain();
2024-10-16 12:08:15 +04:00
return data;
} on DioException catch (e) {
onError?.call(e.response?.statusMessage);
2024-10-16 12:08:15 +04:00
return null;
}
}
}