Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

LucDeCaf
Copy link

@LucDeCaf LucDeCaf commented Apr 2, 2025

Adds User-Agent in similar fashion to Dart and JS repos.

Based on abandoned pull #57

Todos

  • Replace android $base/$version with android <SDK_INT or OS version> - must check if can extract version string from SDK_INT

@LucDeCaf LucDeCaf changed the title Add User-Agent and remove User-Id Add User-Agent field Apr 2, 2025
Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must check if can extract version string from SDK_INT

I think including Build.VERSION and Build.RELEASE_OR_CODENAME should be enough here.

@@ -449,6 +469,8 @@ internal class SyncStream(
}
}

internal expect fun getOS(): String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this to be in a separate file (e.g. utils/OSInfo.kt).

@@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The 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 init:

fun HttpClientConfig<*>.configureClient() {
    install(HttpTimeout)
    install(ContentNegotiation)

    defaultRequest {
        headers {
            append("User-Agent", $LIBRARY_NAME/$LIBRARY_VERSION ${getOS()}")
        }
    }
}

Comment on lines +330 to +332
// TODO: Swift SDK relies on this too.
// Find out how to add a build flag to toggle between "powersync-kotlin" and "powersync-swift".
buildConfigField(STRING, "LIBRARY_NAME", "powersync-kotlin")
Copy link
Contributor

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 reports powersync-kotlin for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants