Skip to content

Commit 0eed4e4

Browse files
committed
Make user agent configurable
1 parent 217d03a commit 0eed4e4

File tree

8 files changed

+54
-11
lines changed

8 files changed

+54
-11
lines changed

PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.powersync
44

55
import com.powersync.sync.ConnectionMethod
6+
import com.powersync.sync.SyncOptions
67

78
/**
89
* Helper class designed to bridge SKIEE methods and allow them to throw
@@ -17,14 +18,23 @@ import com.powersync.sync.ConnectionMethod
1718
public fun throwPowerSyncException(exception: PowerSyncException): Unit = throw exception
1819

1920
/**
20-
* Creates a [ConnectionMethod] from a simple boolean, because creating the actual instance with
21+
* Creates a [ConnectionMethod] based on simple booleans, because creating the actual instance with
2122
* the default constructor is not possible from Swift due to an optional argument with an internal
2223
* default value.
2324
*/
2425
@OptIn(ExperimentalPowerSyncAPI::class)
25-
public fun createConnectionMethod(webSocket: Boolean): ConnectionMethod =
26-
if (webSocket) {
27-
ConnectionMethod.WebSocket()
28-
} else {
29-
ConnectionMethod.Http
30-
}
26+
public fun createSyncOptions(
27+
newClient: Boolean,
28+
webSocket: Boolean,
29+
userAgent: String,
30+
): SyncOptions =
31+
SyncOptions(
32+
newClientImplementation = newClient,
33+
method =
34+
if (webSocket) {
35+
ConnectionMethod.WebSocket()
36+
} else {
37+
ConnectionMethod.Http
38+
},
39+
userAgent = userAgent,
40+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.powersync.sync
2+
3+
import android.os.Build
4+
5+
internal actual fun userAgent(): String = "PowerSync Kotlin SDK (Android ${Build.VERSION.SDK_INT})"

core/src/commonMain/kotlin/com/powersync/sync/RSocketSupport.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import kotlin.coroutines.CoroutineContext
4242
*/
4343
@OptIn(RSocketTransportApi::class, ExperimentalPowerSyncAPI::class)
4444
internal fun HttpClient.rSocketSyncStream(
45+
userAgent: String,
4546
options: ConnectionMethod.WebSocket,
4647
req: JsonObject,
4748
credentials: PowerSyncCredentials,
@@ -90,7 +91,14 @@ internal fun HttpClient.rSocketSyncStream(
9091
setupPayload {
9192
buildPayload {
9293
data("{}")
93-
metadata(JsonUtil.json.encodeToString(ConnectionSetupMetadata(token = "Bearer ${credentials.token}")))
94+
metadata(
95+
JsonUtil.json.encodeToString(
96+
ConnectionSetupMetadata(
97+
token = "Bearer ${credentials.token}",
98+
userAgent = userAgent,
99+
),
100+
),
101+
)
94102
}
95103
}
96104

@@ -119,7 +127,7 @@ internal fun HttpClient.rSocketSyncStream(
119127
private class ConnectionSetupMetadata(
120128
val token: String,
121129
@SerialName("user_agent")
122-
val userAgent: String = userAgent(),
130+
val userAgent: String,
123131
)
124132

125133
/**

core/src/commonMain/kotlin/com/powersync/sync/SyncOptions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class SyncOptions
2121
public val newClientImplementation: Boolean = false,
2222
@property:ExperimentalPowerSyncAPI
2323
public val method: ConnectionMethod = ConnectionMethod.Http,
24+
/**
25+
* The user agent to use for requests made to the PowerSync service.
26+
*/
27+
public val userAgent: String = userAgent(),
2428
) {
2529
public companion object {
2630
/**

core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal class SyncStream(
8383

8484
install(DefaultRequest) {
8585
headers {
86-
append("User-Agent", userAgent())
86+
append("User-Agent", options.userAgent)
8787
}
8888
}
8989
}
@@ -255,6 +255,7 @@ internal class SyncStream(
255255

256256
emitAll(
257257
httpClient.rSocketSyncStream(
258+
userAgent = this@SyncStream.options.userAgent,
258259
options = options,
259260
req = req,
260261
credentials = credentials,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.powersync.sync
22

3-
internal fun userAgent(): String = "PowerSync Kotlin SDK"
3+
internal expect fun userAgent(): String
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.powersync.sync
2+
3+
internal actual fun userAgent(): String {
4+
val os = System.getProperty("os.name") ?: "unknown"
5+
val osVersion = System.getProperty("os.version") ?: ""
6+
val java = System.getProperty("java.vendor.version") ?: System.getProperty("java.runtime.version") ?: "unknown"
7+
8+
return "PowerSync Kotlin SDK (running Java $java on $os $osVersion)"
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.powersync.sync
2+
3+
import kotlin.experimental.ExperimentalNativeApi
4+
5+
@OptIn(ExperimentalNativeApi::class)
6+
internal actual fun userAgent(): String = "PowerSync Kotlin SDK (running on ${Platform.cpuArchitecture.name} ${Platform.osFamily.name})"

0 commit comments

Comments
 (0)