feat(mobile): use custom headers when connecting in widget (#20666)

* feat(mobile): use custom headers when connecting in widget

* delete log in android widget

* chore: code review changes
This commit is contained in:
Brandon Wees
2025-08-05 10:29:27 -05:00
committed by GitHub
parent 7a7843467c
commit 13d43e193e
6 changed files with 58 additions and 8 deletions

View File

@@ -24,14 +24,23 @@ class ImmichAPI(cfg: ServerConfig) {
val serverURL = prefs.getString("widget_server_url", "") ?: ""
val sessionKey = prefs.getString("widget_auth_token", "") ?: ""
val customHeadersJSON = prefs.getString("widget_custom_headers", "") ?: ""
if (serverURL.isBlank() || sessionKey.isBlank()) {
return null
}
var customHeaders: Map<String, String> = HashMap<String, String>()
if (customHeadersJSON.isNotBlank()) {
val stringMapType = object : TypeToken<Map<String, String>>() {}.type
customHeaders = Gson().fromJson(customHeadersJSON, stringMapType)
}
return ServerConfig(
serverURL,
sessionKey
sessionKey,
customHeaders
)
}
}
@@ -50,11 +59,19 @@ class ImmichAPI(cfg: ServerConfig) {
return URL(urlString.toString())
}
private fun HttpURLConnection.applyCustomHeaders() {
serverConfig.customHeaders.forEach { (key, value) ->
setRequestProperty(key, value)
}
}
suspend fun fetchSearchResults(filters: SearchFilters): List<Asset> = withContext(Dispatchers.IO) {
val url = buildRequestURL("/search/random")
val connection = (url.openConnection() as HttpURLConnection).apply {
requestMethod = "POST"
setRequestProperty("Content-Type", "application/json")
applyCustomHeaders()
doOutput = true
}
@@ -75,6 +92,7 @@ class ImmichAPI(cfg: ServerConfig) {
val url = buildRequestURL("/memories", listOf("for" to iso8601))
val connection = (url.openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
applyCustomHeaders()
}
val response = connection.inputStream.bufferedReader().readText()
@@ -94,6 +112,7 @@ class ImmichAPI(cfg: ServerConfig) {
val url = buildRequestURL("/albums")
val connection = (url.openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
applyCustomHeaders()
}
val response = connection.inputStream.bufferedReader().readText()

View File

@@ -55,7 +55,11 @@ data class WidgetEntry (
val deeplink: String?
)
data class ServerConfig(val serverEndpoint: String, val sessionKey: String)
data class ServerConfig(
val serverEndpoint: String,
val sessionKey: String,
val customHeaders: Map<String, String>
)
// MARK: Widget State Keys
val kImageUUID = stringPreferencesKey("uuid")