From 63961e91db1b322cdf08cda347df3a1130c8faf5 Mon Sep 17 00:00:00 2001 From: Lubos Horacek Date: Tue, 2 Jul 2024 12:00:02 +0200 Subject: [PATCH 1/4] Add iOS targets --- search-client/build.gradle.kts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/search-client/build.gradle.kts b/search-client/build.gradle.kts index 09780940..83ccc50e 100644 --- a/search-client/build.gradle.kts +++ b/search-client/build.gradle.kts @@ -89,6 +89,10 @@ kotlin { mingwX64() macosX64() macosArm64() + // iOS targets + iosArm64() + iosX64() + iosSimulatorArm64() // Blocked on ktor-client support // wasmJs { // browser() From efbfa96178b2f6d4e184949b19f0fa4be8a14917 Mon Sep 17 00:00:00 2001 From: Lubos Horacek Date: Tue, 2 Jul 2024 18:57:43 +0200 Subject: [PATCH 2/4] Update project targets for iOS with ktor dependencies --- search-client/build.gradle.kts | 18 +++++- .../ktsearch/KtorRestClient.ios.kt | 56 +++++++++++++++++++ .../ktsearch/RoundRobinNodeSelector.ios.kt | 11 ++++ .../ktsearch/SniffingNodeSelector.ios.kt | 6 ++ search-dsls/build.gradle.kts | 4 ++ 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.ios.kt create mode 100644 search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.ios.kt create mode 100644 search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.ios.kt diff --git a/search-client/build.gradle.kts b/search-client/build.gradle.kts index 83ccc50e..aaad9cf9 100644 --- a/search-client/build.gradle.kts +++ b/search-client/build.gradle.kts @@ -92,7 +92,8 @@ kotlin { // iOS targets iosArm64() iosX64() - iosSimulatorArm64() + // Blocked on json-dsl and kotlinx-serialization-extensions support + // iosSimulatorArm64() // Blocked on ktor-client support // wasmJs { // browser() @@ -101,7 +102,7 @@ kotlin { // } sourceSets { - commonMain { + val commonMain by getting { dependencies { api(kotlin("stdlib-common", "_")) api(project(":search-dsls")) @@ -165,6 +166,19 @@ kotlin { } } + val iosX64Main by getting + val iosArm64Main by getting + // val iosSimulatorArm64Main by getting + val iosMain by creating { + dependsOn(commonMain) + iosX64Main.dependsOn(this) + iosArm64Main.dependsOn(this) + // iosSimulatorArm64Main.dependsOn(this) + dependencies { + implementation(Ktor.client.darwin) + } + } + all { languageSettings { optIn("kotlin.RequiresOptIn") diff --git a/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.ios.kt b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.ios.kt new file mode 100644 index 00000000..a302f8a1 --- /dev/null +++ b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.ios.kt @@ -0,0 +1,56 @@ +package com.jillesvangurp.ktsearch + +import com.jillesvangurp.serializationext.DEFAULT_JSON +import io.ktor.client.* +import io.ktor.client.plugins.auth.* +import io.ktor.client.plugins.auth.providers.* +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.client.plugins.logging.* +import io.ktor.http.* +import io.ktor.serialization.kotlinx.json.* + + +import io.ktor.client.* +import io.ktor.client.engine.darwin.* + +actual fun defaultKtorHttpClient( + logging: Boolean, + user: String?, + password: String?, + elasticApiKey: String? +): HttpClient { + return HttpClient(Darwin) { + engine { + pipelining = true + configureRequest { + setAllowsCellularAccess(true) + } + } + if(!user.isNullOrBlank() && !password.isNullOrBlank()) { + install(Auth) { + basic { + credentials { + BasicAuthCredentials(user, password) + } + sendWithoutRequest { + true + } + } + } + } + if(!elasticApiKey.isNullOrBlank()) { + headers { + append("Authorization", "ApiKey $elasticApiKey") + } + } + install(ContentNegotiation) { + json(DEFAULT_JSON) + } + if (logging) { + install(Logging) { + level = LogLevel.ALL + } + } + + } +} \ No newline at end of file diff --git a/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.ios.kt b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.ios.kt new file mode 100644 index 00000000..100faf45 --- /dev/null +++ b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.ios.kt @@ -0,0 +1,11 @@ +package com.jillesvangurp.ktsearch + +actual fun simpleIndexProvider(initialIndex: Int): IndexProvider = object : IndexProvider { + private var index = 0 + + override fun get(): Int = index + + override fun set(value: Int) { + index = value + } +} \ No newline at end of file diff --git a/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.ios.kt b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.ios.kt new file mode 100644 index 00000000..c77ac9a3 --- /dev/null +++ b/search-client/src/iosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.ios.kt @@ -0,0 +1,6 @@ +package com.jillesvangurp.ktsearch + +/** + * On JVM this will return the current thread name, otherwise this will return null and pick a random node + */ +actual fun threadId(): String? = null \ No newline at end of file diff --git a/search-dsls/build.gradle.kts b/search-dsls/build.gradle.kts index 520ab77a..1bd35528 100644 --- a/search-dsls/build.gradle.kts +++ b/search-dsls/build.gradle.kts @@ -26,6 +26,10 @@ kotlin { mingwX64() macosX64() macosArm64() + iosArm64() + iosX64() + // Blocked on json-dsl and kotlinx-serialization-extensions support + // iosSimulatorArm64() wasmJs { browser() nodejs() From c8f58c281058e0e4be2bc95b9b9bf5be8d483138 Mon Sep 17 00:00:00 2001 From: Lubos Horacek Date: Wed, 10 Jul 2024 12:11:11 +0200 Subject: [PATCH 3/4] Update project config to keep correct dependencies for KTOR --- search-client/build.gradle.kts | 19 +++---- .../ktsearch/KtorRestClient.macos.kt | 56 +++++++++++++++++++ .../ktsearch/RoundRobinNodeSelector.macos.kt} | 0 .../ktsearch/SniffingNodeSelector.macos.kt} | 0 .../ktsearch/KtorRestClient.mingw.kt} | 0 .../ktsearch/RoundRobinNodeSelector.mingw.kt | 11 ++++ .../ktsearch/SniffingNodeSelector.mingw.kt | 6 ++ 7 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.macos.kt rename search-client/src/{nativeMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.native.kt => macosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.macos.kt} (100%) rename search-client/src/{nativeMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.native.kt => macosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.macos.kt} (100%) rename search-client/src/{nativeMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.native.kt => mingwMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.mingw.kt} (100%) create mode 100644 search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.mingw.kt create mode 100644 search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.mingw.kt diff --git a/search-client/build.gradle.kts b/search-client/build.gradle.kts index aaad9cf9..d69d2090 100644 --- a/search-client/build.gradle.kts +++ b/search-client/build.gradle.kts @@ -160,25 +160,24 @@ kotlin { } } - nativeMain { + iosMain { dependencies { - implementation(Ktor.client.curl) + implementation(Ktor.client.darwin) } } - val iosX64Main by getting - val iosArm64Main by getting - // val iosSimulatorArm64Main by getting - val iosMain by creating { - dependsOn(commonMain) - iosX64Main.dependsOn(this) - iosArm64Main.dependsOn(this) - // iosSimulatorArm64Main.dependsOn(this) + macosMain { dependencies { implementation(Ktor.client.darwin) } } + mingwMain { + dependencies { + implementation(Ktor.client.curl) + } + } + all { languageSettings { optIn("kotlin.RequiresOptIn") diff --git a/search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.macos.kt b/search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.macos.kt new file mode 100644 index 00000000..a302f8a1 --- /dev/null +++ b/search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.macos.kt @@ -0,0 +1,56 @@ +package com.jillesvangurp.ktsearch + +import com.jillesvangurp.serializationext.DEFAULT_JSON +import io.ktor.client.* +import io.ktor.client.plugins.auth.* +import io.ktor.client.plugins.auth.providers.* +import io.ktor.client.plugins.contentnegotiation.* +import io.ktor.client.plugins.logging.* +import io.ktor.http.* +import io.ktor.serialization.kotlinx.json.* + + +import io.ktor.client.* +import io.ktor.client.engine.darwin.* + +actual fun defaultKtorHttpClient( + logging: Boolean, + user: String?, + password: String?, + elasticApiKey: String? +): HttpClient { + return HttpClient(Darwin) { + engine { + pipelining = true + configureRequest { + setAllowsCellularAccess(true) + } + } + if(!user.isNullOrBlank() && !password.isNullOrBlank()) { + install(Auth) { + basic { + credentials { + BasicAuthCredentials(user, password) + } + sendWithoutRequest { + true + } + } + } + } + if(!elasticApiKey.isNullOrBlank()) { + headers { + append("Authorization", "ApiKey $elasticApiKey") + } + } + install(ContentNegotiation) { + json(DEFAULT_JSON) + } + if (logging) { + install(Logging) { + level = LogLevel.ALL + } + } + + } +} \ No newline at end of file diff --git a/search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.native.kt b/search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.macos.kt similarity index 100% rename from search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.native.kt rename to search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.macos.kt diff --git a/search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.native.kt b/search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.macos.kt similarity index 100% rename from search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.native.kt rename to search-client/src/macosMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.macos.kt diff --git a/search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.native.kt b/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.mingw.kt similarity index 100% rename from search-client/src/nativeMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.native.kt rename to search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/KtorRestClient.mingw.kt diff --git a/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.mingw.kt b/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.mingw.kt new file mode 100644 index 00000000..100faf45 --- /dev/null +++ b/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/RoundRobinNodeSelector.mingw.kt @@ -0,0 +1,11 @@ +package com.jillesvangurp.ktsearch + +actual fun simpleIndexProvider(initialIndex: Int): IndexProvider = object : IndexProvider { + private var index = 0 + + override fun get(): Int = index + + override fun set(value: Int) { + index = value + } +} \ No newline at end of file diff --git a/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.mingw.kt b/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.mingw.kt new file mode 100644 index 00000000..c77ac9a3 --- /dev/null +++ b/search-client/src/mingwMain/kotlin/com/jillesvangurp/ktsearch/SniffingNodeSelector.mingw.kt @@ -0,0 +1,6 @@ +package com.jillesvangurp.ktsearch + +/** + * On JVM this will return the current thread name, otherwise this will return null and pick a random node + */ +actual fun threadId(): String? = null \ No newline at end of file From 708fc2ee5de31c9b4687a9c9fc23a2468f1727f7 Mon Sep 17 00:00:00 2001 From: Lubos Horacek Date: Wed, 10 Jul 2024 12:17:22 +0200 Subject: [PATCH 4/4] Cleanup --- search-client/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search-client/build.gradle.kts b/search-client/build.gradle.kts index d69d2090..b19ebfe8 100644 --- a/search-client/build.gradle.kts +++ b/search-client/build.gradle.kts @@ -102,7 +102,7 @@ kotlin { // } sourceSets { - val commonMain by getting { + commonMain { dependencies { api(kotlin("stdlib-common", "_")) api(project(":search-dsls"))