diff --git a/lib/data/dtos/hero_dto.dart b/lib/data/dtos/hero_dto.dart index d4dcf44..02a1efc 100644 --- a/lib/data/dtos/hero_dto.dart +++ b/lib/data/dtos/hero_dto.dart @@ -10,13 +10,4 @@ class HeroDto { this.portraitUrl, this.description, }); - - factory HeroDto.fromJson(Map 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?, - ); - } } diff --git a/lib/data/mappers/hero_mapper.dart b/lib/data/mappers/hero_mapper.dart new file mode 100644 index 0000000..f191640 --- /dev/null +++ b/lib/data/mappers/hero_mapper.dart @@ -0,0 +1,16 @@ +import '../dtos/hero_dto.dart'; + +class HeroMapper { + static HeroDto fromJson(Map 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 fromJsonList(List jsonList) { + return jsonList.map((json) => fromJson(json as Map)).toList(); + } +} diff --git a/lib/data/repositories/hero_repository.dart b/lib/data/repositories/hero_repository.dart index 9bac377..22bcb4f 100644 --- a/lib/data/repositories/hero_repository.dart +++ b/lib/data/repositories/hero_repository.dart @@ -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> getHeroes() async { final heroes = await apiService.fetchHeroes(); - return heroes.map((hero) => HeroDto.fromJson(hero)).toList(); + return HeroMapper.fromJsonList(heroes); } Future getHeroDetails(int id) async { final hero = await apiService.fetchHeroDetails(id); - return HeroDto.fromJson(hero); + return HeroMapper.fromJson(hero); } } diff --git a/lib/services/api_service.dart b/lib/services/api_service.dart index b49fd61..99cbf7c 100644 --- a/lib/services/api_service.dart +++ b/lib/services/api_service.dart @@ -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> fetchHeroes() async { - final response = await http.get(Uri.parse('$baseUrl/v2/heroes')); - if (response.statusCode == 200) { - return json.decode(response.body) as List; - } else { - throw Exception('Failed to load heroes'); + try { + final response = await dio.get('/v2/heroes'); + return response.data as List; + } catch (e) { + throw Exception('Failed to load heroes: $e'); } } Future> 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; - } else { - throw Exception('Failed to load hero details'); - } - } - - Future> 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; - } else { - throw Exception('Failed to search hero'); + try { + final response = await dio.get('/v2/heroes/$id'); + return response.data as Map; + } catch (e) { + throw Exception('Failed to load hero details: $e'); } } }