Skip to content

feat: add user agent #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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)
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.mavenPublishPlugin)
alias(libs.plugins.downloadPlugin)
alias(libs.plugins.buildKonfig)
id("com.powersync.plugins.sonatype")
}

Expand Down Expand Up @@ -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 {
Expand All @@ -180,4 +197,3 @@ afterEvaluate {
}

setupGithubRepository()

10 changes: 8 additions & 2 deletions core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" }
Expand All @@ -173,7 +179,7 @@ internal class SyncStream(
contentType(ContentType.Application.Json)
headers {
append(HttpHeaders.Authorization, "Token ${credentials.token}")
append("User-Id", credentials.userId ?: "")
append("User-Agent", powerSyncUserAgent())
}
}
if (response.status.value == 401) {
Expand All @@ -199,7 +205,7 @@ 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 }
setBody(bodyJson)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 = [
Expand Down
Loading