- É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
This commit is contained in:
@@ -145,6 +145,63 @@ class ApiService {
|
||||
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 {}
|
||||
|
||||
Reference in New Issue
Block a user