From e7c4261d69a28bd8660327d57f59a40a765bf5f8 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 20 Sep 2024 13:14:06 +0200 Subject: [PATCH 1/3] feat: add user agent --- core/build.gradle.kts | 18 +++++++++++++++++- .../kotlin/com/powersync/sync/SyncStream.kt | 8 ++++++++ gradle/libs.versions.toml | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 78fbb70e..78c0cd45 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -1,6 +1,7 @@ import de.undercouch.gradle.tasks.download.Download import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import com.powersync.plugins.sonatype.setupGithubRepository +import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING plugins { alias(libs.plugins.kotlinMultiplatform) @@ -8,6 +9,7 @@ plugins { alias(libs.plugins.androidLibrary) alias(libs.plugins.mavenPublishPlugin) alias(libs.plugins.downloadPlugin) + alias(libs.plugins.buildKonfig) id("com.powersync.plugins.sonatype") } @@ -161,6 +163,21 @@ android { } } +buildkonfig { + packageName = "com.powersync.core" + defaultConfigs { + buildConfigField(STRING, "LIBRARY_VERSION", version.toString()) + buildConfigField(STRING, "LIBRARY_NAME", "powersync-kotlin") + } + // This will be used to differentiate between the Swift and Kotlin SDKs + // TODO: Need to update publish plugin to publish individual modules + // then update build script to add -Pbuildkonfig.flavor=swift-sdk as a flag + // and implement that as its own github workflow + defaultConfigs("swift-sdk") { + buildConfigField(STRING, "LIBRARY_NAME", "powersync-swift") + } +} + afterEvaluate { val buildTasks = tasks.matching { @@ -180,4 +197,3 @@ afterEvaluate { } setupGithubRepository() - diff --git a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt index 92eb7422..ffff2b83 100644 --- a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt +++ b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt @@ -8,6 +8,8 @@ import com.powersync.bucket.Checkpoint import com.powersync.bucket.WriteCheckpointResponse import co.touchlab.stately.concurrency.AtomicBoolean import com.powersync.connectors.PowerSyncBackendConnector +import com.powersync.core.BuildKonfig.LIBRARY_VERSION +import com.powersync.core.BuildKonfig.LIBRARY_NAME import com.powersync.utils.JsonUtil import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -164,6 +166,10 @@ internal class SyncStream( } } + private fun powerSyncUserAgent(): String { + return "$LIBRARY_NAME/$LIBRARY_VERSION" + } + private suspend fun getWriteCheckpoint(): String { val credentials = connector.getCredentialsCached() require(credentials != null) { "Not logged in" } @@ -174,6 +180,7 @@ internal class SyncStream( headers { append(HttpHeaders.Authorization, "Token ${credentials.token}") append("User-Id", credentials.userId ?: "") + append("User-Agent", powerSyncUserAgent()) } } if (response.status.value == 401) { @@ -200,6 +207,7 @@ internal class SyncStream( headers { append(HttpHeaders.Authorization, "Token ${credentials.token}") append("User-Id", credentials.userId ?: "") + append("User-Agent", powerSyncUserAgent()) } timeout { socketTimeoutMillis = Long.MAX_VALUE } setBody(bodyJson) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9c5d09b6..9c3fad94 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,6 +15,7 @@ kotlinx-datetime = "0.5.0" kotlinx-io = "0.3.0" ktor = "2.3.10" uuid = "0.8.2" +buildKonfig = "0.15.1" powersync-core = "0.2.1" sqlite-android = "3.45.0" @@ -107,6 +108,7 @@ sqldelight = { id = "app.cash.sqldelight", version.ref = "sqlDelight" } grammarKitComposer = { id = "com.alecstrong.grammar.kit.composer", version.ref = "grammerKit" } mavenPublishPlugin = { id = "com.vanniktech.maven.publish", version.ref = "maven-publish" } downloadPlugin = { id = "de.undercouch.download", version.ref = "download-plugin" } +buildKonfig = { id = "com.codingfeline.buildkonfig", version.ref = "buildKonfig" } [bundles] sqldelight = [ From 493b9478b58f71e1291e92739cae4f1344d40270 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 20 Sep 2024 13:18:12 +0200 Subject: [PATCH 2/3] chore: update todo message --- core/build.gradle.kts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 78c0cd45..85a5bcc7 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -171,8 +171,8 @@ buildkonfig { } // This will be used to differentiate between the Swift and Kotlin SDKs // TODO: Need to update publish plugin to publish individual modules - // then update build script to add -Pbuildkonfig.flavor=swift-sdk as a flag - // and implement that as its own github workflow + // then update build script to add -Pbuildkonfig.flavor=swift-sdk as a flag + // and implement that as its own github workflow defaultConfigs("swift-sdk") { buildConfigField(STRING, "LIBRARY_NAME", "powersync-swift") } From 71c3ab36e9879b68c9f35243d60fa48e3ca37d41 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 20 Sep 2024 13:26:53 +0200 Subject: [PATCH 3/3] chore: remove user id from headers --- core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt index ffff2b83..b7a39073 100644 --- a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt +++ b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt @@ -179,7 +179,6 @@ internal class SyncStream( contentType(ContentType.Application.Json) headers { append(HttpHeaders.Authorization, "Token ${credentials.token}") - append("User-Id", credentials.userId ?: "") append("User-Agent", powerSyncUserAgent()) } } @@ -206,7 +205,6 @@ internal class SyncStream( contentType(ContentType.Application.Json) headers { append(HttpHeaders.Authorization, "Token ${credentials.token}") - append("User-Id", credentials.userId ?: "") append("User-Agent", powerSyncUserAgent()) } timeout { socketTimeoutMillis = Long.MAX_VALUE }