Authentication aufräumen, 3. Schritt Error Handling f. auth + HttpClient
This commit is contained in:
1 parent
9b573243ef
commit
e8bd21d108
4 files changed
+107
-16
No files matched your search
@@ -1,3 +1,3 @@
|
||||
#auto generated
|
||||
#Thu Feb 12 03:10:51 CET 2026
|
||||
build.number=238
|
||||
#Thu Feb 12 22:52:33 CET 2026
|
||||
build.number=251
|
||||
@@ -17,7 +17,7 @@ import androidx.compose.ui.tooling.preview.Preview
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -25,6 +25,9 @@ import net.keinekekse.app.auth.AuthResult
|
||||
import net.keinekekse.app.auth.Authenticator
|
||||
import net.keinekekse.app.books.generated.resources.Res
|
||||
import net.keinekekse.app.books.generated.resources.compose_multiplatform
|
||||
import net.keinekekse.app.books.model.Book
|
||||
import net.keinekekse.app.service.ApiResponse
|
||||
import net.keinekekse.app.service.safeRequest
|
||||
import org.jetbrains.compose.resources.painterResource
|
||||
import org.koin.compose.koinInject
|
||||
import org.koin.core.annotation.Single
|
||||
@@ -44,6 +47,9 @@ fun App() {
|
||||
val test = koinInject<TestAuth>()
|
||||
val authenticator = koinInject<Authenticator>()
|
||||
var showContent by remember { mutableStateOf(false) }
|
||||
// externer StateFlow
|
||||
val logonState by remember { authenticator.logonState }.collectAsState()
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.background(MaterialTheme.colorScheme.primaryContainer)
|
||||
@@ -74,8 +80,7 @@ fun App() {
|
||||
Text("Compose: $greeting")
|
||||
}
|
||||
}
|
||||
// refresh fkt. nicht!
|
||||
when (authenticator.logonState.value) {
|
||||
when (logonState) {
|
||||
is AuthResult.Success -> {
|
||||
Text("Anmeldung erfolgreich")
|
||||
}
|
||||
@@ -99,16 +104,45 @@ fun App() {
|
||||
class TestAuth(val httpClient: HttpClient) {
|
||||
|
||||
private val srv = "https://kas.keinekekse.net"
|
||||
|
||||
suspend fun request(url: String) {
|
||||
logger.debug { "connecting" }
|
||||
// val response = httpClient.get(srv + "testdb/books")
|
||||
logger.debug { "TestAuth: connecting $srv$url" }
|
||||
|
||||
val response: ApiResponse<List<Book>, Any> = httpClient.safeRequest {
|
||||
url(Url(srv + url))
|
||||
method = HttpMethod.Get
|
||||
}
|
||||
when (response) {
|
||||
is ApiResponse.Success -> {
|
||||
logger.debug { "TestAuth: success" }
|
||||
val books = response.body
|
||||
for (book in books) {
|
||||
logger.debug { book }
|
||||
}
|
||||
}
|
||||
|
||||
is ApiResponse.ErrorResponse -> {
|
||||
when (response) {
|
||||
is ApiResponse.ErrorResponse.HttpError -> logger.debug { "TestAuth: HTTP error ${response.code} - ${response.errorBody}" }
|
||||
is ApiResponse.ErrorResponse.NetworkError -> logger.debug { "TestAuth: Network error ${response.exception}" }
|
||||
is ApiResponse.ErrorResponse.SerializationError -> logger.debug { "TestAuth: Serialization error" }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// val books: List<Book> = response.body()
|
||||
|
||||
val response = httpClient.get(srv + url)
|
||||
val body = response.bodyAsText()
|
||||
// var response: HttpResponse? = null
|
||||
// var body = ""
|
||||
// try {
|
||||
// response = httpClient.get(srv + url)
|
||||
// body = response.bodyAsText()
|
||||
// } catch (e: Throwable) {
|
||||
// val error = e.toString()
|
||||
// val status = response?.status
|
||||
// val body = response?.bodyAsText() ?: ""
|
||||
// logger.error(e) { "TestAuth: error $error ($body) [$status]" }
|
||||
// }
|
||||
|
||||
logger.debug { "call to $url" }
|
||||
logger.debug { response.status }
|
||||
logger.debug { body }
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,13 @@ import net.keinekekse.version.Version
|
||||
|
||||
object VERSION: Version {
|
||||
override val version = "1.0.0"
|
||||
override val fullVersion = "1.0.0+build.238"
|
||||
override val fullVersion = "1.0.0+build.251"
|
||||
override val major = 1
|
||||
override val minor = 0
|
||||
override val patch = 0
|
||||
override val preRelease = ""
|
||||
override val buildMetadata = "build.238"
|
||||
override val buildDate = "2026-02-12T02:10:51.539449Z"
|
||||
override val buildMetadata = "build.251"
|
||||
override val buildDate = "2026-02-12T21:52:33.708217Z"
|
||||
override val displayName = "Books App"
|
||||
override val copyright = "Copyright (c) 2026 Hartmut Bauer"
|
||||
override val name = "books"
|
||||
|
||||
Reference in new issue
Block a user