part of '../app/app.dart'; class ApiClient { ApiClient({required this.baseUrl, this.token}); final String baseUrl; final String? token; Uri _uri(String path, [Map? query]) { final normalizedBase = normalizeBaseUrl(baseUrl); final base = Uri.parse(normalizedBase); return base.replace( path: '${base.path.replaceAll(RegExp(r'/$'), '')}/$path', queryParameters: query, ); } Map get _headers => { 'Accept': 'application/json', 'Content-Type': 'application/json', if (token != null) 'Authorization': 'Bearer $token', }; Future> post( String path, Map body, ) async { final response = await http.post( _uri(path), headers: _headers, body: jsonEncode(body), ); return _decode(response); } Future> get( String path, [ Map? query, ]) async { final response = await http.get(_uri(path, query), headers: _headers); return _decode(response); } Map _decode(http.Response response) { final decoded = response.body.isEmpty ? {} : jsonDecode(response.body); if (response.statusCode >= 200 && response.statusCode < 300) { return decoded is Map ? decoded : {'data': decoded}; } final message = decoded is Map ? decoded['message'] : null; throw ApiException(message?.toString() ?? 'Koneksi ke server gagal.'); } } class ApiException implements Exception { ApiException(this.message); final String message; }