From fa22cf2b85c6b5b6a56400e6f1e3be0f976e3449 Mon Sep 17 00:00:00 2001 From: Developer From Jokela Date: Sat, 22 Oct 2022 19:25:38 +0300 Subject: [PATCH] add: API function to fetch additional information about course --- build.gradle.kts | 2 +- .../java/org/openwilma/kotlin/OpenWilma.kt | 15 +++--- .../kotlin/classes/courses/WilmaCourse.kt | 4 +- .../kotlin/classes/courses/WilmaCourseInfo.kt | 9 ++++ .../classes/courses/WilmaCourseScope.kt | 9 ++++ .../org/openwilma/kotlin/methods/Courses.kt | 46 +++++++++++++++---- .../openwilma/kotlin/methods/CoursesKtTest.kt | 3 +- 7 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseInfo.kt create mode 100644 src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseScope.kt diff --git a/build.gradle.kts b/build.gradle.kts index b3ec953..3c72b1d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,7 +19,7 @@ publishing { from(components["java"]) groupId = "org.openwilma" artifactId = "kotlin" - version = "0.9.9-ALPHA" + version = "0.9.9-BETA" } } repositories { diff --git a/src/main/java/org/openwilma/kotlin/OpenWilma.kt b/src/main/java/org/openwilma/kotlin/OpenWilma.kt index 926c3ee..361e691 100644 --- a/src/main/java/org/openwilma/kotlin/OpenWilma.kt +++ b/src/main/java/org/openwilma/kotlin/OpenWilma.kt @@ -3,10 +3,7 @@ package org.openwilma.kotlin import org.openwilma.kotlin.classes.WilmaServer import org.openwilma.kotlin.classes.WilmaSession import org.openwilma.kotlin.classes.announcements.Announcement -import org.openwilma.kotlin.classes.courses.WilmaCourse -import org.openwilma.kotlin.classes.courses.WilmaCourseExam -import org.openwilma.kotlin.classes.courses.WilmaCourseUser -import org.openwilma.kotlin.classes.courses.WilmaHomework +import org.openwilma.kotlin.classes.courses.* import org.openwilma.kotlin.classes.exams.Exam import org.openwilma.kotlin.classes.lessonnotes.LessonNote import org.openwilma.kotlin.classes.responses.SessionResponse @@ -26,8 +23,8 @@ public class OpenWilma { var wilmaSession: WilmaSession = WilmaSession() companion object { - const val version = 9 - const val versionName = "0.9.9-alpha" + const val version = 10 + const val versionName = "0.9.9-beta" const val minimumSupportedWilmaVersion = 19 const val lessonNoteFullHourWidth = 5.63 var checkSessionErrors = true @@ -60,8 +57,9 @@ public class OpenWilma { // Courses - suspend fun courses(wilmaSession: WilmaSession, timeRange: CourseTimeRange): WilmaAPIResponse> = getCourses(wilmaSession, timeRange) + suspend fun courses(wilmaSession: WilmaSession, timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse> = getCourses(wilmaSession, timeRange, skipAdditionalInformation = skipAdditionalInformation) suspend fun course(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse = getCourse(wilmaSession, id) + suspend fun courseAdditionalInfo(wilmaSession: WilmaSession, courseId: Int): WilmaAPIResponse = getCourseAdditionalInformation(wilmaSession, courseId) suspend fun courseExams(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse> = getCourseExams(wilmaSession, id) suspend fun courseHomework(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse> = getCourseHomework(wilmaSession, id) suspend fun courseStudents(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse> = getCourseStudents(wilmaSession, id) @@ -85,8 +83,9 @@ public class OpenWilma { suspend fun activeRole(roleRequired: Boolean = false): WilmaRole? = getActiveRole(wilmaSession, roleRequired) suspend fun schedule(date: LocalDate = LocalDate.now()): WilmaSchedule = getSchedule(wilmaSession, date) suspend fun scheduleRange(start: LocalDate, end: LocalDate): WilmaSchedule = getScheduleRange(wilmaSession, start, end) - suspend fun courses(timeRange: CourseTimeRange): WilmaAPIResponse> = getCourses(wilmaSession, timeRange) + suspend fun courses(timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse> = getCourses(wilmaSession, timeRange, skipAdditionalInformation = skipAdditionalInformation) suspend fun course(id: Int): WilmaAPIResponse = getCourse(wilmaSession, id) + suspend fun courseAdditionalInfo(courseId: Int): WilmaAPIResponse = getCourseAdditionalInformation(wilmaSession, courseId) suspend fun courseExams(id: Int): WilmaAPIResponse> = getCourseExams(wilmaSession, id) suspend fun courseHomework(id: Int): WilmaAPIResponse> = getCourseHomework(wilmaSession, id) suspend fun courseStudents(id: Int): WilmaAPIResponse> = getCourseStudents(wilmaSession, id) diff --git a/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourse.kt b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourse.kt index b33266e..49c8944 100644 --- a/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourse.kt +++ b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourse.kt @@ -1,6 +1,5 @@ package org.openwilma.kotlin.classes.courses -import java.time.LocalDate import java.util.* data class WilmaCourse ( @@ -11,5 +10,6 @@ data class WilmaCourse ( val startDate: Date?, val endDate: Date?, val committed: Boolean, - val teachers: List? + val teachers: List?, + var additionalInfo: WilmaCourseInfo? ) \ No newline at end of file diff --git a/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseInfo.kt b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseInfo.kt new file mode 100644 index 0000000..f65bf54 --- /dev/null +++ b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseInfo.kt @@ -0,0 +1,9 @@ +package org.openwilma.kotlin.classes.courses + +data class WilmaCourseInfo( + val id: Int, + val caption: String, + val description: String?, + val abbreviation: String?, + val scope: WilmaCourseScope? +) \ No newline at end of file diff --git a/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseScope.kt b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseScope.kt new file mode 100644 index 0000000..1b9dbab --- /dev/null +++ b/src/main/java/org/openwilma/kotlin/classes/courses/WilmaCourseScope.kt @@ -0,0 +1,9 @@ +package org.openwilma.kotlin.classes.courses + +data class WilmaCourseScope( + val credits: Double?, + val ecvetPoints: Double?, + val creditUnits: Double?, + val lessons: Double?, + val reiCredits: Double? +) \ No newline at end of file diff --git a/src/main/java/org/openwilma/kotlin/methods/Courses.kt b/src/main/java/org/openwilma/kotlin/methods/Courses.kt index d5c818a..454de82 100644 --- a/src/main/java/org/openwilma/kotlin/methods/Courses.kt +++ b/src/main/java/org/openwilma/kotlin/methods/Courses.kt @@ -3,14 +3,9 @@ package org.openwilma.kotlin.methods import com.google.gson.reflect.TypeToken import okhttp3.Response import org.openwilma.kotlin.classes.WilmaSession -import org.openwilma.kotlin.classes.courses.WilmaCourse -import org.openwilma.kotlin.classes.courses.WilmaCourseExam -import org.openwilma.kotlin.classes.courses.WilmaCourseUser -import org.openwilma.kotlin.classes.courses.WilmaHomework +import org.openwilma.kotlin.classes.courses.* import org.openwilma.kotlin.classes.errors.Error -import org.openwilma.kotlin.classes.exams.Exam import org.openwilma.kotlin.classes.responses.WilmaAPIResponse -import org.openwilma.kotlin.classes.user.WilmaAccountInfo import org.openwilma.kotlin.clients.WilmaHttpClient import org.openwilma.kotlin.enums.CourseTimeRange import org.openwilma.kotlin.parsers.WilmaJSONParser @@ -19,8 +14,8 @@ import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine -public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeRange): WilmaAPIResponse> { - return suspendCoroutine { +public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse> { + val courses: WilmaAPIResponse> = suspendCoroutine { val httpClient = WilmaHttpClient(wilmaSession) httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/gradebooks/$timeRange"), object : WilmaHttpClient.HttpClientInterface { override fun onResponse(response: String, status: Int) { @@ -29,6 +24,34 @@ public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeR override fun onRawResponse(response: Response) {} + override fun onFailed(error: Error) { + it.resumeWithException(error) + } + }) + } + if (!skipAdditionalInformation) { + val coursesWithAdditionalInfo: List? = courses.payload?.map { course -> + course.courseId?.let { + val info = getCourseAdditionalInformation(wilmaSession, it); + course.additionalInfo = info.payload + } + course + } + courses.payload = coursesWithAdditionalInfo + } + return courses +} + +public suspend fun getCourseAdditionalInformation(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse { + return suspendCoroutine { + val httpClient = WilmaHttpClient(wilmaSession) + httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/courses/$id"), object : WilmaHttpClient.HttpClientInterface { + override fun onResponse(response: String, status: Int) { + it.resume(WilmaJSONParser.gson.fromJson(response, object: TypeToken>() {}.type)) + } + + override fun onRawResponse(response: Response) {} + override fun onFailed(error: Error) { it.resumeWithException(error) } @@ -38,7 +61,7 @@ public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeR public suspend fun getCourse(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse { - return suspendCoroutine { + val course: WilmaAPIResponse = suspendCoroutine { val httpClient = WilmaHttpClient(wilmaSession) httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/gradebooks/$id"), object : WilmaHttpClient.HttpClientInterface { override fun onResponse(response: String, status: Int) { @@ -52,6 +75,11 @@ public suspend fun getCourse(wilmaSession: WilmaSession, id: Int): WilmaAPIRespo } }) } + course.payload?.courseId?.let { + val info = getCourseAdditionalInformation(wilmaSession, it); + course.payload?.additionalInfo = info.payload + } + return course } public suspend fun getCourseExams(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse> { diff --git a/src/test/kotlin/org/openwilma/kotlin/methods/CoursesKtTest.kt b/src/test/kotlin/org/openwilma/kotlin/methods/CoursesKtTest.kt index 7aaec7d..e9467f4 100644 --- a/src/test/kotlin/org/openwilma/kotlin/methods/CoursesKtTest.kt +++ b/src/test/kotlin/org/openwilma/kotlin/methods/CoursesKtTest.kt @@ -29,6 +29,7 @@ class CoursesKtTest { @Test fun testCourses() = runBlocking { val courses = client.courses(timeRange = CourseTimeRange.CURRENT) + val coursesWithExtraDetails = client.courses(timeRange = CourseTimeRange.CURRENT, skipAdditionalInformation = false) val coursesPast = client.courses(timeRange = CourseTimeRange.PAST) val course = client.course(21128) val courseExams = client.courseExams(21128) @@ -37,6 +38,6 @@ class CoursesKtTest { println(GsonBuilder() .registerTypeAdapter(LocalDate::class.java, LocalDateGSONAdapter()) .registerTypeAdapter(LocalTime::class.java, LocalTimeGSONAdapter()) - .registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeGSONAdapter()).create().toJson(listOf(coursesPast, courses, course, courseExams, courseHomework, courseStudents))) + .registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeGSONAdapter()).create().toJson(listOf(coursesPast, coursesWithExtraDetails, courses, course, courseExams, courseHomework, courseStudents))) } } \ No newline at end of file