59 lines
2.0 KiB
Dart
59 lines
2.0 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';
|
|||
|
|
|||
|
|
|||
|
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<List<GameData>?> 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<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
|||
|
|
|||
|
final GamesDto gamesDto = GamesDto.fromJson(response.data as Map<String, dynamic>);
|
|||
|
|
|||
|
final List<GameData>? data = gamesDto.data?.map((e) => e.toDomain()).toList();
|
|||
|
|
|||
|
return data;
|
|||
|
}
|
|||
|
on DioException catch (e) {
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|