fetchTimetables function

Future<List> fetchTimetables(
  1. String regionCode,
  2. String school,
  3. SchoolType schoolType,
  4. String key,
  5. int grade,
  6. int classNumber,
  7. DateTime dateTime,
)

Implementation

Future<List> fetchTimetables(
    String regionCode,
    String school,
    SchoolType schoolType,
    String key,
    int grade,
    int classNumber,
    DateTime dateTime) async {
  final DateTime first =
      dateTime.add(Duration(days: DateTime.monday - dateTime.weekday));
  final DateTime last = first.add(const Duration(days: 4));
  final response = await http.get(Uri.parse(
      "https://open.neis.go.kr/hub/${describeEnum(schoolType)}Timetable?key=$key&type=json&ATPT_OFCDC_SC_CODE=$regionCode&SD_SCHUL_CODE=$school&AY=${first.year}&GRADE=$grade&CLASS_NM=$classNumber&TI_FROM_YMD=${DateFormat('yyyyMMdd').format(first)}&TI_TO_YMD=${DateFormat('yyyyMMdd').format(last)}"));

  if (response.statusCode == 200) {
    var result = json.decode(response.body);
    if (result['RESULT'] == null) {
      return parseSchedules(response, schoolType, first.day);
    } else {
      throw Exception();
    }
  } else {
    throw Exception();
  }
}