From a029d8a7dcde290f975a28de12aba6e4b2ad9749 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 10 Sep 2023 21:33:28 +0200 Subject: [PATCH] Move api client creation function within api class --- .../readerforselfossv2/rest/SelfossApi.kt | 94 +++++++++---------- 1 file changed, 45 insertions(+), 49 deletions(-) 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 b637b90..71e75bb 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 @@ -35,63 +35,59 @@ import kotlinx.serialization.json.Json expect fun setupInsecureHTTPEngine(config: CIOEngineConfig) -fun createHttpClient( - appSettingsService: AppSettingsService, - api: SelfossApi -) = - HttpClient(CIO) { - if (appSettingsService.getSelfSigned()) { - engine { - setupInsecureHTTPEngine(this) - } - } - install(ContentNegotiation) { - install(HttpCache) - json(Json { - prettyPrint = true - isLenient = true - ignoreUnknownKeys = true - }) - } - install(Logging) { - logger = object : Logger { - override fun log(message: String) { - Napier.d(message, tag = "LogApiCalls") - } - } - level = LogLevel.INFO - } - install(HttpTimeout) { - requestTimeoutMillis = appSettingsService.getApiTimeout() - } - install(HttpCookies) - install(HttpRequestRetry) { - maxRetries = 2 - retryIf { _, response -> - response.status == HttpStatusCode.Forbidden && api.shouldHavePostLogin() && api.hasLoginInfo() - } - modifyRequest { - Napier.i("Will modify", tag = "HttpSend") - CoroutineScope(Dispatchers.Main).launch { - Napier.i("Will login", tag = "HttpSend") - api.login() - Napier.i("Did login", tag = "HttpSend") - } - } - } - expectSuccess = false - } - class SelfossApi(private val appSettingsService: AppSettingsService) { - var client = createHttpClient(appSettingsService, this) + var client = createHttpClient() + fun createHttpClient() = + HttpClient(CIO) { + if (appSettingsService.getSelfSigned()) { + engine { + setupInsecureHTTPEngine(this) + } + } + install(ContentNegotiation) { + install(HttpCache) + json(Json { + prettyPrint = true + isLenient = true + ignoreUnknownKeys = true + }) + } + install(Logging) { + logger = object : Logger { + override fun log(message: String) { + Napier.d(message, tag = "LogApiCalls") + } + } + level = LogLevel.INFO + } + install(HttpTimeout) { + requestTimeoutMillis = appSettingsService.getApiTimeout() + } + install(HttpCookies) + install(HttpRequestRetry) { + maxRetries = 2 + retryIf { _, response -> + response.status == HttpStatusCode.Forbidden && shouldHavePostLogin() && hasLoginInfo() + } + modifyRequest { + Napier.i("Will modify", tag = "HttpSend") + CoroutineScope(Dispatchers.Main).launch { + Napier.i("Will login", tag = "HttpSend") + login() + Napier.i("Did login", tag = "HttpSend") + } + } + } + expectSuccess = false + } fun url(path: String) = "${appSettingsService.getBaseUrl()}$path" fun refreshLoginInformation() { appSettingsService.refreshApiSettings() - client = createHttpClient(appSettingsService, this) + client = createHttpClient() } fun constructBasicAuthValue(credentials: BasicAuthCredentials): String {