Fix initialize pupil id, add generic method to handle api errors
This commit is contained in:
parent
51dd70067b
commit
4ef106e4c9
5 changed files with 25 additions and 22 deletions
|
@ -0,0 +1,12 @@
|
|||
package io.github.wulkanowy.sdk.hebe
|
||||
|
||||
internal fun <T> ApiResponse<T>.getEnvelopeOrThrowError(): T? {
|
||||
if (envelope == null) {
|
||||
when (status.code) {
|
||||
// 100 -> "Użytkownik nie jest uprawniony do przeglądania żądanych danych"
|
||||
// todo: add more codes
|
||||
else -> error("Unknown error: ${status.message}")
|
||||
}
|
||||
}
|
||||
return envelope
|
||||
}
|
|
@ -2,6 +2,7 @@ package io.github.wulkanowy.sdk.hebe.repository
|
|||
|
||||
import io.github.wulkanowy.sdk.hebe.ApiRequest
|
||||
import io.github.wulkanowy.sdk.hebe.ApiResponse
|
||||
import io.github.wulkanowy.sdk.hebe.getEnvelopeOrThrowError
|
||||
import io.github.wulkanowy.sdk.hebe.register.RegisterRequest
|
||||
import io.github.wulkanowy.sdk.hebe.register.RegisterResponse
|
||||
import io.github.wulkanowy.sdk.hebe.register.StudentInfo
|
||||
|
@ -16,24 +17,14 @@ internal class RegisterRepository(private val service: RegisterService) {
|
|||
certificatePem: String,
|
||||
certificateId: String,
|
||||
firebaseToken: String?,
|
||||
): RegisterResponse {
|
||||
val response = registerDevice(
|
||||
privateKey = certificatePem,
|
||||
certificateId = certificateId,
|
||||
deviceModel = deviceModel,
|
||||
firebaseToken = firebaseToken.orEmpty(),
|
||||
pin = pin,
|
||||
token = token,
|
||||
)
|
||||
if (response.envelope == null) {
|
||||
when (response.status.code) {
|
||||
// todo: add more codes
|
||||
else -> error("Unknown error: ${response.status.message}")
|
||||
}
|
||||
}
|
||||
|
||||
return response.envelope!!
|
||||
}
|
||||
): RegisterResponse = registerDevice(
|
||||
privateKey = certificatePem,
|
||||
certificateId = certificateId,
|
||||
deviceModel = deviceModel,
|
||||
firebaseToken = firebaseToken.orEmpty(),
|
||||
pin = pin,
|
||||
token = token,
|
||||
).getEnvelopeOrThrowError()!!
|
||||
|
||||
private suspend fun registerDevice(
|
||||
privateKey: String,
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
package io.github.wulkanowy.sdk.hebe.repository
|
||||
|
||||
import io.github.wulkanowy.sdk.hebe.getEnvelopeOrThrowError
|
||||
import io.github.wulkanowy.sdk.hebe.models.Grade
|
||||
import io.github.wulkanowy.sdk.hebe.service.StudentService
|
||||
|
||||
internal class StudentRepository(private val studentService: StudentService) {
|
||||
|
||||
suspend fun getGrades(pupilId: Int, periodId: Int): List<Grade>? {
|
||||
suspend fun getGrades(pupilId: Int, periodId: Int): List<Grade> {
|
||||
return studentService.getGrades(
|
||||
pupilId = pupilId,
|
||||
periodId = periodId,
|
||||
).envelope
|
||||
).getEnvelopeOrThrowError().orEmpty()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,5 @@ class HebeRemoteTest {
|
|||
fun `get grades`() = runTest {
|
||||
val grades = hebe.getGrades(559)
|
||||
assertTrue(grades.isNotEmpty())
|
||||
println(grades[0])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -152,7 +152,7 @@ class Sdk {
|
|||
set(value) {
|
||||
field = value
|
||||
scrapper.studentId = value
|
||||
// hebe.studentId = value // todo
|
||||
hebe.pupilId = value
|
||||
}
|
||||
|
||||
var diaryId = 0
|
||||
|
|
Loading…
Reference in a new issue