Skip to content

Commit

Permalink
DefaultGBNetworkDispatcher was moved into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan-Kim committed Jun 4, 2024
1 parent 4128e97 commit d145939
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 27 deletions.
122 changes: 122 additions & 0 deletions DefaultNetworkDispatcher/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<MavenPublication> {
//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)
}
6 changes: 6 additions & 0 deletions DefaultNetworkDispatcher/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.sdk.growthbook"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -27,7 +27,7 @@ class DefaultGBNetworkClientTest {
)
}

classUnderTest = DefaultGBNetworkClient(
classUnderTest = DefaultGBNetworkDispatcher(
HttpClient(mockEngine)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -122,7 +122,7 @@ class DefaultGBNetworkClient(
append("Accept", "application/json")
}
contentType(ContentType.Application.Json)
setBody(bodyParams.toJsonElement())
//setBody(bodyParams.toJsonElement())

This comment has been minimized.

Copy link
@sbelloz

sbelloz Dec 16, 2024

@vazarkevych I've noticed that the body is not being sent in Ktor's dispatcher, while it is sent in the OkHttp dependency.

Is this intentional? Is it documented somewhere?

This comment has been minimized.

Copy link
@Bohdan-Kim

Bohdan-Kim Dec 16, 2024

Author Collaborator

@sbelloz It was a mistake. It was already fixed in this pull request

println("body = $body")
}
onSuccess(response.body())
Expand All @@ -142,4 +142,4 @@ class DefaultGBNetworkClient(
url.parameters.remove(key)
url.parameters.append(key, it)
} ?: Unit
}
}
6 changes: 1 addition & 5 deletions GrowthBook/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pluginManagement {
rootProject.name = "GrowthBook"
include(":GrowthBook")
include(":Core")
include(":DefaultNetworkDispatcher")

0 comments on commit d145939

Please sign in to comment.