-
Notifications
You must be signed in to change notification settings - Fork 13
Add User-Agent field #158
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
base: main
Are you sure you want to change the base?
Add User-Agent field #158
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.powersync.sync | ||
|
||
import android.os.Build | ||
|
||
internal actual fun getOS(): String { | ||
val base = Build.VERSION.BASE_OS | ||
val version = Build.VERSION.SDK_INT | ||
return "android $base/$version" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import com.powersync.bucket.BucketStorage | |
import com.powersync.bucket.Checkpoint | ||
import com.powersync.bucket.WriteCheckpointResponse | ||
import com.powersync.connectors.PowerSyncBackendConnector | ||
import com.powersync.core.BuildKonfig.LIBRARY_NAME | ||
import com.powersync.core.BuildKonfig.LIBRARY_VERSION | ||
import com.powersync.db.crud.CrudEntry | ||
import com.powersync.utils.JsonUtil | ||
import io.ktor.client.HttpClient | ||
|
@@ -35,6 +37,22 @@ import kotlinx.coroutines.flow.flow | |
import kotlinx.datetime.Clock | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlin.collections.MutableSet | ||
import kotlin.collections.buildList | ||
import kotlin.collections.component1 | ||
import kotlin.collections.component2 | ||
import kotlin.collections.filter | ||
import kotlin.collections.forEach | ||
import kotlin.collections.isNotEmpty | ||
import kotlin.collections.joinToString | ||
import kotlin.collections.listOf | ||
import kotlin.collections.map | ||
import kotlin.collections.mutableMapOf | ||
import kotlin.collections.mutableSetOf | ||
import kotlin.collections.set | ||
import kotlin.collections.toList | ||
import kotlin.collections.toMutableList | ||
import kotlin.collections.toMutableSet | ||
|
||
internal class SyncStream( | ||
private val bucketStorage: BucketStorage, | ||
|
@@ -172,7 +190,7 @@ internal class SyncStream( | |
contentType(ContentType.Application.Json) | ||
headers { | ||
append(HttpHeaders.Authorization, "Token ${credentials.token}") | ||
append("User-Id", credentials.userId ?: "") | ||
append("User-Agent", powerSyncUserAgent()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we're doing this on all requests, a better place to put this may be in fun HttpClientConfig<*>.configureClient() {
install(HttpTimeout)
install(ContentNegotiation)
defaultRequest {
headers {
append("User-Agent", $LIBRARY_NAME/$LIBRARY_VERSION ${getOS()}")
}
}
} |
||
} | ||
} | ||
if (response.status.value == 401) { | ||
|
@@ -186,6 +204,8 @@ internal class SyncStream( | |
return body.data.writeCheckpoint | ||
} | ||
|
||
private fun powerSyncUserAgent(): String = "$LIBRARY_NAME/$LIBRARY_VERSION ${getOS()}" | ||
|
||
private fun streamingSyncRequest(req: StreamingSyncRequest): Flow<String> = | ||
flow { | ||
val credentials = connector.getCredentialsCached() | ||
|
@@ -200,7 +220,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) | ||
|
@@ -449,6 +469,8 @@ internal class SyncStream( | |
} | ||
} | ||
|
||
internal expect fun getOS(): String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer this to be in a separate file (e.g. |
||
|
||
internal data class SyncStreamState( | ||
var targetCheckpoint: Checkpoint?, | ||
var validatedCheckpoint: Checkpoint?, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.powersync.sync | ||
|
||
import platform.UIKit.UIDevice | ||
|
||
internal actual fun getOS(): String { | ||
val current = UIDevice.currentDevice | ||
val version = current.systemVersion | ||
|
||
return "ios $version" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.powersync.sync | ||
|
||
internal actual fun getOS(): String { | ||
val os = System.getProperty("os.name") | ||
val version = System.getProperty("os.version") | ||
return "jvm $os/$version" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't have to worry about this. We'll likely add an API for clients to add their own headers in the future. The Swift SDK could use that to always override the
User-Agent
header if necessary. So it should be fine if this always reportspowersync-kotlin
for now.