2022-01-16 15:28:42 +01:00
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-12-13 13:39:06 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-01-19 19:30:09 +01:00
|
|
|
import 'package:http/http.dart' as http;
|
2021-12-13 13:39:06 +01:00
|
|
|
|
2021-12-19 22:20:56 +01:00
|
|
|
Future<String> getCachedTimetable(String ext, String? presetClassNum) async {
|
2021-12-13 13:39:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
String classNum;
|
2021-12-19 22:20:56 +01:00
|
|
|
if (presetClassNum != null) {
|
|
|
|
classNum = presetClassNum.replaceAll("/", "_");
|
|
|
|
} else if (prefs.getString('class_num') != null) {
|
2021-12-13 13:39:06 +01:00
|
|
|
classNum = prefs.getString('class_num')!.replaceAll("/", "_");
|
|
|
|
} else {
|
|
|
|
classNum = '05_1';
|
|
|
|
}
|
|
|
|
var apiKey = prefs.getString('api_key');
|
|
|
|
var headers = {"x-api-key": "$apiKey"};
|
2022-01-19 19:30:09 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2021-12-13 13:39:06 +01:00
|
|
|
}
|