-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0827792
commit 06e14e0
Showing
9 changed files
with
89 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/openwilma/kotlin/methods/Announcements.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters