summaryrefslogtreecommitdiff
path: root/app/src/main/java/org/terst/feedreader/stash/StashClient.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/terst/feedreader/stash/StashClient.kt')
-rw-r--r--app/src/main/java/org/terst/feedreader/stash/StashClient.kt29
1 files changed, 29 insertions, 0 deletions
diff --git a/app/src/main/java/org/terst/feedreader/stash/StashClient.kt b/app/src/main/java/org/terst/feedreader/stash/StashClient.kt
index 28cc2b3..b613d66 100644
--- a/app/src/main/java/org/terst/feedreader/stash/StashClient.kt
+++ b/app/src/main/java/org/terst/feedreader/stash/StashClient.kt
@@ -36,6 +36,11 @@ sealed class StashSaveResult {
data class Failed(val message: String) : StashSaveResult()
}
+sealed class StashVerifyResult {
+ object Valid : StashVerifyResult()
+ data class Invalid(val message: String) : StashVerifyResult()
+}
+
/**
* Client for the parts of Stash's API this app needs: POST /artifacts to
* archive an article. Uses sync=true so content_md comes back in the same
@@ -48,6 +53,30 @@ class StashClient(
private val baseUrl: String,
private val apiKey: String
) {
+ // A cheap authenticated GET against the same collection /artifacts POSTs to, rather than
+ // a dedicated /me or /health endpoint this API doesn't document -- reads are safe/free of
+ // side effects, unlike actually creating an artifact just to test the key works.
+ suspend fun verifyCredentials(): StashVerifyResult = withContext(Dispatchers.IO) {
+ if (baseUrl.isBlank() || apiKey.isBlank()) {
+ return@withContext StashVerifyResult.Invalid("Base URL and API key are both required")
+ }
+ val request = Request.Builder()
+ .url("$baseUrl/artifacts")
+ .header("X-API-Key", apiKey)
+ .get()
+ .build()
+
+ runCatching {
+ client.newCall(request).execute().use { response ->
+ when {
+ response.isSuccessful -> StashVerifyResult.Valid
+ response.code == 401 || response.code == 403 -> StashVerifyResult.Invalid("Rejected: API key not accepted")
+ else -> StashVerifyResult.Invalid("HTTP ${response.code}")
+ }
+ }
+ }.getOrElse { StashVerifyResult.Invalid(it.message ?: "Couldn't reach that URL") }
+ }
+
suspend fun saveArticle(url: String, title: String?): StashSaveResult = withContext(Dispatchers.IO) {
if (baseUrl.isBlank() || apiKey.isBlank()) {
return@withContext StashSaveResult.Failed("Stash isn't configured (set base URL and API key in Settings)")