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

44 lines
1.3 KiB
Dart

import 'package:flutter_app/components/utils/error_callback.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/presentation/home_page/home_page.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<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
int page = 1,
int pageSize = 25,
}) async {
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
});
final MangasDto dto =
MangasDto.fromJson(response.data as Map<String, dynamic>);
final HomeData? data = dto.toDomain();
return data;
} on DioException catch (e) {
onError?.call(e.response?.statusMessage);
return null;
}
}
}