62 lines
1.9 KiB
Dart
62 lines
1.9 KiB
Dart
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';
|
||
|
||
import '../../domain/models/page_of_games_home.dart';
|
||
|
||
|
||
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
|
||
Future<PageOfGames?> loadData({OnErrorCallback? onError, String? q, int page = 1, int pageSize = 10}) async {
|
||
try {
|
||
String url = '$_baseUrl/api/games?key=793f3ef5bdb64d128bebbb6e68ab89bd&format=json&platforms=4';
|
||
url += '&page=$page';
|
||
url += '&page_size=$pageSize';
|
||
|
||
if (q != null) {
|
||
url += '&search=$q';
|
||
}
|
||
|
||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
||
|
||
final GamesDto gamesDto = GamesDto.fromJson(response.data as Map<String, dynamic>);
|
||
|
||
final PageOfGames? data = gamesDto.toDomain();
|
||
|
||
return data;
|
||
}
|
||
on DioException catch (e) {
|
||
onError?.call(e.error?.toString());
|
||
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;
|
||
}
|
||
}
|
||
} |