Skip to content

Commit

Permalink
Version bump + made methods public
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfromjokela committed Sep 18, 2022
1 parent 0827792 commit 06e14e0
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 24 deletions.
4 changes: 1 addition & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ repositories {
mavenCentral()
}



publishing {
publications {
register("mavenJava", MavenPublication::class) {
from(components["java"])
groupId = "org.openwilma"
artifactId = "kotlin"
version = "0.9.3-BETA"
version = "0.9.4-BETA"
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/org/openwilma/kotlin/methods/Announcements.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.openwilma.kotlin.methods

import com.google.gson.reflect.TypeToken
import okhttp3.Response
import org.openwilma.kotlin.OpenWilma
import org.openwilma.kotlin.classes.WilmaSession
import org.openwilma.kotlin.classes.announcements.Announcement
import org.openwilma.kotlin.classes.errors.Error
import org.openwilma.kotlin.classes.exams.Exam
import org.openwilma.kotlin.classes.responses.JSONErrorResponse
import org.openwilma.kotlin.clients.WilmaHttpClient
import org.openwilma.kotlin.parsers.WilmaAnnouncementsParser
import org.openwilma.kotlin.parsers.WilmaExamsParser
import org.openwilma.kotlin.parsers.WilmaJSONParser
import org.openwilma.kotlin.utils.JSONUtils
import org.openwilma.kotlin.utils.URLUtils
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

public suspend fun OpenWilma.getAnnouncements(wilmaSession: WilmaSession): List<Announcement> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "news?printable&format=json"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
if (JSONUtils.isJSONValid(response)) {
val error: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (error.wilmaError != null) {
it.resumeWithException(error.wilmaError!!)
return
}
}
it.resume(WilmaAnnouncementsParser.parseAnnouncements(response))
}

override fun onRawResponse(response: Response) {}

override fun onFailed(error: Error) {
it.resumeWithException(error)
}
})
}
}

public suspend fun OpenWilma.getAnnouncement(wilmaSession: WilmaSession, id: Int): Announcement? {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "news/$id?printable&format=json"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
if (JSONUtils.isJSONValid(response)) {
val error: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (error.wilmaError != null) {
it.resumeWithException(error.wilmaError!!)
return
}
}
it.resume(WilmaAnnouncementsParser.parseAnnouncement(response))
}

override fun onRawResponse(response: Response) {}

