// GCG.MeinCantor - Die Schulplattform für Cantorianer. // Copyright (C) 2021-2022 Georg-Cantor-Gymnasium Halle (Saale) // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import 'dart:convert'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; Future getCachedTimetable(String ext, String? presetClassNum) async { SharedPreferences prefs = await SharedPreferences.getInstance(); String classNum; if (presetClassNum != null) { classNum = presetClassNum.replaceAll("/", "_"); } else if (prefs.getString('class_num') != null) { classNum = prefs.getString('class_num')!.replaceAll("/", "_"); } else { classNum = '05_1'; } var apiKey = prefs.getString('api_key'); var headers = {"x-api-key": "$apiKey"}; var uri = Uri.https("mein.cantorgymnasium.de", "/api/timetable/$ext/$classNum"); final response = await http.get(uri, headers: headers).onError((error, stackTrace) { return http.Response("", 404); }); if (response.statusCode == 404) { var file = await DefaultCacheManager().getSingleFile( "https://mein.cantorgymnasium.de/api/timetable/$ext/$classNum", headers: headers); return (utf8.decode(await file.readAsBytes())); } else { DefaultCacheManager().putFile( "https://mein.cantorgymnasium.de/api/timetable/$ext/$classNum", response.bodyBytes); return (utf8.decode(response.bodyBytes)); } }