getCourses method

Future<List<Course>> getCourses()

If User is logged in, it returns a list of User's registered courses, otherwise, it returns a list of the available courses in the platform (opencourses).

Implementation

Future<List<Course>> getCourses() async {
  List<Course> courses = [];
  Course course;
  final response = await _client.post(
    Uri.https(
      'eclass.$instituteId.gr',
      '/modules/mobile/mcourses.php',
    ),
    body: {
      'token': _token ?? '',
      'registered': '',
    },
  );
  if (response.statusCode != 200 ||
      response.body == 'EXPIRED' ||
      response.body == 'FAILED') {
    throw Exception("Failed to get user's registered courses.");
  }
  final decodedResponse = XmlDocument.parse(response.body);
  final nodes = decodedResponse.findAllElements('course');
  for (final node in nodes) {
    course = Course(
      code: node.attributes[0].value,
      title: node.attributes[1].value,
      description: node.attributes[2].value,
    );
    courses.add(course);
  }
  return courses;
}