This commit is contained in:
MaD 2024-12-18 10:10:10 +04:00
parent 97cad27d29
commit 418b9461b8
4 changed files with 37 additions and 34 deletions

View File

@ -10,13 +10,4 @@ class HeroDto {
this.portraitUrl,
this.description,
});
factory HeroDto.fromJson(Map<String, dynamic> json) {
return HeroDto(
id: json['id'] as int,
name: json['name'] as String,
portraitUrl: json['images']?['icon_image_small'] as String?,
description: json['description']?['lore'] as String?,
);
}
}

View File

@ -0,0 +1,16 @@
import '../dtos/hero_dto.dart';
class HeroMapper {
static HeroDto fromJson(Map<String, dynamic> json) {
return HeroDto(
id: json['id'] as int,
name: json['name'] as String,
portraitUrl: json['images']?['icon_image_small'] as String?,
description: json['description']?['lore'] as String?,
);
}
static List<HeroDto> fromJsonList(List<dynamic> jsonList) {
return jsonList.map((json) => fromJson(json as Map<String, dynamic>)).toList();
}
}

View File

@ -1,5 +1,6 @@
import '../dtos/hero_dto.dart';
import '../../services/api_service.dart';
import '../mappers/hero_mapper.dart';
class HeroRepository {
final ApiService apiService;
@ -8,11 +9,11 @@ class HeroRepository {
Future<List<HeroDto>> getHeroes() async {
final heroes = await apiService.fetchHeroes();
return heroes.map((hero) => HeroDto.fromJson(hero)).toList();
return HeroMapper.fromJsonList(heroes);
}
Future<HeroDto> getHeroDetails(int id) async {
final hero = await apiService.fetchHeroDetails(id);
return HeroDto.fromJson(hero);
return HeroMapper.fromJson(hero);
}
}

View File

@ -1,35 +1,30 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';
class ApiService {
final String baseUrl;
final Dio dio;
ApiService({required this.baseUrl});
ApiService({required String baseUrl})
: dio = Dio(BaseOptions(
baseUrl: baseUrl,
connectTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 3),
));
Future<List<dynamic>> fetchHeroes() async {
final response = await http.get(Uri.parse('$baseUrl/v2/heroes'));
if (response.statusCode == 200) {
return json.decode(response.body) as List<dynamic>;
} else {
throw Exception('Failed to load heroes');
try {
final response = await dio.get('/v2/heroes');
return response.data as List<dynamic>;
} catch (e) {
throw Exception('Failed to load heroes: $e');
}
}
Future<Map<String, dynamic>> fetchHeroDetails(int id) async {
final response = await http.get(Uri.parse('$baseUrl/v2/heroes/$id'));
if (response.statusCode == 200) {
return json.decode(response.body) as Map<String, dynamic>;
} else {
throw Exception('Failed to load hero details');
}
}
Future<Map<String, dynamic>> searchHeroByName(String name) async {
final response = await http.get(Uri.parse('$baseUrl/v2/heroes/by-name/$name'));
if (response.statusCode == 200) {
return json.decode(response.body) as Map<String, dynamic>;
} else {
throw Exception('Failed to search hero');
try {
final response = await dio.get('/v2/heroes/$id');
return response.data as Map<String, dynamic>;
} catch (e) {
throw Exception('Failed to load hero details: $e');
}
}
}