override fun onFailed(error: Error) {
it.resumeWithException(error)
}
})
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/openwilma/kotlin/methods/Authentication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getSessionId(wilmaServer: WilmaServer, skipVersionValidation: Boolean = false): SessionResponse {
public suspend fun OpenWilma.getSessionId(wilmaServer: WilmaServer, skipVersionValidation: Boolean = false): SessionResponse {
return suspendCoroutine {
val httpClient = WilmaHttpClient()
httpClient.getRequest(URLUtils.buildUrl(wilmaServer, "index_json"), object : WilmaHttpClient.HttpClientInterface {
Expand All @@ -43,7 +43,7 @@ suspend fun OpenWilma.getSessionId(wilmaServer: WilmaServer, skipVersionValidati
}
}

suspend fun OpenWilma.signIn(wilmaServer: WilmaServer, username: String, password: String, skipVersionValidation: Boolean = false): WilmaSession {
public suspend fun OpenWilma.signIn(wilmaServer: WilmaServer, username: String, password: String, skipVersionValidation: Boolean = false): WilmaSession {
val sessionId = getSessionId(wilmaServer, skipVersionValidation = skipVersionValidation)
val cookie: String = suspendCoroutine {
val httpClient = WilmaHttpClient(false)
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/openwilma/kotlin/methods/Exams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getUpcomingExams(wilmaSession: WilmaSession,): List<Exam> {
public suspend fun OpenWilma.getUpcomingExams(wilmaSession: WilmaSession): List<Exam> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "exams/calendar?format=json"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
if (JSONUtils.isJSONValid(response)) {
val schedule: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (schedule.wilmaError != null) {
it.resumeWithException(schedule.wilmaError!!)
val error: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (error.wilmaError != null) {
it.resumeWithException(error.wilmaError!!)
return
}
}
Expand All @@ -43,7 +43,7 @@ suspend fun OpenWilma.getUpcomingExams(wilmaSession: WilmaSession,): List<Exam>
}
}

suspend fun OpenWilma.getPastExams(wilmaSession: WilmaSession, start: LocalDate? = null, end: LocalDate? = null): List<Exam> {
public suspend fun OpenWilma.getPastExams(wilmaSession: WilmaSession, start: LocalDate? = null, end: LocalDate? = null): List<Exam> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
val dateFormat = DateTimeFormatter.ofPattern("d.M.yyyy", Locale.getDefault())
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/openwilma/kotlin/methods/LessonNotes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getLessonNotes(wilmaSession: WilmaSession, dateRange: LessonNoteRange = LessonNoteRange.DEFAULT, start: LocalDate? = null, end: LocalDate? = null): List<LessonNote> {
public suspend fun OpenWilma.getLessonNotes(wilmaSession: WilmaSession, dateRange: LessonNoteRange = LessonNoteRange.DEFAULT, start: LocalDate? = null, end: LocalDate? = null): List<LessonNote> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
val dateFormat = DateTimeFormatter.ofPattern("d.M.yyyy", Locale.getDefault())
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "attendance/view?range=${dateRange.identifier}"+(if (start != null) "&first=${start.format(dateFormat)}" else "")+(if (end != null) "&last=${end.format(dateFormat)}" else "")+"&printable&format=json"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
if (JSONUtils.isJSONValid(response)) {
val schedule: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (schedule.wilmaError != null) {
it.resumeWithException(schedule.wilmaError!!)
val error: JSONErrorResponse = WilmaJSONParser.gson.fromJson(response, object: TypeToken<JSONErrorResponse>() {}.type)
if (error.wilmaError != null) {
it.resumeWithException(error.wilmaError!!)
return
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/openwilma/kotlin/methods/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getUserAccount(wilmaSession: WilmaSession): WilmaAPIResponse<WilmaAccountInfo> {
public suspend fun OpenWilma.getUserAccount(wilmaSession: WilmaSession): WilmaAPIResponse<WilmaAccountInfo> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession.wilmaServer, "api/v1/accounts/me"), object : WilmaHttpClient.HttpClientInterface {
Expand All @@ -35,7 +35,7 @@ suspend fun OpenWilma.getUserAccount(wilmaSession: WilmaSession): WilmaAPIRespon
}


suspend fun OpenWilma.getRoles(wilmaSession: WilmaSession): WilmaAPIResponse<List<WilmaRole>> {
public suspend fun OpenWilma.getRoles(wilmaSession: WilmaSession): WilmaAPIResponse<List<WilmaRole>> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/accounts/me/roles", requireRole = false), object : WilmaHttpClient.HttpClientInterface {
Expand All @@ -52,11 +52,11 @@ suspend fun OpenWilma.getRoles(wilmaSession: WilmaSession): WilmaAPIResponse<Lis
}
}

suspend fun OpenWilma.getActiveUserFormKey(wilmaSession: WilmaSession, roleRequired: Boolean = false): String? {
public suspend fun OpenWilma.getActiveUserFormKey(wilmaSession: WilmaSession, roleRequired: Boolean = false): String? {
return getActiveRole(wilmaSession, roleRequired)?.formKey
}

suspend fun OpenWilma.getActiveRole(wilmaSession: WilmaSession, roleRequired: Boolean = false): WilmaRole? {
public suspend fun OpenWilma.getActiveRole(wilmaSession: WilmaSession, roleRequired: Boolean = false): WilmaRole? {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/accounts/me/roles", requireRole = roleRequired), object : WilmaHttpClient.HttpClientInterface {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/openwilma/kotlin/methods/Schedule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getSchedule(wilmaSession: WilmaSession, date: LocalDate = LocalDate.now()): WilmaSchedule {
public suspend fun OpenWilma.getSchedule(wilmaSession: WilmaSession, date: LocalDate = LocalDate.now()): WilmaSchedule {
// Role required for user type
val currentRole: WilmaRole = wilmaSession.getRole() ?: getActiveRole(wilmaSession, false)!!
return suspendCoroutine {
Expand All @@ -51,7 +51,7 @@ suspend fun OpenWilma.getSchedule(wilmaSession: WilmaSession, date: LocalDate =
}
}

suspend fun OpenWilma.getScheduleRange(wilmaSession: WilmaSession, start: LocalDate, end: LocalDate): WilmaSchedule {
public suspend fun OpenWilma.getScheduleRange(wilmaSession: WilmaSession, start: LocalDate, end: LocalDate): WilmaSchedule {
val days: MutableList<ScheduleDay> = mutableListOf()
val terms: MutableList<Term> = mutableListOf()
for (week in DateUtils.splitWeeksFromRange(start, end, WeekFields.ISO)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openwilma/kotlin/methods/WilmaServers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun OpenWilma.getWilmaServers(): WilmaServersResponse {
public suspend fun OpenWilma.getWilmaServers(): WilmaServersResponse {
return suspendCoroutine {
val httpClient = WilmaHttpClient()
httpClient.getRequest(Config.wilmaServersURL, object : WilmaHttpClient.HttpClientInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class WilmaAnnouncementsParser {
return announcements
}

fun parseAnnouncement(htmlDocument: String): Announcement {
fun parseAnnouncement(htmlDocument: String): Announcement? {
val jsoupDocument = Jsoup.parse(htmlDocument)
val card = jsoupDocument.getElementsByClass("panel-body").first()!!
val newsId = jsoupDocument.getElementsByAttributeValue("target", "preview").first()?.attr("href")?.split("/")?.last()?.trim()?.toInt()!!
val card = jsoupDocument.getElementsByClass("panel-body").first() ?: return null
val newsId = jsoupDocument.getElementsByAttributeValue("target", "preview").first()?.attr("href")?.split("/")?.last()?.trim()?.toInt() ?: return null
val newsContent = card.getElementById("news-content")?.html()
val title = card.getElementsByTag("h2").first()!!.text()
val createDateElement = card.getElementsByClass("small semi-bold").first()
Expand Down

0 comments on commit 06e14e0

Please sign in to comment.