Skip to content

Commit

Permalink
add: API function to fetch additional information about course
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfromjokela committed Oct 22, 2022
1 parent 92604bc commit fa22cf2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ publishing {
from(components["java"])
groupId = "org.openwilma"
artifactId = "kotlin"
version = "0.9.9-ALPHA"
version = "0.9.9-BETA"
}
}
repositories {
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/openwilma/kotlin/OpenWilma.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -60,8 +57,9 @@ public class OpenWilma {

// Courses

suspend fun courses(wilmaSession: WilmaSession, timeRange: CourseTimeRange): WilmaAPIResponse<List<WilmaCourse>> = getCourses(wilmaSession, timeRange)
suspend fun courses(wilmaSession: WilmaSession, timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse<List<WilmaCourse>> = getCourses(wilmaSession, timeRange, skipAdditionalInformation = skipAdditionalInformation)
suspend fun course(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse<WilmaCourse> = getCourse(wilmaSession, id)
suspend fun courseAdditionalInfo(wilmaSession: WilmaSession, courseId: Int): WilmaAPIResponse<WilmaCourseInfo> = getCourseAdditionalInformation(wilmaSession, courseId)
suspend fun courseExams(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse<List<WilmaCourseExam>> = getCourseExams(wilmaSession, id)
suspend fun courseHomework(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse<List<WilmaHomework>> = getCourseHomework(wilmaSession, id)
suspend fun courseStudents(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse<List<WilmaCourseUser>> = getCourseStudents(wilmaSession, id)
Expand All @@ -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<List<WilmaCourse>> = getCourses(wilmaSession, timeRange)
suspend fun courses(timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse<List<WilmaCourse>> = getCourses(wilmaSession, timeRange, skipAdditionalInformation = skipAdditionalInformation)
suspend fun course(id: Int): WilmaAPIResponse<WilmaCourse> = getCourse(wilmaSession, id)
suspend fun courseAdditionalInfo(courseId: Int): WilmaAPIResponse<WilmaCourseInfo> = getCourseAdditionalInformation(wilmaSession, courseId)
suspend fun courseExams(id: Int): WilmaAPIResponse<List<WilmaCourseExam>> = getCourseExams(wilmaSession, id)
suspend fun courseHomework(id: Int): WilmaAPIResponse<List<WilmaHomework>> = getCourseHomework(wilmaSession, id)
suspend fun courseStudents(id: Int): WilmaAPIResponse<List<WilmaCourseUser>> = getCourseStudents(wilmaSession, id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openwilma.kotlin.classes.courses

import java.time.LocalDate
import java.util.*

data class WilmaCourse (
Expand All @@ -11,5 +10,6 @@ data class WilmaCourse (
val startDate: Date?,
val endDate: Date?,
val committed: Boolean,
val teachers: List<WilmaCourseUser>?
val teachers: List<WilmaCourseUser>?,
var additionalInfo: WilmaCourseInfo?
)
Original file line number Diff line number Diff line change
@@ -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?
)
Original file line number Diff line number Diff line change
@@ -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?
)
46 changes: 37 additions & 9 deletions src/main/java/org/openwilma/kotlin/methods/Courses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<List<WilmaCourse>> {
return suspendCoroutine {
public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeRange, skipAdditionalInformation: Boolean = true): WilmaAPIResponse<List<WilmaCourse>> {
val courses: WilmaAPIResponse<List<WilmaCourse>> = suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/gradebooks/$timeRange"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
Expand All @@ -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<WilmaCourse>? = 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<WilmaCourseInfo> {
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<WilmaAPIResponse<WilmaCourseInfo>>() {}.type))
}

override fun onRawResponse(response: Response) {}

override fun onFailed(error: Error) {
it.resumeWithException(error)
}
Expand All @@ -38,7 +61,7 @@ public suspend fun getCourses(wilmaSession: WilmaSession, timeRange: CourseTimeR


public suspend fun getCourse(wilmaSession: WilmaSession, id: Int): WilmaAPIResponse<WilmaCourse> {
return suspendCoroutine {
val course: WilmaAPIResponse<WilmaCourse> = suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/gradebooks/$id"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
Expand All @@ -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<List<WilmaCourseExam>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
}
}

0 comments on commit fa22cf2

Please sign in to comment.