Files
Client/lib/api/emergency/emergency_notice_client.dart
T

35 lines
1.2 KiB
Dart

import 'package:dio/dio.dart';
import 'emergency_notice.dart';
/// Loads the emergency notice from a foreign URL over a standalone [Dio]
/// instance — no MarianumConnect interceptors, base URL or auth. That keeps the
/// fallback fully independent of the backend it is meant to survive.
///
/// Fail-safe by contract: if the server is unreachable, times out, answers with
/// a non-2xx status or delivers unparsable content, [fetch] returns `null` and
/// nothing is shown. It never throws.
class EmergencyNoticeClient {
const EmergencyNoticeClient();
Future<EmergencyNotice?> fetch(String url) async {
try {
final dio = Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 5),
sendTimeout: const Duration(seconds: 5),
responseType: ResponseType.plain,
),
);
final response = await dio.get<String>(url);
final raw = response.data;
if (raw == null || raw.isEmpty) return null;
return EmergencyNotice.parse(raw);
} catch (_) {
// Server down / timeout / bad status / malformed body: show nothing.
return null;
}
}
}