File tree Expand file tree Collapse file tree 8 files changed +54
-11
lines changed
PowerSyncKotlin/src/appleMain/kotlin/com/powersync
androidMain/kotlin/com/powersync/sync
commonMain/kotlin/com/powersync/sync
jvmMain/kotlin/com/powersync/sync
nativeMain/kotlin/com/powersync/sync Expand file tree Collapse file tree 8 files changed +54
-11
lines changed Original file line number Diff line number Diff line change 33package com.powersync
44
55import 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
1718public 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+ )
Original file line number Diff line number Diff line change 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 } )"
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ import kotlin.coroutines.CoroutineContext
4242 */
4343@OptIn(RSocketTransportApi ::class , ExperimentalPowerSyncAPI ::class )
4444internal 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(
119127private class ConnectionSetupMetadata (
120128 val token : String ,
121129 @SerialName(" user_agent" )
122- val userAgent : String = userAgent() ,
130+ val userAgent : String ,
123131)
124132
125133/* *
Original file line number Diff line number Diff 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 /* *
Original file line number Diff line number Diff 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,
Original file line number Diff line number Diff line change 11package com.powersync.sync
22
3- internal fun userAgent (): String = " PowerSync Kotlin SDK "
3+ internal expect fun userAgent (): String
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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} )"
You can’t perform that action at this time.
0 commit comments