diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 0af0d6f..d317c4b 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -1,4 +1,4 @@ -val ktorVersion = "2.3.2" +val ktorVersion = "3.0.3" object SqlDelight { const val runtime = "com.squareup.sqldelight:runtime:1.5.4" @@ -66,7 +66,6 @@ kotlin { val androidMain by getting { dependencies { implementation("com.squareup.okhttp3:okhttp:4.11.0") - implementation("io.ktor:ktor-client-okhttp:2.2.4") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") // Sql @@ -86,7 +85,6 @@ kotlin { dependencies { implementation(SqlDelight.native) - implementation("io.ktor:ktor-client-ios:2.1.1") } } val iosX64Test by getting diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/MercuryApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/MercuryApi.kt index 243659b..5fc678c 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/MercuryApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/MercuryApi.kt @@ -1,22 +1,26 @@ package bou.amine.apps.readerforselfossv2.rest -import bou.amine.apps.readerforselfossv2.model.* +import bou.amine.apps.readerforselfossv2.model.MercuryModel +import bou.amine.apps.readerforselfossv2.model.StatusAndData import io.github.aakira.napier.Napier -import io.ktor.client.* -import io.ktor.client.plugins.cache.* -import io.ktor.client.plugins.contentnegotiation.* -import io.ktor.client.plugins.logging.* -import io.ktor.client.request.* -import io.ktor.serialization.kotlinx.json.* +import io.ktor.client.HttpClient +import io.ktor.client.plugins.cache.HttpCache +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.client.plugins.logging.LogLevel +import io.ktor.client.plugins.logging.Logger +import io.ktor.client.plugins.logging.Logging +import io.ktor.client.request.get +import io.ktor.client.request.parameter +import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.json.Json -class MercuryApi() { +class MercuryApi { var client = createHttpClient() private fun createHttpClient(): HttpClient { return HttpClient { + install(HttpCache) install(ContentNegotiation) { - install(HttpCache) json( Json { prettyPrint = true @@ -44,4 +48,4 @@ class MercuryApi() { parameter("link", url) }, ) -} +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt index f0198c0..fc7bb82 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt @@ -45,8 +45,8 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { setupInsecureHTTPEngine(this) } } + install(HttpCache) install(ContentNegotiation) { - install(HttpCache) json( Json { prettyPrint = true @@ -105,8 +105,8 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { private fun hasLoginInfo() = appSettingsService.getUserName().isNotEmpty() && - appSettingsService.getPassword() - .isNotEmpty() + appSettingsService.getPassword() + .isNotEmpty() suspend fun login(): SuccessResponse = if (appSettingsService.getUserName().isNotEmpty() && @@ -127,7 +127,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { client.tryToGet(url("/login")) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -148,7 +150,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { client.tryToPost(url("/login")) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -164,7 +168,8 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { }, ) - private fun shouldHaveNewLogout() = appSettingsService.getApiVersion() >= 5 // We are missing 4.1.0 + private fun shouldHaveNewLogout() = + appSettingsService.getApiVersion() >= 5 // We are missing 4.1.0 suspend fun logout(): SuccessResponse = if (shouldHaveNewLogout()) { @@ -176,7 +181,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { private suspend fun maybeLogoutIfAvailable() = responseOrSuccessIf404( client.tryToGet(url("/logout")) { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -195,7 +202,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { private suspend fun doLogout() = maybeResponse( client.tryToDelete(url("/api/session/current")) { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -233,7 +242,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("updatedsince", updatedSince) parameter("items", items ?: appSettingsService.getItemsNumber()) parameter("offset", offset) - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -258,7 +269,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { } parameter("type", "all") parameter("items", 1) - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -281,7 +294,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -304,7 +319,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -327,7 +344,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -350,7 +369,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -373,7 +394,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -396,7 +419,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -415,7 +440,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { suspend fun apiInformation(): StatusAndData = bodyOrFailure( client.tryToGet(url("/api/about")) { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -438,7 +465,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -461,7 +490,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -484,7 +515,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -507,7 +540,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -528,15 +563,17 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { client.tryToSubmitForm( url = url("/mark"), formParameters = - Parameters.build { - if (!shouldHavePostLogin()) { - append("username", appSettingsService.getUserName()) - append("password", appSettingsService.getPassword()) - } - ids.map { append("ids[]", it) } - }, + Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + ids.map { append("ids[]", it) } + }, block = { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -577,18 +614,20 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { client.tryToSubmitForm( url = url("/source"), formParameters = - Parameters.build { - if (!shouldHavePostLogin()) { - append("username", appSettingsService.getUserName()) - append("password", appSettingsService.getPassword()) - } - append("title", title) - append("url", url) - append("spout", spout) - append(tagsParamName, tags) - }, + Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + append("title", title) + append("url", url) + append("spout", spout) + append(tagsParamName, tags) + }, block = { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -630,18 +669,20 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { client.tryToSubmitForm( url = url("/source/$id"), formParameters = - Parameters.build { - if (!shouldHavePostLogin()) { - append("username", appSettingsService.getUserName()) - append("password", appSettingsService.getPassword()) - } - append("title", title) - append("url", url) - append("spout", spout) - append(tagsParamName, tags) - }, + Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + append("title", title) + append("url", url) + append("spout", spout) + append(tagsParamName, tags) + }, block = { - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -664,7 +705,9 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { parameter("username", appSettingsService.getUserName()) parameter("password", appSettingsService.getPassword()) } - if (appSettingsService.getBasicUserName().isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty()) { + if (appSettingsService.getBasicUserName() + .isNotEmpty() && appSettingsService.getBasicPassword().isNotEmpty() + ) { headers { append( HttpHeaders.Authorization, @@ -679,4 +722,4 @@ class SelfossApi(private val appSettingsService: AppSettingsService) { } }, ) -} +} \ No newline at end of file