From d145939c6b92d4eb96c9bd64c0f531387b617896 Mon Sep 17 00:00:00 2001 From: Bohdan Akimenko Date: Tue, 4 Jun 2024 18:33:11 +0300 Subject: [PATCH] DefaultGBNetworkDispatcher was moved into separate module --- ...{NetworkClient.kt => NetworkDispatcher.kt} | 0 DefaultNetworkDispatcher/build.gradle.kts | 122 ++++++++++++++++++ .../src/androidMain/AndroidManifest.xml | 6 + .../kotlin/com/sdk/growthbook/Dispatcher.kt | 0 .../DefaultGBNetworkDispatcherTest.kt | 8 +- .../kotlin/com/sdk/growthbook/Dispatcher.kt | 0 .../network/DefaultGBNetworkDispatcher.kt | 36 +++--- .../com/sdk/growthbook/utils/SseReader.kt | 0 .../com/sdk/growthbook/utils/SseResponse.kt | 0 .../kotlin/com/sdk/growthbook/Dispatcher.kt | 0 GrowthBook/build.gradle.kts | 6 +- settings.gradle.kts | 1 + 12 files changed, 152 insertions(+), 27 deletions(-) rename Core/src/commonMain/kotlin/com/sdk/growthbook/network/{NetworkClient.kt => NetworkDispatcher.kt} (100%) create mode 100644 DefaultNetworkDispatcher/build.gradle.kts create mode 100644 DefaultNetworkDispatcher/src/androidMain/AndroidManifest.xml rename {GrowthBook => DefaultNetworkDispatcher}/src/androidMain/kotlin/com/sdk/growthbook/Dispatcher.kt (100%) rename GrowthBook/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkClientTest.kt => DefaultNetworkDispatcher/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkDispatcherTest.kt (89%) rename {GrowthBook => DefaultNetworkDispatcher}/src/commonMain/kotlin/com/sdk/growthbook/Dispatcher.kt (100%) rename GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt => DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/network/DefaultGBNetworkDispatcher.kt (96%) rename {GrowthBook => DefaultNetworkDispatcher}/src/commonMain/kotlin/com/sdk/growthbook/utils/SseReader.kt (100%) rename {GrowthBook => DefaultNetworkDispatcher}/src/commonMain/kotlin/com/sdk/growthbook/utils/SseResponse.kt (100%) rename {GrowthBook => DefaultNetworkDispatcher}/src/jvmMain/kotlin/com/sdk/growthbook/Dispatcher.kt (100%) diff --git a/Core/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt b/Core/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkDispatcher.kt similarity index 100% rename from Core/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt rename to Core/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkDispatcher.kt diff --git a/DefaultNetworkDispatcher/build.gradle.kts b/DefaultNetworkDispatcher/build.gradle.kts new file mode 100644 index 00000000..46b5e927 --- /dev/null +++ b/DefaultNetworkDispatcher/build.gradle.kts @@ -0,0 +1,122 @@ +plugins { + kotlin("multiplatform") + id("com.android.library") + kotlin("plugin.serialization") + id("maven-publish") + id("signing") +} + +group = "io.growthbook.sdk" +version = "1.0.0" + +kotlin { + android { + publishLibraryVariants("release") + } + + jvm { + compilations.all { + kotlinOptions.jvmTarget = "1.8" + } + } + + val ktorVersion = "2.1.2" + sourceSets { + val commonMain by getting { + dependencies { + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0") + + implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") + implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") + + implementation("io.growthbook.sdk:Core:1.0.0") + } + } + val androidTest by getting { + dependencies { + implementation(kotlin("test-junit")) + implementation("io.ktor:ktor-client-mock:$ktorVersion") + } + } + } +} + +android { + compileSdk = 34 + namespace = "com.sdk.growthbook.default_network_dispatcher" + defaultConfig { + minSdk = 21 + } + buildTypes { + release { + isMinifyEnabled = false + } + debug { + isMinifyEnabled = false + } + } +} + +val sonatypeUsername: String? = System.getenv("GB_SONATYPE_USERNAME") +val sonatypePassword: String? = System.getenv("GB_SONATYPE_PASSWORD") + +/** + * Publishing Task for MavenCentral + */ +publishing { + repositories { + maven { + name = "kotlin" + val releasesRepoUrl = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") + val snapshotsRepoUrl = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") + url = if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl + credentials { + username = sonatypeUsername + password = sonatypePassword + } + } + } + + publications { + withType { + //artifact(javadocJar) + pom { + name.set("kotlin") + description.set("Default network dispatcher for GrowthBook") + licenses { + license { + name.set("MIT") + url.set("https://opensource.org/licenses/MIT") + } + } + url.set("https://github.com/growthbook/growthbook-kotlin") + issueManagement { + system.set("Github") + url.set("https://github.com/growthbook/growthbook-kotlin/issues") + } + scm { + connection.set("https://github.com/growthbook/growthbook-kotlin.git") + url.set("https://github.com/growthbook/growthbook-kotlin") + } + developers { + developer { + name.set("Bohdan Kim") + email.set("user576g@gmail.com") + } + } + } + } + } +} + +/** + * Signing JAR using GPG Keys + */ +signing { + useInMemoryPgpKeys( + System.getenv("GPG_PRIVATE_KEY"), + System.getenv("GPG_PRIVATE_PASSWORD") + ) + sign(publishing.publications) +} \ No newline at end of file diff --git a/DefaultNetworkDispatcher/src/androidMain/AndroidManifest.xml b/DefaultNetworkDispatcher/src/androidMain/AndroidManifest.xml new file mode 100644 index 00000000..10812a60 --- /dev/null +++ b/DefaultNetworkDispatcher/src/androidMain/AndroidManifest.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/GrowthBook/src/androidMain/kotlin/com/sdk/growthbook/Dispatcher.kt b/DefaultNetworkDispatcher/src/androidMain/kotlin/com/sdk/growthbook/Dispatcher.kt similarity index 100% rename from GrowthBook/src/androidMain/kotlin/com/sdk/growthbook/Dispatcher.kt rename to DefaultNetworkDispatcher/src/androidMain/kotlin/com/sdk/growthbook/Dispatcher.kt diff --git a/GrowthBook/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkClientTest.kt b/DefaultNetworkDispatcher/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkDispatcherTest.kt similarity index 89% rename from GrowthBook/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkClientTest.kt rename to DefaultNetworkDispatcher/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkDispatcherTest.kt index 429bbb8b..2abb8f88 100644 --- a/GrowthBook/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkClientTest.kt +++ b/DefaultNetworkDispatcher/src/androidTest/kotlin/com/sdk/growthbook/DefaultGBNetworkDispatcherTest.kt @@ -1,6 +1,6 @@ package com.sdk.growthbook -import com.sdk.growthbook.network.DefaultGBNetworkClient +import com.sdk.growthbook.network.DefaultGBNetworkDispatcher import io.ktor.client.HttpClient import io.ktor.client.engine.mock.MockEngine import org.junit.Test @@ -15,8 +15,8 @@ import org.junit.Assert.assertTrue private const val FEATURES_ENDPOINT = "/api/features/" -class DefaultGBNetworkClientTest { - private val classUnderTest: DefaultGBNetworkClient +class DefaultGBNetworkDispatcherTest { + private val classUnderTest: DefaultGBNetworkDispatcher init { val mockEngine = MockEngine { @@ -27,7 +27,7 @@ class DefaultGBNetworkClientTest { ) } - classUnderTest = DefaultGBNetworkClient( + classUnderTest = DefaultGBNetworkDispatcher( HttpClient(mockEngine) ) } diff --git a/GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/Dispatcher.kt b/DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/Dispatcher.kt similarity index 100% rename from GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/Dispatcher.kt rename to DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/Dispatcher.kt diff --git a/GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt b/DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/network/DefaultGBNetworkDispatcher.kt similarity index 96% rename from GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt rename to DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/network/DefaultGBNetworkDispatcher.kt index 76ea234f..3b5585b2 100644 --- a/GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/network/NetworkClient.kt +++ b/DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/network/DefaultGBNetworkDispatcher.kt @@ -1,29 +1,29 @@ package com.sdk.growthbook.network -import com.sdk.growthbook.PlatformDependentIODispatcher import com.sdk.growthbook.utils.Resource -import com.sdk.growthbook.utils.readSse -import com.sdk.growthbook.utils.toJsonElement +import kotlinx.coroutines.Job import io.ktor.client.HttpClient -import io.ktor.client.call.body import io.ktor.client.plugins.contentnegotiation.ContentNegotiation -import io.ktor.client.request.HttpRequestBuilder +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.json.Json +import kotlinx.coroutines.CoroutineScope +import com.sdk.growthbook.PlatformDependentIODispatcher +import kotlinx.coroutines.launch import io.ktor.client.request.get -import io.ktor.client.request.headers import io.ktor.client.request.post import io.ktor.client.request.prepareGet -import io.ktor.client.request.setBody +import io.ktor.client.call.body import io.ktor.client.statement.HttpStatement -import io.ktor.http.ContentType -import io.ktor.http.contentType -import io.ktor.serialization.kotlinx.json.json +import io.ktor.client.request.headers +import kotlinx.coroutines.flow.callbackFlow import io.ktor.utils.io.ByteReadChannel -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job +import com.sdk.growthbook.utils.readSse import kotlinx.coroutines.channels.awaitClose -import kotlinx.coroutines.flow.callbackFlow -import kotlinx.coroutines.launch -import kotlinx.serialization.json.Json +import io.ktor.http.ContentType +import io.ktor.http.contentType +//import io.ktor.client.request.setBody +//import com.sdk.growthbook.utils.toJsonElement +import io.ktor.client.request.HttpRequestBuilder internal fun createDefaultHttpClient(): HttpClient = HttpClient { @@ -39,7 +39,7 @@ internal fun createDefaultHttpClient(): HttpClient = /** * Default Ktor Implementation for Network Dispatcher */ -class DefaultGBNetworkClient( +class DefaultGBNetworkDispatcher( /** * Ktor http client instance for sending request @@ -122,7 +122,7 @@ class DefaultGBNetworkClient( append("Accept", "application/json") } contentType(ContentType.Application.Json) - setBody(bodyParams.toJsonElement()) + //setBody(bodyParams.toJsonElement()) println("body = $body") } onSuccess(response.body()) @@ -142,4 +142,4 @@ class DefaultGBNetworkClient( url.parameters.remove(key) url.parameters.append(key, it) } ?: Unit -} \ No newline at end of file +} diff --git a/GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/utils/SseReader.kt b/DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/utils/SseReader.kt similarity index 100% rename from GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/utils/SseReader.kt rename to DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/utils/SseReader.kt diff --git a/GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/utils/SseResponse.kt b/DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/utils/SseResponse.kt similarity index 100% rename from GrowthBook/src/commonMain/kotlin/com/sdk/growthbook/utils/SseResponse.kt rename to DefaultNetworkDispatcher/src/commonMain/kotlin/com/sdk/growthbook/utils/SseResponse.kt diff --git a/GrowthBook/src/jvmMain/kotlin/com/sdk/growthbook/Dispatcher.kt b/DefaultNetworkDispatcher/src/jvmMain/kotlin/com/sdk/growthbook/Dispatcher.kt similarity index 100% rename from GrowthBook/src/jvmMain/kotlin/com/sdk/growthbook/Dispatcher.kt rename to DefaultNetworkDispatcher/src/jvmMain/kotlin/com/sdk/growthbook/Dispatcher.kt diff --git a/GrowthBook/build.gradle.kts b/GrowthBook/build.gradle.kts index 20bd66d7..60181190 100644 --- a/GrowthBook/build.gradle.kts +++ b/GrowthBook/build.gradle.kts @@ -31,13 +31,10 @@ kotlin { dependencies { implementation("io.growthbook.sdk:Core:1.0.0") implementation("org.jetbrains.kotlin:kotlin-stdlib-common") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0") implementation( "org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion" ) - implementation("io.ktor:ktor-client-core:$ktorVersion") - implementation("io.ktor:ktor-utils:$ktorVersion") - implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion") - implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion") implementation("com.ionspin.kotlin:bignum:0.3.3") implementation("com.soywiz.korlibs.krypto:krypto:$kryptoVersion") } @@ -54,7 +51,6 @@ kotlin { } val androidMain by getting { dependencies { - implementation("io.ktor:ktor-client-android:$ktorVersion") implementation("androidx.startup:startup-runtime:1.1.1") implementation("com.soywiz.korlibs.krypto:krypto-android:$kryptoVersion") } diff --git a/settings.gradle.kts b/settings.gradle.kts index 44db9a43..70bdbf94 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,3 +9,4 @@ pluginManagement { rootProject.name = "GrowthBook" include(":GrowthBook") include(":Core") +include(":DefaultNetworkDispatcher")