From 62d5ff05ef4975fa53626a916c8cfb2dabd68f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Fri, 5 May 2023 00:14:37 +0200 Subject: [PATCH] Add InvalidSymbolException to hebe --- .../hebe/exception/InvalidSymbolException.kt | 5 +++ .../sdk/hebe/repository/RegisterRepository.kt | 35 ++++++++++++------- .../wulkanowy/sdk/hebe/HebeRemoteTest.kt | 6 ++-- 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/exception/InvalidSymbolException.kt diff --git a/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/exception/InvalidSymbolException.kt b/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/exception/InvalidSymbolException.kt new file mode 100644 index 00000000..a1139158 --- /dev/null +++ b/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/exception/InvalidSymbolException.kt @@ -0,0 +1,5 @@ +package io.github.wulkanowy.sdk.hebe.exception + +import java.io.IOException + +class InvalidSymbolException : IOException() diff --git a/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/repository/RegisterRepository.kt b/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/repository/RegisterRepository.kt index bed05303..75773bbc 100644 --- a/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/repository/RegisterRepository.kt +++ b/sdk-hebe/src/main/kotlin/io/github/wulkanowy/sdk/hebe/repository/RegisterRepository.kt @@ -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,19 +36,27 @@ internal class RegisterRepository(private val service: RegisterService) { firebaseToken: String, pin: String, token: String, - ): ApiResponse = service.registerDevice( - ApiRequest( - certificateId = certificateId, - firebaseToken = firebaseToken, - envelope = RegisterRequest( - certificate = privateKey, - certificateThumbprint = certificateId, - deviceModel = deviceModel, - pin = pin, - securityToken = token, + ): ApiResponse = runCatching { + service.registerDevice( + ApiRequest( + certificateId = certificateId, + firebaseToken = firebaseToken, + envelope = RegisterRequest( + certificate = privateKey, + certificateThumbprint = certificateId, + deviceModel = deviceModel, + pin = pin, + securityToken = token, + ), ), - ), - ) + ) + }.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 { return service.getStudentsInfo().envelope!! diff --git a/sdk-hebe/src/test/kotlin/io/github/wulkanowy/sdk/hebe/HebeRemoteTest.kt b/sdk-hebe/src/test/kotlin/io/github/wulkanowy/sdk/hebe/HebeRemoteTest.kt index 65828e81..5bf7362e 100644 --- a/sdk-hebe/src/test/kotlin/io/github/wulkanowy/sdk/hebe/HebeRemoteTest.kt +++ b/sdk-hebe/src/test/kotlin/io/github/wulkanowy/sdk/hebe/HebeRemoteTest.kt @@ -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()) }