109 lines
3.9 KiB
Dart
109 lines
3.9 KiB
Dart
import '../models/pokemon.dart';
|
|
import '../utils/pokemon_type.dart';
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:flutter/foundation.dart'; // Import for debugPrint
|
|
|
|
// Classe qui permet de récupérer les données des pokémons depuis l'API Tyradex
|
|
// On utilise la librairie http pour effectuer les requêtes
|
|
// On utilise la librairie dart:convert pour convertir les données JSON en objet Dart
|
|
class PokemonApi {
|
|
static const String baseUrl = 'tyradex.app';
|
|
static const String pokemonUrl = 'api/v1/pokemon';
|
|
|
|
static Future<Pokemon> getPokemon(int id) async {
|
|
print('API Call: Fetching Pokémon $id from Tyradex...');
|
|
// On utilise la méthode get de la classe http pour effectuer une requête GET
|
|
// On utilise Uri.https pour construire l'URL de la requête
|
|
var response = await http.get(Uri.https(baseUrl, "$pokemonUrl/$id"));
|
|
if (response.statusCode != 200) {
|
|
// Si le code de retour de la requête n'est pas 200, on lève une exception
|
|
throw Exception('Erreur lors de la récupération du pokémon $id, code de retour ${response.statusCode}');
|
|
}
|
|
// On utilise la méthode jsonDecode de la librairie dart:convert pour convertir le corps de la réponse en fichier JSON
|
|
var json = jsonDecode(response.body);
|
|
// Récupération du nom en français
|
|
String name = json['name']['fr'] ?? json['name']['en'] ?? 'unknown';
|
|
|
|
// Récupération des types (en français dans l'API Tyradex)
|
|
List types = json['types'] ?? [];
|
|
PokemonType type1 = types.isNotEmpty
|
|
? frenchTypeToEnum(types[0]['name'])
|
|
: PokemonType.unknown;
|
|
PokemonType? type2 = types.length > 1
|
|
? frenchTypeToEnum(types[1]['name'])
|
|
: null;
|
|
|
|
// Récupération des statistiques
|
|
Map<String, dynamic>? stats = json['stats'];
|
|
int hp = stats?['hp'] ?? 0;
|
|
int atk = stats?['atk'] ?? 0;
|
|
int def = stats?['def'] ?? 0;
|
|
int spd = stats?['vit'] ?? 0; // 'vit' est la clé pour la vitesse dans tyradex.app
|
|
|
|
// Récupération de la description
|
|
String? description = json['category'];
|
|
|
|
// On crée un objet Pokemon à partir du fichier JSON
|
|
return Pokemon(
|
|
name: name,
|
|
id: id,
|
|
type1: type1,
|
|
type2: type2,
|
|
hp: hp,
|
|
atk: atk,
|
|
def: def,
|
|
spd: spd,
|
|
description: description,
|
|
);
|
|
}
|
|
|
|
static Future<List<Pokemon>> getAllPokemon() async {
|
|
print('API Call: Fetching ALL Pokémon from Tyradex...');
|
|
final response = await http.get(Uri.https(baseUrl, pokemonUrl));
|
|
|
|
if (response.statusCode == 200) {
|
|
List<dynamic> jsonList = jsonDecode(response.body);
|
|
List<Pokemon> allPokemon = [];
|
|
|
|
for (var json in jsonList) {
|
|
// Skip default tyradex id 0 response which is generic typing
|
|
if(json['pokedex_id'] == 0) continue;
|
|
|
|
try {
|
|
String name = json['name']['fr'];
|
|
int id = json['pokedex_id'];
|
|
List<dynamic> types = json['types'] ?? [];
|
|
PokemonType type1 = frenchTypeToEnum(types[0]['name']);
|
|
PokemonType? type2 = types.length > 1 ? frenchTypeToEnum(types[1]['name']) : null;
|
|
|
|
Map<String, dynamic>? stats = json['stats'];
|
|
int hp = stats?['hp'] ?? 0;
|
|
int atk = stats?['atk'] ?? 0;
|
|
int def = stats?['def'] ?? 0;
|
|
int spd = stats?['vit'] ?? 0;
|
|
|
|
String? description = json['category'];
|
|
|
|
allPokemon.add(Pokemon(
|
|
name: name,
|
|
id: id,
|
|
type1: type1,
|
|
type2: type2,
|
|
hp: hp,
|
|
atk: atk,
|
|
def: def,
|
|
spd: spd,
|
|
description: description,
|
|
));
|
|
} catch (e) {
|
|
debugPrint("Failed parsing pokemon: ${json['name']} - $e");
|
|
}
|
|
}
|
|
return allPokemon;
|
|
} else {
|
|
throw Exception('Failed to load pokemon');
|
|
}
|
|
}
|
|
} |