Fix initialize pupil id, add generic method to handle api errors

This commit is contained in:
Mikołaj Pich 2023-04-16 23:26:26 +02:00
parent 51dd70067b
commit 4ef106e4c9
5 changed files with 25 additions and 22 deletions

View file

@ -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
}

View file

@ -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,

View file

@ -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()
}
}

View file

@ -42,6 +42,5 @@ class HebeRemoteTest {
fun `get grades`() = runTest {
val grades = hebe.getGrades(559)
assertTrue(grades.isNotEmpty())
println(grades[0])
}
}

View file

@ -152,7 +152,7 @@ class Sdk {
set(value) {
field = value
scrapper.studentId = value
// hebe.studentId = value // todo
hebe.pupilId = value
}
var diaryId = 0