Files
gamemanager/lib/services/api_service.dart
T
neckfire 851a55cf38
Build APK / build (push) Successful in 3m6s
app : écran de gestion des mods via Modrinth
- Écran Mods (recherche Modrinth + liste installée) accessible depuis le détail d_un serveur Minecraft
- Ajout/retrait réservés aux admins (bouton masqué/averti sinon), via l_API gamepanel
- Service API : role(), mods(), searchMods(), addMod(), removeMod() + modèle ModHit
2026-10-09 18:34:31 +02:00

215 lines
8.8 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import '../config.dart';
import '../models.dart';
import 'auth_service.dart';
/// Client de l'API GameManager (Bearer JWT pocket-id).
class ApiService {
ApiService(this._auth);
final AuthService _auth;
Future<String> get baseUrl async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('api_base') ?? Config.defaultApiBase;
}
Future<void> setBaseUrl(String url) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('api_base', url.trim().replaceAll(RegExp(r'/$'), ''));
}
Future<Map<String, String>> _headers() async {
final token = await _auth.validToken();
return {'Authorization': 'Bearer ${token ?? ''}', 'Accept': 'application/json'};
}
Future<List<GameServer>> servers() async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) throw ApiException('Erreur ${res.statusCode}');
final list = jsonDecode(res.body) as List;
return list.map((e) => GameServer.fromJson(e as Map<String, dynamic>)).toList();
}
Future<GameServer> server(String id) async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers/$id'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) throw ApiException('Erreur ${res.statusCode}');
return GameServer.fromJson(jsonDecode(res.body) as Map<String, dynamic>);
}
/// Bloc « Se connecter » (adresse, version, vocal, carte, lien modpack).
Future<ConnectInfo> connectInfo(String id) async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers/$id/connect'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) throw ApiException('Erreur ${res.statusCode}');
return ConnectInfo.fromJson(jsonDecode(res.body) as Map<String, dynamic>, base);
}
Future<List<DockerContainer>> containers() async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/containers'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) throw ApiException('Erreur ${res.statusCode}');
return (jsonDecode(res.body) as List)
.map((e) => DockerContainer.fromJson(e as Map<String, dynamic>))
.toList();
}
Future<GameServer> createServer(Map<String, dynamic> payload) async {
final base = await baseUrl;
final res = await http.post(
Uri.parse('$base/api/servers'),
headers: {...await _headers(), 'Content-Type': 'application/json'},
body: jsonEncode(payload),
);
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) {
throw ApiException(_msg(res.body) ?? 'Création refusée (${res.statusCode})');
}
return GameServer.fromJson(jsonDecode(res.body) as Map<String, dynamic>);
}
Future<void> deleteServer(String id) async {
final base = await baseUrl;
final res = await http.delete(Uri.parse('$base/api/servers/$id'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) {
throw ApiException(_msg(res.body) ?? 'Suppression refusée (${res.statusCode})');
}
}
String? _msg(String body) {
try {
return (jsonDecode(body) as Map<String, dynamic>)['detail'] as String?;
} catch (_) {
return null;
}
}
Future<void> start(String id) => _action(id, 'start');
Future<void> stop(String id) => _action(id, 'stop');
Future<void> _action(String id, String action) async {
final base = await baseUrl;
final res = await http.post(
Uri.parse('$base/api/servers/$id/$action'),
headers: await _headers(),
);
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Action refusée (${res.statusCode})');
}
Future<String> iconUrl(String id) async => '${await baseUrl}/api/servers/$id/icon';
/// Stats d'utilisation du conteneur (CPU/RAM/réseau). `{running:false}` si éteint.
Future<Map<String, dynamic>> stats(String id) async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers/$id/stats'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) throw ApiException('Erreur ${res.statusCode}');
return jsonDecode(res.body) as Map<String, dynamic>;
}
/// Image importée du serveur (bytes), null si aucune.
Future<Uint8List?> imageBytes(String id) async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers/$id/image'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode != 200) return null;
return res.bodyBytes;
}
Future<void> uploadImage(String id, String filePath) async {
final base = await baseUrl;
final token = await _auth.validToken();
final req = http.MultipartRequest('POST', Uri.parse('$base/api/servers/$id/image'))
..headers['Authorization'] = 'Bearer ${token ?? ''}'
..files.add(await http.MultipartFile.fromPath('file', filePath));
final res = await http.Response.fromStream(await req.send());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Envoi refusé (${res.statusCode})');
}
Future<void> deleteImage(String id) async {
final base = await baseUrl;
final res = await http.delete(Uri.parse('$base/api/servers/$id/image'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Suppression refusée (${res.statusCode})');
}
// ── Rôle & mods (Modrinth) ──────────────────────────────────
Future<String> role() async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/me'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Profil indisponible (${res.statusCode})');
return (jsonDecode(res.body)['role'] as String?) ?? 'member';
}
Future<List<String>> mods(String id) async {
final base = await baseUrl;
final res = await http.get(Uri.parse('$base/api/servers/$id/mods'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Mods indisponibles (${res.statusCode})');
return ((jsonDecode(res.body)['projects'] as List?) ?? const []).cast<String>();
}
Future<List<ModHit>> searchMods(String q, String serverId) async {
final base = await baseUrl;
final uri = Uri.parse('$base/api/modrinth/search')
.replace(queryParameters: {'q': q, 'server_id': serverId});
final res = await http.get(uri, headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode >= 400) throw ApiException('Recherche échouée (${res.statusCode})');
return ((jsonDecode(res.body)['hits'] as List?) ?? const [])
.map((h) => ModHit.fromJson(h as Map<String, dynamic>))
.toList();
}
Future<void> addMod(String id, String slug) async {
final base = await baseUrl;
final res = await http.post(
Uri.parse('$base/api/servers/$id/mods'),
headers: {...await _headers(), 'Content-Type': 'application/json'},
body: jsonEncode({'slug': slug}),
);
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode == 403) throw ApiException('Réservé aux administrateurs');
if (res.statusCode >= 400) throw ApiException(_detail(res) ?? 'Ajout refusé (${res.statusCode})');
}
Future<void> removeMod(String id, String slug) async {
final base = await baseUrl;
final res = await http.delete(Uri.parse('$base/api/servers/$id/mods/$slug'), headers: await _headers());
if (res.statusCode == 401) throw UnauthorizedException();
if (res.statusCode == 403) throw ApiException('Réservé aux administrateurs');
if (res.statusCode >= 400) throw ApiException(_detail(res) ?? 'Retrait refusé (${res.statusCode})');
}
String? _detail(http.Response res) {
try {
return jsonDecode(res.body)['detail']?.toString();
} catch (_) {
return null;
}
}
}
class UnauthorizedException implements Exception {}
class ApiException implements Exception {
ApiException(this.message);
final String message;
@override
String toString() => message;
}