2024-11-15 21:47:58 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/data/dto/games_dto.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/data/mappers/games_mapper.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/data/repositories/api_interface.dart';
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/domain/models/game.dart';
|
|
|
|
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import '../../domain/models/page_of_games_home.dart';
|
|
|
|
|
|
2024-11-15 21:47:58 +04:00
|
|
|
|
|
|
|
|
|
class GamesRepository extends ApiInterface {
|
|
|
|
|
// Для обращения к api, с удобным выводом в консоль
|
|
|
|
|
static final Dio _dio = Dio()
|
|
|
|
|
..interceptors.add(PrettyDioLogger(
|
|
|
|
|
requestHeader: true,
|
|
|
|
|
requestBody: true,
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
static const String _baseUrl = 'https://api.rawg.io';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
2024-11-19 23:39:56 +04:00
|
|
|
|
Future<PageOfGames?> loadData({OnErrorCallback? onError, String? q, int page = 1, int pageSize = 10}) async {
|
2024-11-15 21:47:58 +04:00
|
|
|
|
try {
|
2024-11-19 23:39:56 +04:00
|
|
|
|
String url = '$_baseUrl/api/games?key=793f3ef5bdb64d128bebbb6e68ab89bd&format=json&platforms=4';
|
|
|
|
|
url += '&page=$page';
|
|
|
|
|
url += '&page_size=$pageSize';
|
|
|
|
|
|
2024-11-15 21:47:58 +04:00
|
|
|
|
if (q != null) {
|
|
|
|
|
url += '&search=$q';
|
|
|
|
|
}
|
2024-11-19 23:39:56 +04:00
|
|
|
|
|
2024-11-15 21:47:58 +04:00
|
|
|
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
|
|
|
|
|
|
|
|
|
final GamesDto gamesDto = GamesDto.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
final PageOfGames? data = gamesDto.toDomain();
|
2024-11-15 21:47:58 +04:00
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
on DioException catch (e) {
|
2024-11-19 23:39:56 +04:00
|
|
|
|
onError?.call(e.error?.toString());
|
2024-11-15 21:47:58 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<GameData?> loadGameData(int? id) async {
|
|
|
|
|
try {
|
|
|
|
|
String url = '$_baseUrl/api/games/${id.toString()}?key=793f3ef5bdb64d128bebbb6e68ab89bd&format=json';
|
|
|
|
|
|
|
|
|
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
|
|
|
|
|
|
|
|
|
final GameDto gameDto = GameDto.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
|
|
|
|
|
final GameData data = gameDto.toDomain();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
on DioException catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|