adopt MarianumConnectQuery base in remaining queries
This commit is contained in:
+10
-20
@@ -1,35 +1,25 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../errors/marianumconnect_error.dart';
|
||||
import '../../marianumconnect_api.dart';
|
||||
import '../../marianumconnect_endpoint.dart';
|
||||
import '../../marianumconnect_query.dart';
|
||||
import '../timetable_get_week/timetable_get_week_response.dart';
|
||||
import 'timetable_element_type.dart';
|
||||
|
||||
/// Fetches a foreign element's weekly timetable from
|
||||
/// `timetable/{student|teacher|room|class}/{id}`. The response shape is
|
||||
/// identical to `timetable/me`, so [TimetableGetWeekResponse] is reused.
|
||||
class TimetableGetElementWeek {
|
||||
final Dio _dio;
|
||||
|
||||
TimetableGetElementWeek({Dio? dio}) : _dio = dio ?? MarianumConnectApi.dio();
|
||||
class TimetableGetElementWeek extends MarianumConnectQuery {
|
||||
TimetableGetElementWeek({super.dio});
|
||||
|
||||
Future<TimetableGetWeekResponse> run({
|
||||
required TimetableElementType type,
|
||||
required int id,
|
||||
required DateTime from,
|
||||
required DateTime until,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.get<Map<String, dynamic>>(
|
||||
MarianumConnectEndpoint.resolve('timetable/${type.pathSegment}/$id'),
|
||||
queryParameters: {'from': _format(from), 'until': _format(until)},
|
||||
);
|
||||
return TimetableGetWeekResponse.fromJson(response.data!);
|
||||
} on DioException catch (e) {
|
||||
throw mapMarianumConnectError(e);
|
||||
}
|
||||
}
|
||||
}) => guard(() async {
|
||||
final response = await dio.get<Map<String, dynamic>>(
|
||||
endpoint('timetable/${type.pathSegment}/$id'),
|
||||
queryParameters: {'from': _format(from), 'until': _format(until)},
|
||||
);
|
||||
return TimetableGetWeekResponse.fromJson(response.data!);
|
||||
});
|
||||
|
||||
String _format(DateTime d) =>
|
||||
'${d.year.toString().padLeft(4, '0')}-${d.month.toString().padLeft(2, '0')}-${d.day.toString().padLeft(2, '0')}';
|
||||
|
||||
Reference in New Issue
Block a user