Add InvalidSymbolException to hebe

This commit is contained in:
Mikołaj Pich 2023-05-05 00:14:37 +02:00
parent 8726501d8b
commit 62d5ff05ef
3 changed files with 31 additions and 15 deletions

View file

@ -0,0 +1,5 @@
package io.github.wulkanowy.sdk.hebe.exception
import java.io.IOException
class InvalidSymbolException : IOException()

View file

@ -2,11 +2,14 @@ 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.exception.InvalidSymbolException
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
import io.github.wulkanowy.sdk.hebe.service.RegisterService
import retrofit2.HttpException
import java.net.HttpURLConnection.HTTP_INTERNAL_ERROR
internal class RegisterRepository(private val service: RegisterService) {
@ -33,7 +36,8 @@ internal class RegisterRepository(private val service: RegisterService) {
firebaseToken: String,
pin: String,
token: String,
): ApiResponse<RegisterResponse> = service.registerDevice(
): ApiResponse<RegisterResponse> = runCatching {
service.registerDevice(
ApiRequest(
certificateId = certificateId,
firebaseToken = firebaseToken,
@ -46,6 +50,13 @@ internal class RegisterRepository(private val service: RegisterService) {
),
),
)
}.onFailure {
if (it is HttpException && it.code() == HTTP_INTERNAL_ERROR) {
if ("ArgumentException" in it.response()?.errorBody()?.string().orEmpty()) {
throw InvalidSymbolException()
}
}
}.getOrThrow()
suspend fun getStudentInfo(): List<StudentInfo> {
return service.getStudentsInfo().envelope!!

View file

@ -32,9 +32,9 @@ class HebeRemoteTest {
fun `register device`() = runTest {
val res = hebe.register(
firebaseToken = "",
token = "3S1JDDR",
pin = "212343",
symbol = "gminanowosolna",
token = "FK11234",
pin = "123456",
symbol = "powiatwulkanowy",
)
assertTrue(res.privatePem.isNotEmpty())
}