Lena Stepanova
06/30/2021, 10:51 AMclass 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
actual class HttpClientProvider actual constructor() {
actual fun getHttpClient(
host: String
): HttpClient {
return HttpClient(Ios){}
}
}
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")
}
}
}