https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
l

Lena Stepanova

06/30/2021, 10:51 AM
Hi everyone! Is it possible to dynamically change the host when making a request? I use ktor to access a local source (host IP is determined from user input) and this code works for Android, but in Ios I get org.koin.core.error.InstanceCreationException: Could not create instance for [Single:'...shared.ktor.LocalKtorApi']
Copy code
class LocalApi (log: Kermit) : LocalKtorApi {
    private var localUrl = ""

    // If this is a constructor property, then it gets captured
    // inside HttpClient config and freezes this whole class.
    @Suppress("CanBePrimaryConstructorProperty")
    private val log = log

    private val client = HttpClientProvider().getHttpClient(localUrl).config {
        defaultRequest {
            host = ""
            header(HttpHeaders.ContentType, ContentType.Application.Json)
            url {
                //protocol = URLProtocol.HTTPS
            }
        }
        install(JsonFeature) {
            accept(ContentType.Text.Plain)
            val json = Json {
                ignoreUnknownKeys = true
                useAlternativeNames = false
            }
            serializer = KotlinxSerializer(json)
        }

        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    log.v("Network") { message }
                }
            }

            level = LogLevel.ALL
        }

    }


    init {
        ensureNeverFrozen()
    }


    override suspend fun verifyLocalCredentials(url: String, credentials: String) {
        localUrl = url
        log.v { "credentials: $credentials" }
        return client.get {
            url(path = "/apps")
            header("Authorization", "Basic $credentials")
        }
    }

}
May be the problem is with ios HttpClient? Because it's simply
Copy code
actual class HttpClientProvider actual constructor() {
    actual fun getHttpClient(
        host: String
    ): HttpClient {
        return HttpClient(Ios){}
    }
}
🧵 2
Was solved with creating a local HttpClient inside the request function itself
Copy code
override suspend fun verifyLocalCredentials(url: String, credentials: String) {
        //localUrl = url
        log.v { "credentials: $credentials" }
        return HttpClientProvider().getHttpClient(localUrl).config {
            defaultRequest{
                host = url
                header(HttpHeaders.ContentType, ContentType.Application.Json)
            }

            install(JsonFeature) {
                accept(ContentType.Text.Plain)
                val json = Json {
                    ignoreUnknownKeys = true
                    //<https://github.com/Kotlin/kotlinx.serialization/issues/1450#issuecomment-841214332>
                    useAlternativeNames = false
                }
                serializer = KotlinxSerializer(json)
            }

            install(Logging) {
                level = <http://LogLevel.INFO|LogLevel.INFO>
            }

        }.get {
            url(path = "/apps")
            header("Authorization", "Basic $credentials")
        }
    }

}
55 Views