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'; class GamesRepository extends ApiInterface { // Для обращения к api, с удобным выводом в консоль static final Dio _dio = Dio() ..interceptors.add(PrettyDioLogger( requestHeader: true, requestBody: true, )); // static const String _baseUrl = 'https://www.giantbomb.com'; static const String _baseUrl = 'https://api.rawg.io'; @override Future?> loadData(String? q) async { try { // const String url = '$_baseUrl/api/games/?api_key=cfbca5bd0888309438b9cef29bbe13364bece292&format=json&limit=30&filter=date_added:2020-01-01|2099-12-31,original_release_date:2020-01-01|2099-12-31,platforms:94'; String url = '$_baseUrl/api/games?key=793f3ef5bdb64d128bebbb6e68ab89bd&format=json&page_size=10&platforms=4'; if (q != null) { url += '&search=$q'; } final Response response = await _dio.get>(url); final GamesDto gamesDto = GamesDto.fromJson(response.data as Map); final List? data = gamesDto.data?.map((e) => e.toDomain()).toList(); return data; } on DioException catch (e) { return null; } } Future loadGameData(int? id) async { try { String url = '$_baseUrl/api/games/${id.toString()}?key=793f3ef5bdb64d128bebbb6e68ab89bd&format=json'; final Response response = await _dio.get>(url); final GameDto gameDto = GameDto.fromJson(response.data as Map); final GameData data = gameDto.toDomain(); return data; } on DioException catch (e) { return null; } } }